<SelectInput>: 如何根据react-admin 的编辑组件中的记录数据填充选项?

<SelectInput>: how to populate choices depending on record data in Edit component of react-admin?

我需要根据记录的字段值显示不同的选择。

我需要的代码如下所示:

const myChoicesGenerator = (record) => {
    if (record.field === false) {
      return ['a', 'b'];
    } else {
      return ['b', 'c'];
    }

<SelectInput ... choices = {myChoicesGenerator}/>

但不幸的是我无法在 "choices" 属性 中传递函数,所以这段代码不起作用。

有办法吗?

您可以使用 <FormDataConsumer /> 获取当前记录并将其传递给您的函数。

<FormDataConsumer>
    {
        ({formData, ...rest}) =>
            <SelectInput 
                 choices={myChoiceGenerator(formData)}
                 {...rest}
            />
    }
</FormDataConsumer>

文档:https://marmelab.com/react-admin/Inputs.html#linking-two-inputs

我不知道值 ['a', 'b'] 是否对 <SelectInput /> 有效。考虑使用 documentation.

中描述的元组列表 (id, name)