在表单中,每个 Form.Item
代表一个表单项
function BasicForm() {
const onSubmit = (event: BaseEventOrig<FormProps.onSubmitEventDetail>) => {
Toast.open(JSON.stringify(event.detail.value))
}
return (
<Form onSubmit={onSubmit}>
<Toast id="toast" />
<Cell.Group inset>
<Form.Item name="username" rules={[{ required: true, message: "请填写用户名" }]}>
<Form.Label>用户名</Form.Label>
<Form.Control>
<Input placeholder="用户名" />
</Form.Control>
</Form.Item>
<Form.Item name="password" rules={[{ required: true, message: "请填写密码" }]}>
<Form.Label>密码</Form.Label>
<Form.Control>
<Input password placeholder="密码" />
</Form.Control>
</Form.Item>
<Field
name="text"
label={{ align: "left", children: "文本" }}
rules={[{ required: true, message: "请填写文本" }]}
>
<Input placeholder="请输入文本" />
</Field>
</Cell.Group>
<View style={{ margin: "16px" }}>
<Button shape="round" block color="primary" formType="submit">
提交
</Button>
</View>
</Form>
)
}
使用Field
组件
function BasicForm() {
const onSubmit = (event: BaseEventOrig<FormProps.onSubmitEventDetail>) => {
Toast.open(JSON.stringify(event.detail.value))
}
return (
<Form onSubmit={onSubmit}>
<Toast id="toast" />
<Cell.Group inset>
<Field label="用户名" name="username" rules={[{ required: true, message: "请填写用户名" }]}>
<Input placeholder="用户名" />
</Field>
<Field label="密码" name="password" rules={[{ required: true, message: "请填写密码" }]}>
<Input password placeholder="密码" />
</Field>
</Cell.Group>
<View style={{ margin: "16px" }}>
<Button shape="round" block color="primary" formType="submit">
提交
</Button>
</View>
</Form>
)
}
通过 rules
定义表单校验规则,所有可用字段见下方表格。
function FormWithRules() {
const asyncValidator = (val: any) =>
new Promise<boolean>((resolve) => {
Toast.loading("验证中...")
setTimeout(() => {
Toast.close("toast")
resolve(/\d{6}/.test(val))
}, 1000)
})
function onValidate(errors: FormValidError[]) {
Toast.open({
style: {
textAlign: "left",
},
message: JSON.stringify(errors, undefined, 2),
})
}
return (
<Form defaultValues={{ validatorMessage: "abc" }} onValidate={onValidate}>
<Toast id="toast" />
<Cell.Group inset>
<Field label="文本" name="pattern" rules={[{ pattern: /\d{6}/, message: "请输入正确内容" }]}>
<Input placeholder="正则校验" />
</Field>
<Field
label="文本"
name="validator"
rules={[{ validator: (val) => /1\d{10}/.test(val), message: "请输入正确内容" }]}
>
<Input placeholder="函数校验" />
</Field>
<Field
label="文本"
name="validatorMessage"
rules={[{ validator: (val) => `${val ?? ""} 不合法,请重新输入` }]}
>
<Input placeholder="校验函数返回错误提示" />
</Field>
<Field
label="文本"
name="asyncValidator"
rules={[{ validator: asyncValidator, message: "请输入正确内容" }]}
>
<Input placeholder="异步函数校验" />
</Field>
</Cell.Group>
<View style={{ margin: "16px" }}>
<Button shape="round" block color="primary" formType="submit">
提交
</Button>
</View>
</Form>
)
}
设置 disabled
后,会为 Form
内部的 taroify
组件 Input
, Textarea
, Checkbox
, Switch
, Checkbox.Group
, Radio.Group
, Rate
, Slider
, Stepper
, Uploader
设置 disabled
form设置disabled后,也可以单独为表单项和组件设置disabled={false}, 优先级:表单 < 表单项 < 组件
在表单中使用 Checkbox 组件。
<Field label="复选框" name="checkbox">
<Checkbox shape="square" />
</Field>
<Field label="复选框组" name="checkboxGroup">
<Checkbox.Group direction="horizontal">
<Checkbox name="1" shape="square">
复选框 1
</Checkbox>
<Checkbox name="2" shape="square">
复选框 2
</Checkbox>
</Checkbox.Group>
</Field>
在表单中使用 Radio 组件。
<Field label="单选框" name="radio">
<Radio.Group direction="horizontal">
<Radio name="1">单选框 1</Radio>
<Radio name="2">单选框 2</Radio>
</Radio.Group>
</Field>
在表单中使用 Uploader 组件。
function UploaderField() {
const itemRef = useRef<FormItemInstance>()
function onUpload() {
chooseImage({
count: 1,
sizeType: ["original", "compressed"],
sourceType: ["album", "camera"],
}).then(({ tempFiles }) => {
itemRef.current?.setValue([
...(itemRef.current?.getValue() ? [...itemRef.current?.getValue()] : []),
{
url: tempFiles[0].path,
type: tempFiles[0].type,
name: tempFiles[0].originalFileObj?.name,
},
])
})
}
return (
<Field label="文件上传" ref={itemRef} name="uploader">
<Uploader multiple maxFiles={2} onUpload={onUpload} />
</Field>
)
}
在表单中使用 Picker 组件。
function PickerField() {
const itemRef = useRef<FormItemInstance>()
const [open, setOpen] = useState(false)
const columns = useMemo(() => [
{ label: "杭州", value: "Hangzhou" },
{ label: "