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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With