代码之家  ›  专栏  ›  技术社区  ›  dmz9

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

  •  1
  • dmz9  · 技术社区  · 6 年前

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

    我需要的代码如下:

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

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

    有办法吗?

    1 回复  |  直到 6 年前
        1
  •  3
  •   despatates    6 年前

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

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

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

    我不知道这些价值观 ['a', 'b'] 有效期为 <SelectInput /> . 考虑使用元组列表( id , name )如中所述 documentation .