Skip to content

Commit 2aa4317

Browse files
author
Illia Obukhau
committed
fix(datagrid-dropdown-filter-web): compare options by value to fix issue with option checked state
1 parent 6a0f820 commit 2aa4317

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

packages/pluggableWidgets/datagrid-dropdown-filter-web/src/components/FilterComponent.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ export function FilterComponent(props: FilterComponentProps): ReactElement {
181181
options.map((option, index) => (
182182
<li
183183
className={classNames({
184-
"filter-selected": !multiSelect && selectedFilters.includes(option)
184+
"filter-selected": !multiSelect && isSelected(selectedFilters, option)
185185
})}
186-
key={index}
186+
key={`val:${option.value}`}
187187
onClick={e => {
188188
e.preventDefault();
189189
e.stopPropagation();
@@ -212,7 +212,7 @@ export function FilterComponent(props: FilterComponentProps): ReactElement {
212212
<input
213213
id={`${id}_checkbox_toggle_${index}`}
214214
type="checkbox"
215-
checked={selectedFilters.includes(option)}
215+
checked={isSelected(selectedFilters, option)}
216216
onChange={PreventReactErrorsAboutReadOnly}
217217
/>
218218
<label htmlFor={`${id}_checkbox_toggle_${index}`} style={{ pointerEvents: "none" }}>
@@ -301,9 +301,13 @@ export function FilterComponent(props: FilterComponentProps): ReactElement {
301301
);
302302
}
303303

304+
function hasSameValue(a: FilterOption): (b: FilterOption) => boolean {
305+
return b => a.value === b.value;
306+
}
307+
304308
function toggleFilter(filters: FilterOption[], filterToToggle: FilterOption): FilterOption[] {
305309
const alteredFilters = [...filters];
306-
const index = filters.indexOf(filterToToggle);
310+
const index = filters.findIndex(hasSameValue(filterToToggle));
307311
if (index > -1) {
308312
alteredFilters.splice(index, 1);
309313
} else {
@@ -312,3 +316,7 @@ function toggleFilter(filters: FilterOption[], filterToToggle: FilterOption): Fi
312316

313317
return alteredFilters;
314318
}
319+
320+
function isSelected(selected: FilterOption[], option: FilterOption): boolean {
321+
return !!selected.find(hasSameValue(option));
322+
}

0 commit comments

Comments
 (0)