Skip to content

Commit

Permalink
fix: Apply filter on autocomplete options
Browse files Browse the repository at this point in the history
  • Loading branch information
tklein1801 committed May 16, 2024
1 parent 4e16086 commit bc12e06
Showing 1 changed file with 2 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
TextField,
type TextFieldProps,
Typography,
createFilterOptions,
} from '@mui/material';

import {StyledAutocompleteOption} from '@/components/Base';
Expand All @@ -31,16 +30,6 @@ export interface IStockExchangeAutocompleteProps {
textFieldProps?: TextFieldProps;
}

/**
* The filter function used for creating options in the stock exchange autocomplete component.
* @typeParam T - The type of the options.
* @param option - The option to filter.
* @returns Whether the option should be included in the filtered list.
*/
const filter = createFilterOptions<TStockExchangeAutocompleteOption>({
stringify: option => option.label + option.ticker,
});

/**
* Applies a filter to the stock exchange options based on the provided state.
*
Expand All @@ -52,13 +41,11 @@ export function applyStockExchangeOptionsFilter(
options: TStockExchangeAutocompleteOption[],
state: FilterOptionsState<TStockExchangeAutocompleteOption>,
): TStockExchangeAutocompleteOption[] {
const filtered = filter(options, state);
const matches = filtered.filter(
return options.filter(
option =>
option.label.toLowerCase().includes(state.inputValue.toLowerCase()) ||
option.ticker.toLowerCase().includes(state.inputValue.toLowerCase()),
);
return matches;
}

export const StockExchangeAutocomplete: React.FC<IStockExchangeAutocompleteProps> = ({
Expand All @@ -77,6 +64,7 @@ export const StockExchangeAutocomplete: React.FC<IStockExchangeAutocompleteProps
return option.label;
}}
value={value}
filterOptions={applyStockExchangeOptionsFilter}
onChange={onChange}
isOptionEqualToValue={(option, value) => option.value === value.value}
defaultValue={defaultValue}
Expand Down

0 comments on commit bc12e06

Please sign in to comment.