跳到主要内容

Form 表单

表单

import { Form, FormItem, FormType, Input, Wrapper } from "@finalx/components";

interface IProps {}
interface IFields {
name: string;
idCard: string;
mobile: string;
accNo: string;
}
export const Index: FC<IProps> = () => {
const [status, setStatus] = useState(false);
const formRef = useRef<FormType>({} as FormType);
const onSubmit = async () => {
const validateValues: IFields = await formRef.current.validateFields();
console.log("validateValues", validateValues);
};

return (
<Wrapper>
<Form formRef={formRef} labelColor='white'>
<FormItem name='name' label='名称' required disabled>
<Input placeholder='请输入名称' placeholderStyle='color: rgb(255,255,255,0.4)' />
</FormItem>

<FormItem name='idCard' label='身份证号码' required rules={{ type: "idCard" }}>
<Input placeholder='请输入身份证号码' placeholderStyle='color: rgb(255,255,255,0.4)' />
</FormItem>
<FormItem name='mobile' label='手机号' required rules={[{ type: "phone" }]}>
<Input placeholder='请输入手机号' placeholderStyle='color: rgb(255,255,255,0.4)' />
</FormItem>

<FormItem
name='accNo'
label='银行卡号'
required
rules={{
custom: val => {
console.log("val", val);
const regex = /^\d{12,19}$/;
const isValid = regex.test(val);
return isValid;
},
message: "请输入正确的银行卡号"
}}
>
<Input placeholder='请输入' placeholderStyle='color: rgb(255,255,255,0.4)' />
</FormItem>
</Form>
<Button onClick={onSubmit}>确认</Button>
</Wrapper>
);
};

Form 组件属性

属性名类型默认值必填描述
formRefRef<Form>-组件的操作实体
childrenReactElement | ReactElement[] | boolean | (boolean | ReactElement)[]-组件子组件
initFields{ [key: string]: any }-组件初始化值
itemHeightstring-表单项高度
labelSizestring-表单项标签大小
labelColorstring-表单项标签颜色
errorColorstring-表单错误显示颜色
disabledboolean-表单是否禁用
layout"normal" | "between"between表单布局
errorAligin"left" | "right"right错误显示布局
hiddenRequiredIconboolean-是否隐藏必填的图标
requiredIconStyleCSSProperties-必填图标的样式

FormItem 组件属性

属性名类型默认值必填描述
namestring-表单项的唯一标识
rulesRuleType | RuleType[]-表单校验规则
childrenReactElement-组件子组件
itemHeightstring-表单项高度
labelstring-表单项标签
labelSizestring-表单项标签大小
labelColorstring-表单项标签颜色
errorColorstring-表单错误显示颜色
requiredbooleanfalse表单是否必填
disabledbooleanfalse表单是否禁用
layout"normal" | "between"between表单布局
errorAligin"left" | "right"right错误显示布局
hiddenRequiredIconboolean-是否隐藏必填的图标
requiredIconStyleCSSProperties-必填图标的样式

RuleType 属性

属性名类型默认值必填描述
type"email" | "phone" | "idCard"-内置的表单校验类型
messagestring-校验失败报错内容
custom(val: any) => boolean-自定义报错校验规则

Form 组件事件

属性名参数必填描述
onChange-表单更新事件