diff --git a/packages/table-core/src/features/global-filtering/globalFilteringFeature.ts b/packages/table-core/src/features/global-filtering/globalFilteringFeature.ts index ef4f630c02..335c63471f 100644 --- a/packages/table-core/src/features/global-filtering/globalFilteringFeature.ts +++ b/packages/table-core/src/features/global-filtering/globalFilteringFeature.ts @@ -28,10 +28,12 @@ export const globalFilteringFeature: TableFeature = { onGlobalFilterChange: makeStateUpdater('globalFilter', table), globalFilterFn: 'auto', getColumnCanGlobalFilter: (column) => { - const value = table + const row = table .getCoreRowModel() - .flatRows[0]?.getAllCellsByColumnId() - [column.id]?.getValue() + .flatRows.find( + (row) => row.getAllCellsByColumnId()[column.id]?.getValue() != null, + ) + const value = row?.getAllCellsByColumnId()[column.id]?.getValue() return typeof value === 'string' || typeof value === 'number' }, diff --git a/packages/table-core/tests/implementation/features/column-filtering/createFilteredRowModel.test.ts b/packages/table-core/tests/implementation/features/column-filtering/createFilteredRowModel.test.ts index 455a34f509..49a3a6fc1c 100644 --- a/packages/table-core/tests/implementation/features/column-filtering/createFilteredRowModel.test.ts +++ b/packages/table-core/tests/implementation/features/column-filtering/createFilteredRowModel.test.ts @@ -479,6 +479,26 @@ describe('createFilteredRowModel', () => { }) describe('global filtering edge cases', () => { + it('should filter a column when its first row value is undefined', () => { + type NullableNameRow = { name?: string } + const nullableColumns: Array< + ColumnDef + > = [{ accessorKey: 'name', id: 'name' }] + + const table = constructTable({ + features, + columns: nullableColumns, + data: [{ name: undefined }, { name: 'hello' }, { name: 'world' }], + initialState: { + globalFilter: 'hello', + }, + }) + + expect( + table.getFilteredRowModel().rows.map((row) => row.original.name), + ).toEqual(['hello']) + }) + it('should pass rows through when no columns are globally filterable', () => { const table = constructTable({ features,