Skip to content

Commit

Permalink
Cleanup the single select
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 committed Feb 15, 2024
1 parent 44777f2 commit 5c981d2
Showing 1 changed file with 5 additions and 37 deletions.
42 changes: 5 additions & 37 deletions client/src/app/components/FilterToolbar/SelectFilterControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,18 @@ export const SelectFilterControl = <TItem, TFilterCategoryKey extends string>({
>): JSX.Element | null => {
const [isFilterDropdownOpen, setIsFilterDropdownOpen] = React.useState(false);

const getChipFromOptionValue = (
optionValue: SelectOptionProps | undefined
) => {
return optionValue ? optionValue.value : "";
};

const getOptionKeyFromChip = (chip: string) =>
category.selectOptions.find(
(optionProps) => optionProps.value.toString() === chip
)?.key;

const getOptionValueFromOptionKey = (
optionKey: string
): SelectOptionProps => {
return (
category.selectOptions.find((optionProps) => {
return optionProps.value === optionKey;
}) || { value: "", children: "", key: "" }
);
};

const onFilterSelect = (value: string) => {
setFilterValue(value ? [value] : null);
setIsFilterDropdownOpen(false);
};

const chips = filterValue ? filterValue : [];

const onFilterClear = (chip: string) => {
const optionKey = getOptionKeyFromChip(chip);
const newValue = filterValue
? filterValue.filter((val) => val !== optionKey)
: [];
setFilterValue(newValue.length > 0 ? newValue : null);
const newValue = filterValue?.filter((val) => val !== chip);
setFilterValue(newValue?.length ? newValue : null);
};

const selections: SelectOptionProps[] = filterValue
? filterValue.map(getOptionValueFromOptionKey)
: [];

const chips = selections ? selections.map(getChipFromOptionValue) : [];

const toggle = (toggleRef: React.Ref<MenuToggleElement>) => {
return (
<MenuToggle
Expand Down Expand Up @@ -116,11 +88,7 @@ export const SelectFilterControl = <TItem, TFilterCategoryKey extends string>({
<SelectList>
{category.selectOptions.map((o: SelectOptionProps, index) => {
return (
<SelectOption
key={`${index}-${o.value}`}
isSelected={o.value == filterValue}
{...o}
>
<SelectOption {...o} key={`${index}-${o.value}`}>
{o.value}
</SelectOption>
);
Expand Down

0 comments on commit 5c981d2

Please sign in to comment.