ArrayInput with SimpleFormIterator and conditional inputs

一个人想着一个人 提交于 2021-01-28 00:30:24

问题


Let's say I have the following array in my create form

    const CreateDefaults = {Inputs: [{id: "ID1", param: "a"},{id: "ID2", param: "b"}]};

and then I want to show extra TextInput only when id==="ID2"

export const MyCreate = withStyles(myStyles)(({ classes, ...props }) => (
<Create {...props}>
  <SimpleForm defaultValue={CreateDefaults}>
    <ArrayInput source="Inputs" {...props}>
      <SimpleFormIterator {...props}>
        <DisabledInput source="id" />
        {/*this does not work*/}
        {this.id === "ID2" ? (<TextInput source="ParamValue"/>) :null}
      </SimpleFormIterator>
    </ArrayInput>
  </SimpleForm>
</Create>
));

How can I do something like that? I know that for the whole form, one can use FormDataConsumer. However, what can one do inside ArrayInput/SimpleFormIterator for each iteration?

How to access current object in iteration? I tried something like the answer given to the 'Custom Input do not receive record when inside ArrayInput' issue in the react-admin github page, but it still does not receive the record in custom input.


回答1:


From the latest documentation here, you can see that if you use FormDataConsumer that is within an ArrayInput, you have a parameter called scopedFormData that you can use to get the current record and dereference that to get whatever fields you need. It usually also goes hand in hand with the getSource function you can use when setting the source within your FormDataConsumer.



来源:https://stackoverflow.com/questions/51741302/arrayinput-with-simpleformiterator-and-conditional-inputs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!