0

I'm using on react-admin and try to add a filter options to a list.

My problem is how to add a filter input based on another filter input?

I want to filter the list by organization and by project bu project is associated to an organization so I want to enable select a project only after organization is selected and and only of projects associated tothe selected organization.

That is waht I try to do:

 const ProjectInputs = () => {
    const { values } = useFormState();
    const translate = useTranslate();
    const classes = useStyles();

    return (

        <ReferenceInput
            label={translate("resources.projects.name", { smart_count: 1 })}
            source="projectId"
            reference="projects"
            filter={{ organizationId: values.organizationId }}
            disabled={!values.organizationId}
            alwaysOn
            variant="standard"
        >
            <SelectInput
                optionText="name"
                resettable
                alwaysOn
            />

        </ReferenceInput>

    );
}
export const Paymentilter: FC<Omit<FilterProps, 'children'>> = props => {
const classes = useFilterStyles();
return (
    <Filter {...props}>

        <ReferenceInput
            source="organizationId"
            reference="organizations"
            variant="standard"
        >
            <AutocompleteInput
                optionText={(choice?: Organization) =>
                    choice?.id // the empty choice is { id: '' }
                        ? `${choice.name}`
                        : ''
                }

            />
        </ReferenceInput>

        <ProjectInputs />
    </Filter>
);

};

It works but there are some problems:

  1. I cant define the project input to be always on.
  2. In the add filter button there is no name for this input only blank shrink space.
  3. The position of the project input is unordered input is popping up.
  4. after choosing this filter I cant close it.

Do you have an idea for me?

How to fix my problems?

Sholud I solve my issue in another way?

Thank you!

EDIT:

I did like @doctoragon way, and that is ok. in this way the project input cant be choosen from the add filter button only when we choose an organization it apears and when we cancel it disaper.

That ok for me but the second input still looks different as you can see on the picture it is upper then al the others inputs, and the cancel button is overirded by the chosen option. the project input before we choose a project the project input after we choose a project

1 Answer 1

1

You might be able to solve this using a FormDataConsumer inside your Filter.

<Filter {...props}>
  <ReferenceInput source="organizationId" ... />
  <FormDataConsumer source="projects" alwaysOn>
    {
      ({ formData, ...restOfTheProps }) => organizationId &&  <ProjectInputs />
    }
  </FormDataConsumer>
</Filter>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that works but only the apearnce not well, I edited the questiion and add screenshot to explain the problem –
Try adding a source prop to your FormDataConsumer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.