dmz9
dmz9

Reputation: 333

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

I need to show different choices based on record's field value.

The code for my needs looks like this:

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

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

But unfortunately i can't pass functions in "choices" property, so this code doesn't work.

Is there a way to do it?

Upvotes: 4

Views: 2181

Answers (1)

despatates
despatates

Reputation: 133

You may use a <FormDataConsumer /> to get the current record and pass it to your function.

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

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

I dont' know if the values ['a', 'b'] are valid for <SelectInput />. Consider using a list of tuples (id, name) as described in the documentation.

Upvotes: 4

Related Questions