Skip to content

Commit cbef10a

Browse files
committed
fix global filtering with undefined first values
1 parent 209699f commit cbef10a

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

packages/table-core/src/features/global-filtering/globalFilteringFeature.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ export const globalFilteringFeature: TableFeature = {
2828
onGlobalFilterChange: makeStateUpdater('globalFilter', table),
2929
globalFilterFn: 'auto',
3030
getColumnCanGlobalFilter: (column) => {
31-
const value = table
31+
const row = table
3232
.getCoreRowModel()
33-
.flatRows[0]?.getAllCellsByColumnId()
34-
[column.id]?.getValue()
33+
.flatRows.find(
34+
(row) => row.getAllCellsByColumnId()[column.id]?.getValue() != null,
35+
)
36+
const value = row?.getAllCellsByColumnId()[column.id]?.getValue()
3537

3638
return typeof value === 'string' || typeof value === 'number'
3739
},

packages/table-core/tests/implementation/features/column-filtering/createFilteredRowModel.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,26 @@ describe('createFilteredRowModel', () => {
479479
})
480480

481481
describe('global filtering edge cases', () => {
482+
it('should filter a column when its first row value is undefined', () => {
483+
type NullableNameRow = { name?: string }
484+
const nullableColumns: Array<
485+
ColumnDef<typeof features, NullableNameRow, any>
486+
> = [{ accessorKey: 'name', id: 'name' }]
487+
488+
const table = constructTable<typeof features, NullableNameRow>({
489+
features,
490+
columns: nullableColumns,
491+
data: [{ name: undefined }, { name: 'hello' }, { name: 'world' }],
492+
initialState: {
493+
globalFilter: 'hello',
494+
},
495+
})
496+
497+
expect(
498+
table.getFilteredRowModel().rows.map((row) => row.original.name),
499+
).toEqual(['hello'])
500+
})
501+
482502
it('should pass rows through when no columns are globally filterable', () => {
483503
const table = constructTable<typeof features, TestRow>({
484504
features,

0 commit comments

Comments
 (0)