I have made a multi select dropdown which is implemented like below
MyFilter{
key: string; // uniquely identifies selected filter among many filters on page
order: number; // display order of filter on page
options: DropListOption[]; // options in a dropdown
values: DropListOption[]; // selected values from dropdown
}
DropListOption{
label: string; // name of option
value: any; //value of option
}
I have implemented a method which is called when a checkbox is selected or unselected as below:
onSelect(key: string, option: DropListOption): void{
const filtersOpt = this.filters; // type of filter is MyFilter
filtersOpt.map((filter) => {
if(filter.key === key){
const selected: boolean = !!filter.values.find((opt) => opt.value === option.value);
filters.values = selected // filters.values used as property binding to display number of selected values
? filters.values.filter((opt) => opt.value !== option.value)
: [...filters.values, option];
}
return filter;
});
this.updateFilters.emit(filtersOpt); //emitting to parent component
}
Although my logic is working fine but being new to Angular/RxJS (front-end development), I am not very sure if my code is good code. Please can you let me know our thoughts or correct me. I would appreciate if someone can tell me any better way of implementing my onSelect() method.