javascript - How to dynamically show or hide form fields depending on the state of a BooleanInput field in react-admin?
问题描述
I need to build a complex edit form with react-admin. The form has a variety of yes/no sliders made with the BooleanInput component of react-admin. If the user sets the slider to "yes", further form fields should appear dynamically, which refer thematically to the slider. How do I query the status of the BooleanInput component or would this task be solved in react in a different way?
<BooleanInput source="yesno" label="show or hide fields" />
<AutocompleteArrayInput source="probably_hidden1" label="show or hide me" choices={[
{ id: 'one', name: '1' },
{ id: 'two', name: '2' },
{ id: 'three', name: '3' }
]} />
<TextInput multiline source="text" label="show or hide me too" />
解决方案
我发现:可以像这样使用 FormDataConsumer 来完成:
<BooleanInput source="yesno" label="show or hide fields" />
<FormDataConsumer>
{({ formData, ...rest }) => formData.yesno && <div>
<AutocompleteArrayInput source="yesno" label="show or hide fields" choices={[
{ id: 'one', name: '1' },
{ id: 'two', name: '2' },
{ id: 'three', name: '3' }
]} {...rest} />
<TextInput multiline source="text" label=""show or hide me too" {...rest} />
</div>
}
</FormDataConsumer>
请参阅: https ://marmelab.com/react-admin/Inputs.html#hiding-inputs-based-on-other-inputs
推荐阅读
- c# - Net Core:在返回 Html 的自定义标签助手中使用标签助手?
- sql - SQL Server:可选参数行为逻辑
- python - 使用 int 和 None 将字符串转换为数组
- python - 带有 :not 的 CSS 选择器未按预期运行
- kubernetes - 如何为 Kubernetes 配置“efs-provider”?
- javascript - 如何在文档中查找 _id 并更新数组中的特定值?
- python - 试图验证中心极限定理,生成的图有点混乱
- django - Gunicorn 没有看到新设置
- php - 在抓取大网址时,我总是使用 simple_html_dom php 被禁止
- java - 不同环境的不同导入依赖