Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export const globalFilteringFeature: TableFeature = {
onGlobalFilterChange: makeStateUpdater('globalFilter', table),
globalFilterFn: 'auto',
getColumnCanGlobalFilter: (column) => {
if (
'enableGlobalFilter' in column.columnDef &&
column.columnDef.enableGlobalFilter === true
) {
return true
}

const value = table
.getCoreRowModel()
.flatRows[0]?.getAllCellsByColumnId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,34 @@ describe('createFilteredRowModel', () => {
})

describe('global filtering edge cases', () => {
it('should globally filter an object-valued column that explicitly opts in', () => {
interface ObjectValueRow {
value: { label: string }
}

const table = constructTable<typeof features, ObjectValueRow>({
features,
columns: [
{
accessorKey: 'value',
id: 'value',
enableGlobalFilter: true,
},
],
data: [{ value: { label: 'keep' } }, { value: { label: 'drop' } }],
globalFilterFn: (row, _columnId, filterValue) =>
row.original.value.label.includes(String(filterValue)),
initialState: {
globalFilter: 'keep',
},
})

expect(table.getFilteredRowModel().rows).toHaveLength(1)
expect(table.getFilteredRowModel().rows[0]!.original.value.label).toBe(
'keep',
)
})

it('should pass rows through when no columns are globally filterable', () => {
const table = constructTable<typeof features, TestRow>({
features,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ describe('column_getCanGlobalFilter', () => {
expect(column_getCanGlobalFilter(table.getColumn('active')!)).toBe(false)
})

it('should allow non-string, non-number values to explicitly opt in', () => {
const filterColumns: Array<ColumnDef<typeof features, Sample, any>> = [
{ accessorKey: 'active', id: 'active', enableGlobalFilter: true },
]
const table = constructTable({ features, data, columns: filterColumns })

expect(column_getCanGlobalFilter(table.getColumn('active')!)).toBe(true)
})

it('should return false for display columns without an accessor', () => {
const table = makeTable()

Expand Down Expand Up @@ -106,6 +115,20 @@ describe('column_getCanGlobalFilter', () => {
expect(column_getCanGlobalFilter(table.getColumn('firstName')!)).toBe(false)
expect(column_getCanGlobalFilter(table.getColumn('age')!)).toBe(true)
})

it('should let a custom predicate reject an explicit column opt-in', () => {
const filterColumns: Array<ColumnDef<typeof features, Sample, any>> = [
{ accessorKey: 'active', id: 'active', enableGlobalFilter: true },
]
const table = constructTable({
features,
data,
columns: filterColumns,
getColumnCanGlobalFilter: () => false,
})

expect(column_getCanGlobalFilter(table.getColumn('active')!)).toBe(false)
})
})

describe('table_getGlobalAutoFilterFn', () => {
Expand Down
Loading