如何根据react-admin中BooleanInput字段的状态动态显示或隐藏表单字段?

els*_*sni 3 javascript jsx reactjs react-admin

我需要使用react-admin 构建一个复杂的编辑表单。该表单有各种是/否滑块,由react-admin 的 BooleanInput 组件制作。如果用户将滑块设置为“是”,则应动态显示更多表单字段,这些字段在主题上引用滑块。如何查询 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" />
Run Code Online (Sandbox Code Playgroud)

els*_*sni 7

我发现:可以使用 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>
Run Code Online (Sandbox Code Playgroud)

请参阅: https ://marmelab.com/react-admin/Inputs.html#hiding-inputs-based-on-other-inputs