From 26847e723b945517a7afa1a1a6967b3da401b802 Mon Sep 17 00:00:00 2001 From: wanxiankai Date: Tue, 14 Jul 2026 08:30:36 +0800 Subject: [PATCH] fix(table-core): refresh faceted values on option updates --- .changeset/fresh-facets-update.md | 5 ++ .../table-core/src/features/ColumnFaceting.ts | 10 +++- .../table-core/src/features/GlobalFaceting.ts | 10 +++- .../table-core/tests/ColumnFaceting.test.ts | 51 +++++++++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 .changeset/fresh-facets-update.md create mode 100644 packages/table-core/tests/ColumnFaceting.test.ts diff --git a/.changeset/fresh-facets-update.md b/.changeset/fresh-facets-update.md new file mode 100644 index 0000000000..9040715309 --- /dev/null +++ b/.changeset/fresh-facets-update.md @@ -0,0 +1,5 @@ +--- +'@tanstack/table-core': patch +--- + +Refresh custom faceted unique value getters when `getFacetedUniqueValues` changes. diff --git a/packages/table-core/src/features/ColumnFaceting.ts b/packages/table-core/src/features/ColumnFaceting.ts index f356252deb..4f22421b0a 100644 --- a/packages/table-core/src/features/ColumnFaceting.ts +++ b/packages/table-core/src/features/ColumnFaceting.ts @@ -60,10 +60,16 @@ export const ColumnFaceting: TableFeature = { return column._getFacetedRowModel() } + let getFacetedUniqueValues = table.options.getFacetedUniqueValues column._getFacetedUniqueValues = - table.options.getFacetedUniqueValues && - table.options.getFacetedUniqueValues(table, column.id) + getFacetedUniqueValues && getFacetedUniqueValues(table, column.id) column.getFacetedUniqueValues = () => { + if (getFacetedUniqueValues !== table.options.getFacetedUniqueValues) { + getFacetedUniqueValues = table.options.getFacetedUniqueValues + column._getFacetedUniqueValues = + getFacetedUniqueValues && getFacetedUniqueValues(table, column.id) + } + if (!column._getFacetedUniqueValues) { return new Map() } diff --git a/packages/table-core/src/features/GlobalFaceting.ts b/packages/table-core/src/features/GlobalFaceting.ts index 020b410f9f..04169ec7ee 100644 --- a/packages/table-core/src/features/GlobalFaceting.ts +++ b/packages/table-core/src/features/GlobalFaceting.ts @@ -41,10 +41,16 @@ export const GlobalFaceting: TableFeature = { return table._getGlobalFacetedRowModel() } + let getFacetedUniqueValues = table.options.getFacetedUniqueValues table._getGlobalFacetedUniqueValues = - table.options.getFacetedUniqueValues && - table.options.getFacetedUniqueValues(table, '__global__') + getFacetedUniqueValues && getFacetedUniqueValues(table, '__global__') table.getGlobalFacetedUniqueValues = () => { + if (getFacetedUniqueValues !== table.options.getFacetedUniqueValues) { + getFacetedUniqueValues = table.options.getFacetedUniqueValues + table._getGlobalFacetedUniqueValues = + getFacetedUniqueValues && getFacetedUniqueValues(table, '__global__') + } + if (!table._getGlobalFacetedUniqueValues) { return new Map() } diff --git a/packages/table-core/tests/ColumnFaceting.test.ts b/packages/table-core/tests/ColumnFaceting.test.ts new file mode 100644 index 0000000000..23d7e500fa --- /dev/null +++ b/packages/table-core/tests/ColumnFaceting.test.ts @@ -0,0 +1,51 @@ +import { describe, expect, it, vi } from 'vitest' +import { ColumnDef, createTable, getCoreRowModel } from '../src' + +type Person = { + name: string +} + +const data: Person[] = [{ name: 'Alice' }] +const columns: ColumnDef[] = [{ accessorKey: 'name' }] + +function createFacetedValuesFactory(value: string) { + return vi.fn(() => () => new Map([[value, 1]])) +} + +describe('ColumnFaceting', () => { + it('updates custom faceted unique values when the option changes', () => { + const initialFactory = createFacetedValuesFactory('initial') + const updatedFactory = createFacetedValuesFactory('updated') + const table = createTable({ + columns, + data, + getCoreRowModel: getCoreRowModel(), + getFacetedUniqueValues: initialFactory, + onStateChange() {}, + renderFallbackValue: null, + state: {}, + }) + const column = table.getColumn('name')! + + expect(column.getFacetedUniqueValues()).toEqual(new Map([['initial', 1]])) + expect(table.getGlobalFacetedUniqueValues()).toEqual( + new Map([['initial', 1]]), + ) + + table.setOptions((options) => ({ + ...options, + getFacetedUniqueValues: updatedFactory, + })) + + expect(column.getFacetedUniqueValues()).toEqual(new Map([['updated', 1]])) + expect(table.getGlobalFacetedUniqueValues()).toEqual( + new Map([['updated', 1]]), + ) + expect(column.getFacetedUniqueValues()).toEqual(new Map([['updated', 1]])) + expect(table.getGlobalFacetedUniqueValues()).toEqual( + new Map([['updated', 1]]), + ) + expect(initialFactory).toHaveBeenCalledTimes(2) + expect(updatedFactory).toHaveBeenCalledTimes(2) + }) +})