简体   繁体   English

如何使用 react-final-form 添加或删除表单键

[英]How to add or remove form keys using react-final-form

single config example shows what configuration I use for inputs.单个配置示例显示了我用于输入的配置。

    export const age = [
      {
        value: '',
        type: 'radio',
        name: 'age',
        items: [
          {value: 'kid', label: 'Im less than 18',
          {value: 'adult', label: 'Im 18!',
        ],
      },
    ];

export const someDate = [
  {
    type: 'date',
    value: null,
    name: 'someDate',
    label: 'Enter date',
    format: 'dd/MM/yyyy',
    minDate: new Date('2019-12-31'),
    maxDate: new Date('2020-03-31'),
    validators: required.required.date
  }
];

RFForm is a wrapper for rect-final-form Form component and I pass all components inside as children. RFForm 是 rect-final-form Form 组件的包装器,我将所有组件作为子组件传入。

<RFForm
      config={[...age, ...someDate, ...somethingElse]}
      submit={submit}
    >
      <SectionBox title="Your age">
        <Box mb={5}>
          <InputList inputs={age}/>
        </Box>
        <SectionTitle title="Date"/>
        <Box maxWidth="50%">
          <InputList inputs={someDate}/>
        </Box>
      </SectionBox>

      <SectionBox title="Something else">
        <Box maxWidth="50%">
          <InputList inputs={somethingElse}/>
        </Box>
      </SectionBox>


     {
        conditionToHideOrShowSection && <SectionWithInputs />
     }

      {
        buttons
      }

    </RFForm>

I want to add a new section that will hide or show depends on what the user picks.我想添加一个新部分,该部分将根据用户选择的内容隐藏或显示。 The section should be ONLY present when the second radio button value is selected (adult).该部分应仅在选择第二个单选按钮值(成人)时出现。 The section will contain a component with inputs from the configuration.该部分将包含一个带有来自配置的输入的组件。 All the fields should be required.所有字段都应该是必需的。 This should only be validated when the section is present.这应该仅在该部分存在时进行验证。

  1. First question - how should I check if the specific radio button is checked.第一个问题 - 我应该如何检查是否选中了特定的单选按钮。 I don't have access to useForm from here and I need to hide or show section.我无法从这里访问 useForm,我需要隐藏或显示部分。
  2. Second question - how to dynamically add new inputs when other inputs are changing.第二个问题 - 如何在其他输入发生变化时动态添加新输入。 Remember that I have to add it to config (RFForm props) because initial value must be updated.请记住,我必须将其添加到 config (RFForm props) 中,因为必须更新初始值。
  1. Actually you have access to it, but in other way via render method link实际上您可以访问它,但以其他方式通过render方法链接

  2. This sample shows how to conditionally show inputs link .此示例展示了如何有条件地显示输入链接 Be aware if input hide back the data won't gone, it's still there , so to cleanup you should use change('inputName', undefined) (at least it helps me in my case)请注意,如果输入隐藏数据不会消失,它仍然存在,因此要清理您应该使用change('inputName', undefined) (至少在我的情况下它对我有帮助)

  3. At last you also should checkout about subscription prop.最后,您还应该查看subscription道具。 link 关联

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM