Skip to content
Open
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
7 changes: 7 additions & 0 deletions .changeset/use-table-resolved-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tanstack/react-table': patch
'@tanstack/preact-table': patch
'@tanstack/octane-table': patch
---

`table.options` on the table returned by `useTable` now includes feature defaults from `getDefaultTableOptions`, matching `row.table.options`. It used to be the raw options passed to the hook.
3 changes: 2 additions & 1 deletion packages/octane-table/src/useTable.tsrx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export function useTable<
return useMemo(
() => ({
...table,
options: tableOptions,
// resolved options: feature defaults merged under `tableOptions`
options: { ...table.options, ...tableOptions },
state,
}),
[table, tableOptions, state],
Expand Down
3 changes: 2 additions & 1 deletion packages/preact-table/src/useTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ export function useTable<
return useMemo(
() => ({
...table,
options: tableOptions,
// resolved options: feature defaults merged under `tableOptions`
options: { ...table.options, ...tableOptions },
state,
}),
[table, tableOptions, state],
Expand Down
34 changes: 34 additions & 0 deletions packages/preact-table/tests/unit/useTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { cleanup, fireEvent, render, screen } from '@testing-library/preact'
import { useState } from 'preact/hooks'
import { act } from 'preact/test-utils'
import {
columnResizingFeature,
columnSizingFeature,
createPaginatedRowModel,
rowPaginationFeature,
tableFeatures,
Expand Down Expand Up @@ -492,3 +494,35 @@ describe('useTable controlled state', () => {
expect(text('Rapid isolated page index')).toBe('3')
})
})

describe('useTable options', () => {
it('exposes feature default options on the returned table', () => {
const resizingFeatures = tableFeatures({
columnSizingFeature,
columnResizingFeature,
})
const resizingColumns: ReadonlyArray<
ColumnDef<typeof resizingFeatures, TestRow>
> = []
let latestTable: PreactTable<typeof resizingFeatures, TestRow> | undefined

function OptionsHarness({ mode }: { mode?: 'onChange' }) {
latestTable = useTable({
features: resizingFeatures,
columns: resizingColumns,
data,
...(mode ? { columnResizeMode: mode } : {}),
})

return null
}

const { rerender } = render(<OptionsHarness />)

expect(latestTable!.options.columnResizeMode).toBe('onEnd')

rerender(<OptionsHarness mode="onChange" />)

expect(latestTable!.options.columnResizeMode).toBe('onChange')
})
})
3 changes: 2 additions & 1 deletion packages/react-table/src/useTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ export function useTable<
return useMemo(
() => ({
...table,
options: tableOptions,
// resolved options: feature defaults merged under `tableOptions`
options: { ...table.options, ...tableOptions },
state,
}),
[table, tableOptions, state],
Expand Down
34 changes: 34 additions & 0 deletions packages/react-table/tests/useTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
render as testingLibraryRender,
} from '@testing-library/react'
import {
columnResizingFeature,
columnSizingFeature,
createPaginatedRowModel,
rowPaginationFeature,
tableFeatures,
Expand Down Expand Up @@ -882,3 +884,35 @@ describe('useTable state subscriptions', () => {
expect(errors).not.toContain('The result of getSnapshot should be cached')
})
})

describe('useTable options', () => {
it('exposes feature default options on the returned table', () => {
const resizingFeatures = tableFeatures({
columnSizingFeature,
columnResizingFeature,
})
const resizingColumns: ReadonlyArray<
ColumnDef<typeof resizingFeatures, TestRow>
> = []
let latestTable: ReactTable<typeof resizingFeatures, TestRow> | undefined

function OptionsHarness({ mode }: { mode?: 'onChange' }) {
latestTable = useTable({
features: resizingFeatures,
columns: resizingColumns,
data,
...(mode ? { columnResizeMode: mode } : {}),
})

return null
}

const { rerender } = render(<OptionsHarness />)

expect(latestTable!.options.columnResizeMode).toBe('onEnd')

rerender(<OptionsHarness mode="onChange" />)

expect(latestTable!.options.columnResizeMode).toBe('onChange')
})
})