({
filterOptions,
filter,
urlKey,
}: {
filterOptions: AtLeastOne<FilterOption>;
filter: FilterData;
urlKey?: string;
})
| 6 | import { type AtLeastOne } from "~/types/shared.types"; |
| 7 | |
| 8 | const SelectFieldDropdown = ({ |
| 9 | filterOptions, |
| 10 | filter, |
| 11 | urlKey, |
| 12 | }: { |
| 13 | filterOptions: AtLeastOne<FilterOption>; |
| 14 | filter: FilterData; |
| 15 | urlKey?: string; |
| 16 | }) => { |
| 17 | const updateFilter = useFilters({ urlKey }).updateFilter; |
| 18 | |
| 19 | const { field } = filter; |
| 20 | |
| 21 | const selectedOption = useMemo(() => { |
| 22 | return filterOptions.find((option) => option.field === field); |
| 23 | }, [field, filterOptions]); |
| 24 | |
| 25 | const updateFieldSelection = (option: FilterOption) => { |
| 26 | const optionTypeChanged = option.type !== selectedOption?.type; |
| 27 | let comparator = filter.comparator; |
| 28 | let value = filter.value; |
| 29 | if (optionTypeChanged) { |
| 30 | const newComparatorOptions = comparatorsForFilterType(option.type); |
| 31 | comparator = newComparatorOptions[0]; |
| 32 | value = ""; |
| 33 | } |
| 34 | |
| 35 | updateFilter({ ...filter, field: option.field, comparator, value }); |
| 36 | }; |
| 37 | |
| 38 | if (!selectedOption) { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | return ( |
| 43 | <InputDropdown |
| 44 | options={filterOptions} |
| 45 | getDisplayLabel={(option) => (!!option.label ? option.label : option.field)} |
| 46 | selectedOption={selectedOption} |
| 47 | onSelect={updateFieldSelection} |
| 48 | /> |
| 49 | ); |
| 50 | }; |
| 51 | |
| 52 | export default SelectFieldDropdown; |
nothing calls this directly
no test coverage detected