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 #4835

Closed
wants to merge 1 commit into from
Closed
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
70 changes: 63 additions & 7 deletions packages/react-table/__tests__/core/core.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ describe('core', () => {
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
header.column.columnDef.header,
header.getContext()
)}
</th>
))}
</tr>
Expand All @@ -160,10 +160,7 @@ describe('core', () => {
<th key={header.id} colSpan={header.colSpan}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.footer,
header.getContext()
)}
: flexRender(header.column.columnDef.footer, header.getContext())}
</th>
))}
</tr>
Expand Down Expand Up @@ -264,4 +261,63 @@ 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 table = useReactTable({
data: [
['Hello', 'World', 'Just', 'Testing'],
],
columns: [
{ header: 'Name', accessorKey: 0 },
{ header: 'Col1', accessorKey: 1 },
{ header: 'Col2', accessorKey: 2 },
{ header: 'Col3', accessorKey: 3 },
],
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(/name/i)).toBeInTheDocument()
expect(RTL.within(thead).getByText(/col1/i)).toBeInTheDocument()
expect(RTL.within(thead).getByText(/col2/i)).toBeInTheDocument()
expect(RTL.within(thead).getByText(/col3/i)).toBeInTheDocument()

const tbody = RTL.screen.getByTestId("tbody")
expect(RTL.within(tbody).getByText(/hello/i)).toBeInTheDocument()
expect(RTL.within(tbody).getByText(/world/i)).toBeInTheDocument()
expect(RTL.within(tbody).getByText(/just/i)).toBeInTheDocument()
expect(RTL.within(tbody).getByText(/testing/i)).toBeInTheDocument()
})
})
2 changes: 1 addition & 1 deletion packages/table-core/src/core/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ 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 @@ -283,7 +283,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