如何根据 react-admin 中 BooleanInput 字段的状态动态显示或隐藏表单字段?
How to dynamically show or hide form fields depending on the state of a BooleanInput field in react-admin?
我需要用 react-admin 构建一个复杂的编辑表单。该表单具有各种 yes/no 滑块,这些滑块是使用 react-admin 的 BooleanInput 组件制作的。
如果用户将滑块设置为 "yes",则应动态显示更多表单字段,这些字段在主题上与滑块相关。我如何查询 BooleanInput 组件的状态,或者这个任务是否会以不同的方式在 React 中解决?
<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" />
你可以在 JSX 中动态编写代码
> { isShow ? <TextInput ... /> : null }
我发现:可以像这样使用 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
我需要用 react-admin 构建一个复杂的编辑表单。该表单具有各种 yes/no 滑块,这些滑块是使用 react-admin 的 BooleanInput 组件制作的。 如果用户将滑块设置为 "yes",则应动态显示更多表单字段,这些字段在主题上与滑块相关。我如何查询 BooleanInput 组件的状态,或者这个任务是否会以不同的方式在 React 中解决?
<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" />
你可以在 JSX 中动态编写代码
> { isShow ? <TextInput ... /> : null }
我发现:可以像这样使用 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