How can I filter RadioButton choices based on another field in react-admin?

454 views Asked by At

I am using read-admin. https://marmelab.com/react-admin . I have a ReferenceArrayInput field named "Members" for selecting multiple values. I also have a RadioButtonGroupInput named "Manager" whose choices depend upon the values selected for "Members" i.e Manager can only be one of the members. How do I display only the selected members as options in Manager field? I used FormDataConsumer to get current form values and use the members as choices in manager field. But it shows all the operators as choices.

Here is my file.jsx

export const MyCreate = (props) => {

  return (
    <Create {...props}>
      <SimpleForm>
        
        <ReferenceArrayInput
          source="members"
          reference="operator"
          label="Members"
        >
          <SelectArrayInput optionText='name'/>
        </ReferenceArrayInput>

        <FormDataConsumer>
          {({ formData, ...rest }) => (
            <ReferenceInput
              source="manager"
              label="Manager"
              reference="operator"
            >
              <RadioButtonGroupInput
                optionText="name"
                choices={formData.members}
              />
            </ReferenceInput>
          )}
        </FormDataConsumer>

      </SimpleForm>
    </Create>
  )
}
1

There are 1 answers

0
insafski On

Try this:

<FormDataConsumer>
   {({ formData, ...rest }) => (
      <RadioButtonGroupInput
          optionText="name"
          source="manager"
          choices={formData.members}
      />
   )}
</FormDataConsumer>