Skip to content

Commit

Permalink
fix(filter): support filters on number fields (#2308)
Browse files Browse the repository at this point in the history
fixes #1913
  • Loading branch information
sleidig authored Mar 20, 2024
1 parent c51c818 commit 9303825
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/app/core/filter/filters/filters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,22 @@ describe("Filters", () => {
filter.selectedOptionValues = ["true", "false"];
testFilter(filter, [recordFalse, recordTrue], [recordFalse, recordTrue]);
});

it("should support numbers as options", () => {
const filter = new SelectableFilter("counts", [], "Counts");

const keys = [1, 4, 7];
filter.options = SelectableFilter.generateOptions(keys, "category");

expect(filter.options).toHaveSize(keys.length);

filter.selectedOptionValues = ["1"];

const testData = [
{ id: 1, category: 1 },
{ id: 2, category: 4 },
];
const filteredData = testFilter(filter, testData, [testData[0]]);
expect(filteredData[0].category).toBe(1);
});
});
4 changes: 2 additions & 2 deletions src/app/core/filter/filters/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ export class SelectableFilter<T extends Entity> extends Filter<T> {
* @param attributeName The name of the property of a data item that is compared to the value in the filter function.
*/
public static generateOptions<T extends Entity>(
valuesToMatchAsOptions: string[],
valuesToMatchAsOptions: (string | number)[],
attributeName: string,
): FilterSelectionOption<T>[] {
return valuesToMatchAsOptions
.filter((k) => !!k)
.map((k) => ({
key: k.toLowerCase(),
key: k.toString().toLowerCase(),
label: k.toString(),
filter: { [attributeName]: k } as DataFilter<T>,
}));
Expand Down

0 comments on commit 9303825

Please sign in to comment.