-
Notifications
You must be signed in to change notification settings - Fork 645
fix(SelectPanel): selected items should appear at the top #5867
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
Conversation
🦋 Changeset detectedLatest commit: ead6002 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 |
|
👋 Hi, this pull request contains changes to the source code that github/github depends on. If you are GitHub staff, we recommend testing these changes with github/github using the integration workflow. Thanks! |
| const itemASelected = selectedOnSort.some(item => | ||
| Object.entries(item).every(([key, value]) => { | ||
| if (key === 'selected') { | ||
| return true | ||
| } | ||
| return itemA[key as keyof ItemProps] === value | ||
| }), | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems expensive :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wanted to ask if it would be possible for the sort order to be maintained as a stateful value and compute it onClose or onOpen instead of having to run the sort every render?
It seemed like the logic that we have for this is something like:
- When open, keep the ordering of items constant
- When closing (or when re-opening), sort the items by:
- selected items come first
- sort selected items by group, if applicable
- otherwise, sort by alphanumeric
- otherwise, sort by alphanumeric
- selected items come first
Does that track with what you're seeing?
| // Then order by text | ||
| if ((itemA.text ?? '') < (itemB.text ?? '')) { | ||
| return -1 | ||
| } else if ((itemA.text ?? '') > (itemB.text ?? '')) { | ||
| return 1 | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I baked in alphabetically ordering through text but now that I'm looking at the issue it doesn't really look like a requirement 🤔
I can remove and do selection-at-top only if this is not wanted/needed 👀
CC: @emilybrick
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consumers could want to sub-sort based on something besides alphabetical order. For example: sort most frequently used labels to the top.
I think we should remove this default alphanumeric sort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to steal the alphanumeric stuff from data table btw if it's helpful! We have some sort helpers over there
size-limit report 📦
|
| }, | ||
| } as ItemProps | ||
| }) | ||
| .sort((itemA, itemB) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm coding this as the default behavior. Wondering if we should allow consumer to opt-out of this behavior or provide their custom sort function 👀
cc: @emilybrick
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel strongly that we should let consumers opt-out by providing their own sort function. I think DataTable has a nice API for handling this: https://github.com/primer/react/blob/main/packages/react/src/DataTable/column.ts#L59
…nto fix/select-panel-ordering
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
visual updates are due to items being sorted now
…nto fix/select-panel-ordering
This reverts commit 4d41982.
emilybrick
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, selected items move to top/appear at top + in the same order as they appear in the button. WIth border removed. Thank you!
…nto fix/select-panel-ordering
|
👋 Hi from github/github! Your integration PR is ready: https://github.com/github/github/pull/371812 |
joshblack
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments around the effect stuff, feel free to ignore me if this is getting annoying 😅 lol
| useEffect(() => { | ||
| if (open) { | ||
| resetSort() | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [open]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could potentially handled this this like in: https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes to avoid having to use an effect here. Another idea is to call resetSort() when onClose() is called (or whatever appropriate prop it would be for when it open/closes)
| @@ -385,8 +463,56 @@ export function SelectPanel({ | |||
| }, | |||
| } as ItemProps | |||
| }) | |||
|
|
|||
| setItemsToRender(itemsToRender) | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this and the effect below potentially move into just the render function instead of effects or are we experiencing some perf problems when that happens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we can, I put it in a useEffect as a response to your comment here #5867 (comment), (also see #5867 (comment)) so we didn't have to sort on every render 👀
| sortKey?: keyof ItemInput | ||
| sortDirection?: 'asc' | 'desc' | ||
| sortFn?: (a: ItemInput, b: ItemInput) => number |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you share more about baking these directly into select panel? Put another way, folks could just sort the array however they wanted to before but now they would need to hook into SelectPanel's API to do that.
This might be a preference for folks downstream or on the team, totally understand if so 👍 It wasn't immediately clear to me why we would do this and just wanted to ask.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my logic was we're already sorting in order to display selected at the top so I was thinking instead of doing (potentially) a double sort, one at consumer-side and one on our side, we could just do it all together, so that's where sortKey and sortDirection come in sortFn comes from @mperrotti suggestion that we should allow for custom sort functions.
I also thought providing these could offload work from the consumer and make it "easier", I could be wrong though 👀
Put another way, folks could just sort the array however they wanted to before but now they would need to hook into SelectPanel's API to do that
these are all opt-in, folks could totally still do their own sorting before hand and everything should work as expected.
All that being said, these three props can totally be removed and this PRs purpose will still be served.
|
🔴 golden-jobs completed with status |
Co-authored-by: Josh Black <[email protected]>
|
closing in favor of #5971 |
Closes https://github.com/github/primer/issues/2409
Adds logic to SelectPanel to automatically sort items, displaying the selected items first. The sorted list gets rearranged on:
Also adds capabilities for custom sorting, providing a sorting key, sort by ascending or descending and opt-out of "selected on top" functionality.
by default, selected items will be displayed at the top when primer_react_select_panel_order_selected_at_top FF is on
Changelog
New
Changed
Removed
Rollout strategy
Testing & Reviewing
SelectPanel stories
Merge checklist