文档地址: 链接.
import { Form, Input, Button, Select, Space } from "antd";
export default function App() {
const [form] = Form.useForm();
return (
<Form
form={form}
onFinish={(values) => {
console.log(values);
}}
onFinishFailed={(errInfo) => {
console.log(errInfo);
}}
>
<Form.Item
label="笔记"
name="note"
rules={[
{
required: true,
message: "笔记为必填项.",
},
]}
>
<Input />
</Form.Item>
<Form.Item
label="性别"
name="gender"
rules={[
{
required: true,
message: "性别为必填项.",
},
]}
>
<Select
placeholder="选择一个选项并更改上面的输入文本"
onChange={(value) => {
switch (value) {
case "male":
form.setFieldsValue({
note: "你好,男人",
});
break;
case "female":
form.setFieldsValue({
note: "你好,女人",
});
break;
case "other":
form.setFieldsValue({
note: "男女?",
});
break;
default:
break;
}
}}
>
<Select.Option value="male">男性</Select.Option>
<Select.Option value="female">女性</Select.Option>
<Select.Option value="other">保密</Select.Option>
</Select>
</Form.Item>
<Form.Item
noStyle
shouldUpdate={(prevValues, currentValues) => {
return prevValues.gender !== currentValues.gender;
}}
>
{(props) => {
return props.getFieldValue("gender") === "other" ? (
<Form.Item
label="定制性别"
name="customizeGender"
rules={[
{
required: true,
message:'定制性别为必填项.'
},
]}
>
<Input />
</Form.Item>
) : null;
}}
</Form.Item>
<Form.Item>
<Space>
<Button type="primary" htmlType="submit">提交</Button>
<Button
onClick={() => {
form.resetFields();
}}
>重置</Button>
<Button
onClick={() => {
form.setFieldsValue({
note: "hello world",
gender: "other",
});
}}
>填充表单</Button>
<Button
onClick={() => {
let res = form.getFieldValue();
console.log(res);
}}
>获取表单值</Button>
</Space>
</Form.Item>
</Form>
);
}
|