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 @@ -32,7 +32,9 @@ export function getDefaultColumnVisibilityState(): ColumnVisibilityState {
* Updates this column's visibility when hiding is allowed.
*
* Passing `visible` stores that value. Omitting it flips the column's current
* visibility state. Columns that cannot hide are start unchanged.
* visibility state. Group columns update their hideable leaf columns because
* visibility state is keyed by leaf column ids. Columns that cannot hide stay
* unchanged.
*
* @example
* ```ts
Expand All @@ -47,9 +49,18 @@ export function column_toggleVisibility<
if (column_getCanHide(column)) {
table_setColumnVisibility(column.table, (old) => {
const next = Object.assign(makeObjectMap<boolean>(), old)
next[column.id] =
const nextVisible =
visible ??
!callMemoOrStaticFn(column, 'getIsVisible', column_getIsVisible)

const leafColumns = column.getLeafColumns()
for (let i = 0; i < leafColumns.length; i++) {
const leafColumn = leafColumns[i]!
if (column_getCanHide(leafColumn)) {
next[leafColumn.id] = nextVisible
}
}

return next
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,61 @@ describe('columnVisibilityFeature.utils', () => {
expect(result).toEqual({ firstName: true })
})

it('should toggle hideable leaf columns for a group column', () => {
const onColumnVisibilityChange = vi.fn()
const columns: Array<ColumnDef<typeof features, Person, any>> = [
{
id: 'name',
columns: [
{ accessorKey: 'firstName', id: 'firstName' },
{
accessorKey: 'lastName',
id: 'lastName',
enableHiding: false,
},
],
},
]
const table = constructTable({
features,
data: generateTestData(1),
columns,
onColumnVisibilityChange,
})
const groupColumn = table.getColumn('name')!

column_toggleVisibility(groupColumn, false)

const result = getUpdaterResult(onColumnVisibilityChange, {})
expect(result).toEqual({ firstName: false })
expect(result).not.toHaveProperty('name')
})

it('should infer group visibility toggles from its leaf columns', () => {
const onColumnVisibilityChange = vi.fn()
const columns: Array<ColumnDef<typeof features, Person, any>> = [
{
id: 'name',
columns: [
{ accessorKey: 'firstName', id: 'firstName' },
{ accessorKey: 'lastName', id: 'lastName' },
],
},
]
const table = constructTable({
features,
data: generateTestData(1),
columns,
onColumnVisibilityChange,
})
const groupColumn = table.getColumn('name')!

column_toggleVisibility(groupColumn)

const result = getUpdaterResult(onColumnVisibilityChange, {})
expect(result).toEqual({ firstName: false, lastName: false })
})

it('should not toggle when column cannot be hidden', () => {
const onColumnVisibilityChange = vi.fn()
const table = makeTable(1, {
Expand Down
Loading