fix: coerce accessor key to string during column creation
This is based on the PR by @jpedroh (https://github.com/TanStack/table/pull/4835)
During render, if the accessorKey was not a string, e.g. a number, it would result in an error when trying to normalize it during column creation, because it was trying to access methods which are not available in a number (replace and include). This PR forces the accessor key to be constructed as a string during column creation, thus removing this bug.
fixes https://github.com/TanStack/table/issues/4815
Summary by CodeRabbit
Release Notes
New Features
- Columns now accept numeric array indices as accessor keys. Tables can be configured using integer indices (0, 1, 2, etc.) alongside traditional string-based keys, providing enhanced flexibility when working with array-based data structures.
โ ๏ธ No Changeset found
Latest commit: 329cbce1f4250a717a54144ed28630b252b7c771
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Walkthrough
This PR enables support for numeric array indices as accessorKey values in column definitions. The changes update the type signature to accept number in addition to string, implement normalization logic to coerce numeric keys to strings internally, and add test coverage validating the functionality.
Changes
| Cohort / File(s) | Change Summary |
|---|---|
Type definition update packages/table-core/src/types.ts |
Updates ColumnDefResolved.accessorKey property from string to string | number to allow numeric array indices |
Accessor key normalization packages/table-core/src/core/column.ts |
Implements conditional coercion of resolvedColumnDef.accessorKey to string when defined, enabling numeric keys to work with downstream accessor logic |
Test coverage packages/react-table/tests/core/core.test.tsx |
Adds import for '@testing-library/jest-dom/vitest' and new test case verifying numeric indices (0, 1, 2) as accessorKey values render headers and data correctly |
Estimated code review effort
๐ฏ 2 (Simple) | โฑ๏ธ ~10 minutes
- Focus areas for review:
- Verify the string coercion logic in
column.tshandles all edge cases (undefined, null, numeric values) - Confirm the test case properly validates the documented array-index accessor behavior
- Ensure type broadening to
string | numberdoesn't introduce unexpected behavior downstream
- Verify the string coercion logic in
Poem
๐ฐ Numeric keys once caused a fright, But now they're coerced to strings just right! Arrays indexed, clean and true, Your table renders fresh and new! ๐
Pre-merge checks and finishing touches
โ Failed checks (2 warnings)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Description check | โ ๏ธ Warning | The PR description references the bug fix and linked issue but does not follow the required template structure with sections for 'Changes', 'Checklist', and 'Release Impact'. | Provide a description following the template with '๐ฏ Changes' section, 'โ Checklist' with contribution verification, and '๐ Release Impact' with changeset information. |
| Docstring Coverage | โ ๏ธ Warning | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
โ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | โ Passed | The title clearly and concisely describes the main change: coercing accessor keys to strings during column creation, which directly addresses the bug fix. |
| Linked Issues check | โ Passed | The PR successfully addresses issue #4815 by coercing numeric accessorKey values to strings, allowing array indices to work without crashing. |
| Out of Scope Changes check | โ Passed | All changes (type updates, string coercion in column creation, and test coverage) are directly scoped to fixing the array-index accessorKey bug. |
โจ Finishing touches
- [ ] ๐ Generate docstrings
๐งช Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.