-
Notifications
You must be signed in to change notification settings - Fork 353
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 1 commit
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
}; | ||||||
|
||||||
type Props = Pick<DataEditorProps, "getCellContent" | "rows" | "columns"> & { | ||||||
filter?: ColumnFilter | ColumnFilter[]; | ||||||
}; | ||||||
type Result = Pick<DataEditorProps, "getCellContent" | "rows"> & { | ||||||
getOriginalIndex: (index: number) => number; | ||||||
}; | ||||||
|
||||||
export function useColumnFilter(p: Props): Result { | ||||||
const { filter, rows, getCellContent: getCellContentIn } = p; | ||||||
|
||||||
const filters = useMemo(() => { | ||||||
if (filter === undefined) return [] as ColumnFilter[]; | ||||||
return Array.isArray(filter) ? filter : [filter]; | ||||||
}, [filter]); | ||||||
|
||||||
const filterCols = useMemo(() => | ||||||
filters.map(s => { | ||||||
const c = p.columns.findIndex(col => s.column === col || (col.id !== undefined && s.column.id === col.id)); | ||||||
return c === -1 ? undefined : c; | ||||||
}), | ||||||
[filters, p.columns] | ||||||
); | ||||||
|
||||||
const filterMap = useMemo(() => { | ||||||
const activeFilters = filters | ||||||
.map((s, i) => ({ filter: s, col: filterCols[i] })) | ||||||
.filter((v): v is { filter: ColumnFilter; col: number } => v.col !== undefined); | ||||||
|
||||||
if (activeFilters.length === 0) return undefined; | ||||||
|
||||||
const values = activeFilters.map(() => new Array<GridCell>(rows)); | ||||||
for (let sIndex = 0; sIndex < activeFilters.length; sIndex++) { | ||||||
const { col } = activeFilters[sIndex]; | ||||||
const index: [number, number] = [col, 0]; | ||||||
for (let i = 0; i < rows; i++) { | ||||||
index[1] = i; | ||||||
values[sIndex][i] = getCellContentIn(index); | ||||||
} | ||||||
} | ||||||
|
||||||
return range(rows).filter((r) => { | ||||||
for (let sIndex = 0; sIndex < activeFilters.length; sIndex++) { | ||||||
const { filter: colFilter } = activeFilters[sIndex]; | ||||||
const v = values[sIndex][r]; | ||||||
if (!colFilter.filterCallback(v)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The filter callback receives the entire GridCell object
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
return false; | ||||||
} | ||||||
} | ||||||
return true; | ||||||
}) | ||||||
}, [getCellContentIn, rows, filters, filterCols]); | ||||||
|
||||||
const getOriginalIndex = useCallback( | ||||||
(index: number): number => { | ||||||
if (filterMap === undefined) return index; | ||||||
return filterMap[index]; | ||||||
}, | ||||||
[filterMap] | ||||||
); | ||||||
|
||||||
const getCellContent = useCallback<typeof getCellContentIn>( | ||||||
([col, row]) => { | ||||||
if (filterMap === undefined) return getCellContentIn([col, row]); | ||||||
row = filterMap[row]; | ||||||
return getCellContentIn([col, row]); | ||||||
}, | ||||||
[getCellContentIn, filterMap] | ||||||
); | ||||||
|
||||||
if (filterMap === undefined) { | ||||||
return { getCellContent: p.getCellContent, getOriginalIndex, rows: p.rows }; | ||||||
} | ||||||
|
||||||
return { | ||||||
getOriginalIndex, | ||||||
getCellContent, | ||||||
rows: filterMap.length, | ||||||
}; | ||||||
} |
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" }, | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||
], | ||||||||||||
}) | ||||||||||||
); | ||||||||||||
|
||||||||||||
const order = Array.from({ length: data.length }, (_, i) => result.current.getOriginalIndex(i)); | ||||||||||||
expect(order).toEqual([3, 4, undefined, undefined, undefined]); | ||||||||||||
}); | ||||||||||||
}); |
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 > value
will be comparing a number to a GridCell object. Should bereturn filterValue > Number(value.data)
.Copilot uses AI. Check for mistakes.