Skip to content

Commit

Permalink
fix(structure): fixing incorrect way of setting col visibility (#6716)
Browse files Browse the repository at this point in the history
* fix(structure): fixing incorrect way of setting col visibility

* feat(structure): fixing for PR comments
  • Loading branch information
jordanl17 authored May 21, 2024
1 parent 93c7d66 commit 06f7902
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {describe, expect, it, jest} from '@jest/globals'
import {type ObjectSchemaType} from '@sanity/types'
import {renderHook} from '@testing-library/react'

import {useDocumentSheetColumns} from '../useDocumentSheetColumns'
Expand Down Expand Up @@ -33,18 +34,17 @@ describe('useDocumentSheetColumns', () => {
{name: 'phone number', type: {name: 'number'}},
{name: 'has pet', type: {name: 'boolean'}},
],
}
} as unknown as ObjectSchemaType

const {result} = renderHook(() => useDocumentSheetColumns(mockSchemaType))
expect(result.current.initialColumnsVisibility).toEqual({
'Preview': true,
'selected': true,
'name': true,
'nickname': true,
'email': true,
'age': true,
'address.street': true,
'address.country': false,
'address_street': true,
'address_country': false,
'phone number': false,
'has pet': false,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ const getColsFromSchemaType = (schemaType: ObjectSchemaType, parentalField?: str
const {type, name} = field
if (SUPPORTED_FIELDS.includes(type.name)) {
const nextCol = columnHelper.accessor(
// accessor must use dot notation for internal tanstack method of reading cell data
parentalField ? `${parentalField}.${field.name}` : field.name,
{
id: parentalField ? `${parentalField}_${field.name}` : field.name,
header: field.type.title,
enableHiding: true,
cell: (info) => <SheetListCell {...info} fieldType={type} />,
Expand Down Expand Up @@ -120,7 +122,8 @@ export function useDocumentSheetColumns(documentSchemaType?: ObjectSchemaType) {
return []
}
return [
columnHelper.accessor('selected', {
columnHelper.display({
id: 'selected',
enableHiding: false,
header: (info) => (
<Checkbox
Expand All @@ -138,6 +141,7 @@ export function useDocumentSheetColumns(documentSchemaType?: ObjectSchemaType) {
}),
columnHelper.accessor('Preview', {
enableHiding: false,
id: 'Preview',
cell: (info) => {
return (
<PreviewCell
Expand All @@ -156,17 +160,20 @@ export function useDocumentSheetColumns(documentSchemaType?: ObjectSchemaType) {
() =>
flatColumns(columns).reduce<[VisibilityState, number]>(
([accCols, countAllowedVisible], column) => {
if (!column.id) throw new Error('Column must have an id')
const visibilityKey = column.id

// this column is always visible
if (!column.enableHiding) {
return [{...accCols, [column.accessorKey]: true}, countAllowedVisible]
return [{...accCols, [visibilityKey]: true}, countAllowedVisible]
}

// have already reached column visibility limit, hide column by default
if (countAllowedVisible === VISIBLE_COLUMN_LIMIT) {
return [{...accCols, [column.accessorKey]: false}, countAllowedVisible]
return [{...accCols, [visibilityKey]: false}, countAllowedVisible]
}

return [{...accCols, [column.accessorKey]: true}, countAllowedVisible + 1]
return [{...accCols, [visibilityKey]: true}, countAllowedVisible + 1]
},
[{}, 0],
),
Expand Down

0 comments on commit 06f7902

Please sign in to comment.