重置 Radio.Group 值 onSubmit 点击 antd 库
Reset Radio.Group values onSubmit click for antd library
我正在使用 ant-design 库。有 useForm()
钩子来重置值。
所有值都将重置为原始值,但 Radio.Group 不会。
如何将我的单选按钮状态重置为状态中的 priority
值?
const [priority, setPriority] = useState(2);
const [form] = Form.useForm();
const onSubmit = (values) => {
console.log('Received values of form: ', values);
form.resetFields();
}
<Form
form={form}
labelCol={{ span: 4 }}
wrapperCol={{ span: 14 }}
layout="horizontal"
initialValues={{ size: "large" }}
size={"large"}
onFinish={onSubmit}
>
<Form.Item
name="title"
rules={[{ required: true, message: 'Title is required!' }]}
label="Title">
<Input placeholder="Title" />
</Form.Item>
<Form.Item
name="content"
rules={[{ required: true, message: 'Content is required!' }]}
label="Content">
<Input.TextArea placeholder="Content" />
</Form.Item>
<Form.Item label="Switch" name="switch">
<Switch />
</Form.Item>
<Form.Item name="priority" label="Radio.Button">
<Radio.Group defaultValue={priority}
onChange={onPriorityChange}>
<Radio.Button value={2}>High</Radio.Button>
<Radio.Button value={1}>Medium</Radio.Button>
<Radio.Button value={0}>Low</Radio.Button>
</Radio.Group>
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
我为 initialValues 添加了优先级并且它起作用了。
参考这个。
https://codesandbox.io/s/elegant-agnesi-9vp4x?file=/src/App.js
我正在使用 ant-design 库。有 useForm()
钩子来重置值。
所有值都将重置为原始值,但 Radio.Group 不会。
如何将我的单选按钮状态重置为状态中的 priority
值?
const [priority, setPriority] = useState(2);
const [form] = Form.useForm();
const onSubmit = (values) => {
console.log('Received values of form: ', values);
form.resetFields();
}
<Form
form={form}
labelCol={{ span: 4 }}
wrapperCol={{ span: 14 }}
layout="horizontal"
initialValues={{ size: "large" }}
size={"large"}
onFinish={onSubmit}
>
<Form.Item
name="title"
rules={[{ required: true, message: 'Title is required!' }]}
label="Title">
<Input placeholder="Title" />
</Form.Item>
<Form.Item
name="content"
rules={[{ required: true, message: 'Content is required!' }]}
label="Content">
<Input.TextArea placeholder="Content" />
</Form.Item>
<Form.Item label="Switch" name="switch">
<Switch />
</Form.Item>
<Form.Item name="priority" label="Radio.Button">
<Radio.Group defaultValue={priority}
onChange={onPriorityChange}>
<Radio.Button value={2}>High</Radio.Button>
<Radio.Button value={1}>Medium</Radio.Button>
<Radio.Button value={0}>Low</Radio.Button>
</Radio.Group>
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
我为 initialValues 添加了优先级并且它起作用了。
参考这个。
https://codesandbox.io/s/elegant-agnesi-9vp4x?file=/src/App.js