-
Notifications
You must be signed in to change notification settings - Fork 536
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(DataTable): sort table on initial render with initialSortColumn
or initialSortDirection
props
#4602
Conversation
… or `initialSortDirection` props
🦋 Changeset detectedLatest commit: 3370857 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 |
Hello @bwittenberg, thank you for the PR! Also thank you for providing detailed comments on what this PR aims to achieve. The one potential issue that could arise from this change, is that that the state would render twice, as the conditional For example: const [rowOrder, setRowOrder] = useState((data) => {
// sort the data when we store it in state
}) |
@TylerJDev , thanks for the review! I'm happy to implement your suggestion, but wanted to provide some thoughts for you to consider. Please let me know which option you prefer, or maybe you have another option in mind that I'm not seeing. If you prefer changes to the original PR, I can run through the PR checklist again. FWIW, I prefer Option1: Avoid the call to
|
@bwittenberg, thanks so much for the response, and sorry about the delay on my part! I'll take a look at your proposals before the week is up! |
Sounds good @TylerJDev ! |
I apologize for the long wait @bwittenberg, I completely lost track of time! 🙇 I'd say let's go with option 1, I think that will be the safer option, and will allow us to monitor for any potential regressions a bit easier since we don't need to worry about performance like we might for option 2. Also I love the additional option 3! I'm not too familiar with the component to understand the full tradeoffs we might have with this option, so I'll cc @joshblack in case he wants to provide input here 😁 Next steps would be:
|
Thanks for the response @TylerJDev ! This is on my radar to fix! |
Hi! This pull request has been marked as stale because it has been open with no activity for 60 days. You can comment on the pull request or remove the stale label to keep it open. If you do nothing, this pull request will be closed in 7 days. |
Closes #3951
Changelog
New
Changed
useTable
hook is responsible to sort the data for theDataTable
. If the data changes, then it sorts according to the current sort state. It should also sort data on initial render. This PR updates the initial state ofuseTable
so that the data is sorted on initial render.Removed
Rollout strategy
Answering this question led to a questions about the purpose/intended use of this component. In my experience with data tables, consumers either have all the data on the client and want to render it in the table, or consumers do not have all the data, and may need to fetch more.
If consumers have all the data, then it's possible to sort and filter on the client, and then render correct results. I'll name this "completeData" mode. I wouldn't use this name as part of the API, but it works for this discussion.
If consumers do not have all the data, then it's not possible to sort and filter on the client, because all client side sorts and filters operate on a partial data set. For correct results, the sort and filter must be done on the server, where the complete data set is known. I'll name this "partialData" mode.
Why discuss these two different usages in this PR?
Prior to this change, on initial render only, the data table would not sort the passed in data. For consumers with partial data, this is the desired behavior. After this change, consumers that are using this component with partial data will notice that the data is now sorted on the client on initial render.
Based on the current
DataTable
API, I think this component was designed for "completeData" mode, because any sorting and renders after the initial render only sort the data that's in the component state.To support "partialData" mode, this component would need to allow consumers to pass in the "sortColumn" and "sortDirection", and also expose callbacks so consumers can respond to sort change like
onSortChange
.To handle the "partialData" and "completeData" modes of a table, it could make sense to build a "controlled" version of the
DataTable
, maybe something likeStatelessDataTable
, and then theDataTable
can remain the "uncontrolled" version that adds state to theStatelessDataTable
.After looking at more of the components in
DataTable
, for the "partialData" mode, consumers could useTable
directly withimport { Table } from '@primer/react/experimental'
.Apologies for the lengthy discussion. I suspect that the authors already know all of this, so this is mostly me trying to convince myself that this change should be considered a patch, because the
DataTable
is designed for the "completeData" mode, and consumers are probably not expecting the buggy behavior.Testing & Reviewing
http://<storybookurl>/?path=/story/drafts-components-datatable-features--with-custom-sorting
, assert data is sorted by updated at, click the updated at column, assert data is sorted in opposite direction, clickRepository
header, assert data is sorted byRepository
, clickRepository
again, assert sort changes directionMerge checklist