Skip to main content

Form

just one hook~.

hook:

  • useFields

fields:

  • fieldText
http://localhost:3000
show code
import { useFields } from "mui-eazy";
import { useFormConfigCreater_root } from "./formConfig";

export default () => {
const formConfig = useFormConfigCreater_root()();
const { formNode } = useFields(formConfig);
return <>{formNode}</>;
};

Props

tip
  • Type: any, can be customized as needed.
  • Default value:
    • Not specified: optional.
    • Specified as "require": mandatory.
  • Type link: Click to navigate to the details below.
OptionTypeDefaultDescription
formConfigrequired

Ts Type

FormConfigItem

OptionTypeDefaultDescription
idstring-可配置,不配置变自动生成唯一值 uuid
prefixstring-表单名前缀
typeInputType表单类型
schemaany使用yup创建的表单校验
defaultValueunknown表单的默认值,往往用于表单回显
namestring表单的名字,作为表单提交的字段名
labelstring表单项的标题
isHiddenboolean是否隐藏
fieldConfigFieldConfigrequired表单项组件的配置
configInputConfigrequired基础 Input 组件的配置
watchFunctionrequired监听

FieldConfig

type FieldConfig = Partial<RHFTextFieldProps> &
Partial<RHFSelectProps> &
Partial<RHFUploadProps> &
Partial<RHFDatePickerProps> &
Partial<RHFCheckboxProps> &
Partial<RHFEditorProps> &
Partial<RHFMultiSelectProps>;

InputConfig

type InputConfig = {
options?:
| string[]
| {
key: string | number;
value: string | number;
label: string | number;
}[];
};

watch

type watch = (props: {
currentConfig: Omit<FormConfigItem, "watch">;
values: { [key: string]: any };
info: Info;
api: UseFormReturn<any>;
}) => void;

interface Info {
name: string;
type: EventType;
}

export type EventType =
| "focus"
| "blur"
| "change"
| "changeText"
| "valueChange"
| "contentSizeChange"
| "endEditing"
| "keyPress"
| "submitEditing"
| "layout"
| "selectionChange"
| "longPress"
| "press"
| "pressIn"
| "pressOut"
| "momentumScrollBegin"
| "momentumScrollEnd"
| "scroll"
| "scrollBeginDrag"
| "scrollEndDrag"
| "load"
| "error"
| "progress"
| "custom";

props

OptionTypeDescription
currentConfigFormConfigItem该表单项目的配置信息,可以进行查询和修改
values{[key: string]: any}所属表单下的所有表单项的键值对结合。
infoInfo事件的信息
apiUseFormReturn<any> react hook form 的 form 实例

demo

{
...
watch: ({ currentConfig, values, info, api }) => {
if (info.name == 'Parent_Category_id') {
currentConfig.config!.options = allChildCategory
.filter((item) => {
let flag = item.parent_id == values['Parent_Category_id'];
return flag;
})
.map((item) => {
const { id, name } = item;
return {
value: id,
key: id,
label: name,
};
});
}
},
...
}