Search code examples
admin-on-rest

Admin-on-Rest Is there a way to get rid of duplicates in ReferenceInput for Filters?


I am running into an issue where I am trying to filter on a column called "Severity", there are duplicates within the column. This column contains ERROR, INFO, or DEBUG. When I use this:

const LogFilter = (props) => (
     <Filter {...props}>
        <TextInput label="Search" source="q" alwaysOn />
        <ReferenceInput label="Severity" source="severity" reference="archivedfiles" allowEmpty>
            <SelectInput optionText="Severity" />
        </ReferenceInput>
     </Filter>
);

In my drop down I get a list of tons of things repeated... Is there a way to make it have only the options I want with no duplicates?


Solution

  • What you need is a HigherOrder component to wrap the SelectInput and filter things out before they hit the select input.

    const SelectUniqueChoices = WrappedComponent => props => {
      const filteredChoices = UniqueElements(props.choices)
      const newProps = {...props}
      newProps.choices = filteredChoices 
      return (<WrappedComponent {...newProps} />)
    }
    
    const UniqueElements = (array) => (
       Array.from(new Set(array))
    )
    
    const WrappedSelectField = SelectUniqueChoices(SelectInput)
    
    const LogFilter = (props) => (
         <Filter {...props}>
            <TextInput label="Search" source="q" alwaysOn />
            <ReferenceInput label="Severity" source="severity" reference="archivedfiles" allowEmpty>
                <WrappedSelectField optionText="Severity" />
            </ReferenceInput>
         </Filter>
    );