Skip to content
Merged
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
8 changes: 5 additions & 3 deletions packages/table-core/src/core/columns/constructColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ export function constructColumn<
} as ColumnDefResolved<{}, TData, TValue>

const accessorKey = resolvedColumnDef.accessorKey
const accessorKeyString =
accessorKey === undefined ? undefined : String(accessorKey)

const id =
resolvedColumnDef.id ??
(accessorKey ? accessorKey.replaceAll('.', '_') : undefined) ??
accessorKeyString?.replaceAll('.', '_') ??
(typeof resolvedColumnDef.header === 'string'
? resolvedColumnDef.header
: undefined)
Expand All @@ -63,9 +65,9 @@ export function constructColumn<

if (resolvedColumnDef.accessorFn) {
accessorFn = resolvedColumnDef.accessorFn
} else if (accessorKey) {
} else if (accessorKey !== undefined) {
// Support deep accessor keys
if (accessorKey.includes('.')) {
if (typeof accessorKey === 'string' && accessorKey.includes('.')) {
const keys = accessorKey.split('.')
accessorFn = (originalRow: TData) => {
let result = originalRow as Record<string, any> | undefined
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/types/ColumnDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,5 @@ export type ColumnDefResolved<
TData extends RowData,
TValue extends CellData = CellData,
> = Partial<UnionToIntersection<ColumnDef<TFeatures, TData, TValue>>> & {
accessorKey?: string
accessorKey?: (string & {}) | keyof TData
}
2 changes: 1 addition & 1 deletion packages/table-core/src/types/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export type DeepValue<T, TProp> =
T extends Record<string | number, any>
? TProp extends `${infer TBranch}.${infer TDeepProp}`
? DeepValue<T[TBranch], TDeepProp>
: T[TProp & string]
: T[TProp & keyof T]
: never

export type NoInfer<T> = [T][T extends any ? 0 : never]
Expand Down
6 changes: 3 additions & 3 deletions packages/table-core/src/worker/initTableWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ export function initTableWorker<
.map(
(def) =>
def.id ??
(typeof def.accessorKey === 'string'
? def.accessorKey.replaceAll('.', '_')
: undefined),
(def.accessorKey === undefined
? undefined
: String(def.accessorKey).replaceAll('.', '_')),
)
.filter((id): id is string => id != null)
} else {
Expand Down
27 changes: 26 additions & 1 deletion packages/table-core/tests/unit/helpers/columnHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, expectTypeOf, it } from 'vitest'
import { constructTable, createColumnHelper } from '../../../src'
import { testFeatures } from '../../fixtures/features'

Expand Down Expand Up @@ -74,4 +74,29 @@ describe('createColumnHelper', () => {
expect(row.getValue('shouty')).toBe('LINSLEY')
expect(row.getValue('actions')).toBeUndefined()
})

it('should resolve numeric accessor keys for tuple rows', () => {
type TupleRow = [string, number]
const tupleHelper = createColumnHelper<typeof features, TupleRow>()
const firstColumn = tupleHelper.accessor(0, {
cell: (info) => {
expectTypeOf(info.getValue()).toEqualTypeOf<string>()
return info.getValue()
},
})
const secondColumn = tupleHelper.accessor(1, {})
const table = constructTable<typeof features, TupleRow>({
features,
columns: tupleHelper.columns([firstColumn, secondColumn]),
data: [['Alice', 42]],
})
const row = table.getRowModel().rows[0]!

expect(table.getAllLeafColumns().map((column) => column.id)).toEqual([
'0',
'1',
])
expect(row.getValue('0')).toBe('Alice')
expect(row.getValue('1')).toBe(42)
})
})
Loading