Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 531
dmz9 Avatar asked Oct 21 '25 15:10

dmz9


1 Answers

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.

like image 143
despatates Avatar answered Oct 23 '25 19:10

despatates



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!