本文介绍了重置AntSub库上的on.Submit单击上的Radio.Group值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ant-design库.有useForm()钩可以重置值.

I am using ant-design library. There is useForm() hook to reset values.

所有值都将重置为原始值,但不是Radio.Group.

All values are being reset to original values but not for Radio.Group.

如何将单选按钮的状态从State重置为priority值?

How do I reset my radio button state to priority value from State?

  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添加了优先级,并且它起作用了.

I added priority to the initialValues and it worked.

请参阅.

https://codesandbox.io/s/Elegant-agnesi-9vp4x?file =/src/App.js

这篇关于重置AntSub库上的on.Submit单击上的Radio.Group值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 10:09