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
5 changes: 5 additions & 0 deletions .changeset/fresh-facets-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/table-core': patch
---

Refresh custom faceted unique value getters when `getFacetedUniqueValues` changes.
10 changes: 8 additions & 2 deletions packages/table-core/src/features/ColumnFaceting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
10 changes: 8 additions & 2 deletions packages/table-core/src/features/GlobalFaceting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
51 changes: 51 additions & 0 deletions packages/table-core/tests/ColumnFaceting.test.ts
Original file line number Diff line number Diff line change
@@ -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<Person>[] = [{ 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<Person>({
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)
})
})