Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: coerce accessor key to string during column creation #5808

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
63 changes: 63 additions & 0 deletions packages/react-table/tests/core/core.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@testing-library/jest-dom/vitest'
import * as React from 'react'
import { describe, expect, it } from 'vitest'
import { act, renderHook } from '@testing-library/react-hooks'
Expand Down Expand Up @@ -263,4 +264,66 @@ describe('core', () => {
expect(rowModel.flatRows.length).toEqual(3)
expect(rowModel.rowsById['2']?.original).toEqual(defaultData[2])
})

it('renders the table if accessorKey is an array index', () => {
const Table = () => {
const [data] = React.useState<[string, string, number][]>(() => [
['Tanner', 'Linsley', 29],
])
const [columns] = React.useState<ColumnDef<[string, string, number]>[]>(
() => [
{ header: 'First Name', accessorKey: 0 },
{ header: 'Last Name', accessorKey: 1 },
{ header: 'Age', accessorKey: 2 },
]
)
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
})

return (
<table>
<thead data-testid="thead">
<tr>
{table.getFlatHeaders().map(header => (
<th key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</th>
))}
</tr>
</thead>
<tbody data-testid="tbody">
{table.getRowModel().rows.map(row => (
<tr key={row.id}>
{row.getVisibleCells().map(cell => (
<td key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
)
}

RTL.render(<Table />)

const thead = RTL.screen.getByTestId('thead')
expect(RTL.within(thead).getByText(/first name/i)).toBeInTheDocument()
expect(RTL.within(thead).getByText(/last name/i)).toBeInTheDocument()
expect(RTL.within(thead).getByText(/age/i)).toBeInTheDocument()

const tbody = RTL.screen.getByTestId('tbody')
expect(RTL.within(tbody).getByText(/tanner/i)).toBeInTheDocument()
expect(RTL.within(tbody).getByText(/linsley/i)).toBeInTheDocument()
expect(RTL.within(tbody).getByText(29)).toBeInTheDocument()
})
})
5 changes: 4 additions & 1 deletion packages/table-core/src/core/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export function createColumn<TData extends RowData, TValue>(
...columnDef,
} as ColumnDefResolved<TData>

const accessorKey = resolvedColumnDef.accessorKey
const accessorKey =
typeof resolvedColumnDef.accessorKey !== 'undefined'
? String(resolvedColumnDef.accessorKey)
: undefined

let id =
resolvedColumnDef.id ??
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export type ColumnDefResolved<
TData extends RowData,
TValue = unknown,
> = Partial<UnionToIntersection<ColumnDef<TData, TValue>>> & {
accessorKey?: string
accessorKey?: string | number
}

export interface Column<TData extends RowData, TValue = unknown>
Expand Down