-
Notifications
You must be signed in to change notification settings - Fork 367
Introduce useColumnFilter #1074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export { useCollapsingGroups } from "./use-collapsing-groups.js"; | ||
| export { useMoveableColumns } from "./use-movable-columns.js"; | ||
| export { useColumnSort } from "./use-column-sort.js"; | ||
| export { useColumnFilter } from "./use-column-filter.js"; | ||
| export { useAsyncDataSource } from "./use-async-data-source.js"; | ||
| export { useUndoRedo } from "./use-undo-redo.js"; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||
| import { type DataEditorProps, type GridCell, type GridColumn } from "@glideapps/glide-data-grid"; | ||||||
| import range from "lodash/range.js"; | ||||||
| import { useMemo, useCallback } from "react"; | ||||||
|
|
||||||
| export type ColumnFilter = { | ||||||
| column: GridColumn; | ||||||
| filterCallback: (cellValue: any) => boolean; | ||||||
|
||||||
| filterCallback: (cellValue: any) => boolean; | |
| filterCallback: (cellValue: GridCell) => boolean; |
Copilot
AI
Aug 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filter callback receives the entire GridCell object v, but the type definition suggests it should receive cellValue: any. This inconsistency could cause runtime errors if consumers expect just the cell data rather than the full cell object.
| if (!colFilter.filterCallback(v)) { | |
| if (!colFilter.filterCallback(v.data)) { |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||
| import { useColumnFilter } from "../src/use-column-filter.js"; | ||||||||||||
| import { renderHook } from "@testing-library/react-hooks"; | ||||||||||||
| import { GridCellKind, type GridCell } from "@glideapps/glide-data-grid"; | ||||||||||||
| import { expect, describe, test } from "vitest"; | ||||||||||||
|
|
||||||||||||
| describe("use-column-filter", () => { | ||||||||||||
| test("multi column filter", () => { | ||||||||||||
| const columns = [ | ||||||||||||
| { title: "A", id: "A" }, | ||||||||||||
| { title: "B", id: "B" }, | ||||||||||||
| ]; | ||||||||||||
| const data = [ | ||||||||||||
| ["2", "a"], | ||||||||||||
| ["1", "b"], | ||||||||||||
| ["2", "b"], | ||||||||||||
| ["1", "a"], | ||||||||||||
| ["3", "c"], | ||||||||||||
| ]; | ||||||||||||
|
|
||||||||||||
| const getCellContent = ([col, row]: readonly [number, number]): GridCell => ({ | ||||||||||||
| kind: GridCellKind.Text, | ||||||||||||
| allowOverlay: false, | ||||||||||||
| data: data[row][col], | ||||||||||||
| displayData: data[row][col], | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| const { result } = renderHook(() => | ||||||||||||
| useColumnFilter({ | ||||||||||||
| columns, | ||||||||||||
| rows: data.length, | ||||||||||||
| getCellContent, | ||||||||||||
| filter: [ | ||||||||||||
| { column: columns[0], filterCallback: (v) => v !== "2" }, | ||||||||||||
| { column: columns[1], filterCallback: (v) => v !== "b" }, | ||||||||||||
|
||||||||||||
| { column: columns[1], filterCallback: (v) => v !== "b" }, | |
| { column: columns[0], filterCallback: (v) => v.data !== "2" }, | |
| { column: columns[1], filterCallback: (v) => v.data !== "b" }, |
Copilot
AI
Aug 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test filter callback expects a string but will receive a GridCell object. This should be (v) => v.data !== "b" to access the cell's data property.
| { column: columns[1], filterCallback: (v) => v !== "b" }, | |
| { column: columns[1], filterCallback: (v) => v.data !== "b" }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filter callback expects a GridCell object but treats the parameter as a number. This will cause a runtime error since
filterValue > valuewill be comparing a number to a GridCell object. Should bereturn filterValue > Number(value.data).