feat: 新增表单只读功能 (#3335)

* fix(useFormEvents): 修复setFieldsValue 方法设置完值后,表单属性丢失formActionType 的bug

* feat: 新增 sync 的action

* feat(FormItem): 新增只读属性

* feat(FormItem): 新增表单只读属性

---------

Co-authored-by: gavin-james <meaganlindesy1258@gmail.com>
This commit is contained in:
林飞 2023-11-26 14:04:13 +08:00 committed by GitHub
parent 0f13758554
commit 342328ce5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View File

@ -116,6 +116,21 @@
return disabled; return disabled;
}); });
const getReadonly = computed(() => {
const { readonly: globReadonly } = props.formProps;
const { dynamicReadonly } = props.schema;
const { readonly: itemReadonly = false } = unref(getComponentsProps);
let readonly = globReadonly || itemReadonly;
if (isBoolean(dynamicReadonly)) {
readonly = dynamicReadonly;
}
if (isFunction(dynamicReadonly)) {
readonly = dynamicReadonly(unref(getValues));
}
return readonly;
});
function getShow(): { isShow: boolean; isIfShow: boolean } { function getShow(): { isShow: boolean; isIfShow: boolean } {
const { show, ifShow } = props.schema; const { show, ifShow } = props.schema;
const { showAdvancedButton } = props.formProps; const { showAdvancedButton } = props.formProps;
@ -280,6 +295,7 @@
size, size,
...unref(getComponentsProps), ...unref(getComponentsProps),
disabled: unref(getDisable), disabled: unref(getDisable),
readonly: unref(getReadonly),
}; };
const isCreatePlaceholder = !propsData.disabled && autoSetPlaceHolder; const isCreatePlaceholder = !propsData.disabled && autoSetPlaceHolder;
@ -305,7 +321,12 @@
return <Comp {...compAttr} />; return <Comp {...compAttr} />;
} }
const compSlot = isFunction(renderComponentContent) const compSlot = isFunction(renderComponentContent)
? { ...renderComponentContent(unref(getValues), { disabled: unref(getDisable) }) } ? {
...renderComponentContent(unref(getValues), {
disabled: unref(getDisable),
readonly: unref(getReadonly),
}),
}
: { : {
default: () => renderComponentContent, default: () => renderComponentContent,
}; };
@ -339,7 +360,7 @@
const { itemProps, slot, render, field, suffix, component } = props.schema; const { itemProps, slot, render, field, suffix, component } = props.schema;
const { labelCol, wrapperCol } = unref(itemLabelWidthProp); const { labelCol, wrapperCol } = unref(itemLabelWidthProp);
const { colon } = props.formProps; const { colon } = props.formProps;
const opts = { disabled: unref(getDisable) }; const opts = { disabled: unref(getDisable), readonly: unref(getReadonly) };
if (component === 'Divider') { if (component === 'Divider') {
return ( return (
<Col span={24}> <Col span={24}>
@ -397,7 +418,7 @@
const realColProps = { ...baseColProps, ...colProps }; const realColProps = { ...baseColProps, ...colProps };
const { isIfShow, isShow } = getShow(); const { isIfShow, isShow } = getShow();
const values = unref(getValues); const values = unref(getValues);
const opts = { disabled: unref(getDisable) }; const opts = { disabled: unref(getDisable), readonly: unref(getReadonly) };
const getContent = () => { const getContent = () => {
return colSlot return colSlot

View File

@ -85,6 +85,8 @@ export interface FormProps {
size?: 'default' | 'small' | 'large'; size?: 'default' | 'small' | 'large';
// Whether to disable // Whether to disable
disabled?: boolean; disabled?: boolean;
// Whether to readonly
readonly?: boolean;
// Time interval fields are mapped into multiple // Time interval fields are mapped into multiple
fieldMapToTime?: FieldMapToTime; fieldMapToTime?: FieldMapToTime;
// Placeholder is set automatically // Placeholder is set automatically
@ -218,6 +220,8 @@ interface BaseFormSchema {
dynamicDisabled?: boolean | ((renderCallbackParams: RenderCallbackParams) => boolean); dynamicDisabled?: boolean | ((renderCallbackParams: RenderCallbackParams) => boolean);
dynamicReadonly?: boolean | ((renderCallbackParams: RenderCallbackParams) => boolean);
dynamicRules?: (renderCallbackParams: RenderCallbackParams) => Rule[]; dynamicRules?: (renderCallbackParams: RenderCallbackParams) => Rule[];
} }
export interface ComponentFormSchema extends BaseFormSchema { export interface ComponentFormSchema extends BaseFormSchema {