Repository navigation
fix(DataTable): sort rows on the first render with an initial sort - #8710
junkerderprovinz wants to merge 2 commits into
Conversation
🦋 Changeset detectedLatest commit: 6ec7c2a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
8de9d87 to
48b88bf
Compare
Ultroncn
left a comment
There was a problem hiding this comment.
Thanks for fixing the first-render sort — the code change itself looks right to me: rowOrder is now initialised through getSortedRowOrder (useTable.ts), the column lookup uses the same column.id ?? column.field identity as the header derivation and the sortByColumn reset check, and the externalSorting path still keeps the given order (the new test covers it).
One doc gap from the behaviour change: the initialSortColumn docs still tell consumers to pre-sort their data, which this PR makes unnecessary:
packages/react/src/DataTable/DataTable.tsx(~line 56-60, JSDoc): "Provide the id or field of the column by which the table is sorted. When using thisprop, the input data must be sorted by this column in ascending order"packages/react/src/DataTable/DataTable.docs.json(~line 190-194): same sentence (it tracks the JSDoc — both files change in lockstep in the git history).
Since the table now sorts the rows itself on the first render (in initialSortDirection, default ascending), that sentence reads as a requirement that no longer exists. Would it make sense to reword it along the lines of: "Provide the id or field of the column by which the table is sorted. The rows are sorted by this column on the first render; the input data does not need to be pre-sorted." — with the externalSorting case kept in mind (there the table does not sort internally, so the input data still needs to arrive in the desired order)?
initialSortColumn and initialSortDirection set the header state, but the rows kept the order of data until data changed or a header was clicked. The row order is now sorted in its useState initializer, the approach agreed on in primer#4602. getSortedRowOrder moves out of the hook and looks up columns instead of headers so it can run before the headers exist. Co-authored-by: Ben Wittenberg <394958+bwittenberg@users.noreply.github.com>
aca1910 to
4cd0310
Compare
|
Hi @Ultroncn, thank you for checking the |
Closes #3951
Hi! This picks up #4602 by @bwittenberg, who found the cause and wrote the first version of this fix. Ben mentioned in the PR not being able to continue, so I finished it along the lines @TylerJDev chose there (option 1, sort the data when the row order state is created). Thank you, Ben, you are credited as co-author.
With
initialSortColumnorinitialSortDirection, the header shows the sort direction, but the rows stay in the order ofdatauntildatachanges or a header is clicked. The row order is now sorted in itsuseStateinitializer, so there is no extra render. For that,getSortedRowOrdermoves out of the hook and looks up the column incolumnsinstead ofheaders, because the headers do not exist yet when the initial state is created.externalSortingstill keeps the order as given. AninitialSortColumngiven as a column'sfieldwhile the column also has anidnow resolves to that column, so the header shows the sort too.#3951 was closed by the stale bot, but the bug is still there on
main.Changelog
New
Changed
DataTablesorts the rows on the first render wheninitialSortColumnorinitialSortDirectionis set.Removed
Rollout strategy
Tables that pass
initialSortColumnwith unsorteddatawill now show sorted rows on the first render, which is what the header already claims. Ben raised that some consumers might rely on the old order, so you may want to check it with the integration workflow.Testing & Reviewing
The existing test for "only
initialSortDirectionis provided" asserted the unsorted order and is updated. There are new tests for sorting on the first render withinitialSortColumn, withinitialSortDirection="DESC"and thealphanumericstrategy, and forexternalSortingkeeping the given order. The first-render tests fail onmain, and all DataTable tests pass with the change.AI was used as a helper for this change.
Thanks for maintaining Primer!