-
Notifications
You must be signed in to change notification settings - Fork 2
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
Make MultiSelect keep single selection unless a checkbox is clicked #1665
Conversation
bdb8e44
to
6b4177e
Compare
9ee0aa2
to
b7605c5
Compare
835624d
to
e4e86b5
Compare
} | ||
}} | ||
onKeyPress={e => { | ||
if (!disabled && ['Enter', 'Space'].includes(e.code)) { | ||
onKeyDown={e => { |
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 should have always been onKeyDown
, not onKeyPress
, as the keypress
event is deprecated.
85e1b57
to
5d447c9
Compare
// Do not invoke callback if clicked element is the checkbox, as it has | ||
// its own event handler. | ||
if (!disabled && e.target !== checkboxRef.current) { | ||
selectOneValue(); | ||
} |
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 first tried to simply do e.stopPropagation()
in the checkbox event handler, but it is an onChange
handler that triggers after this onClick
, so it didn't work.
That's why I did this explicit check here instead.
5d447c9
to
ac5c267
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1665 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 61 61
Lines 1044 1061 +17
Branches 406 413 +7
=========================================
+ Hits 1044 1061 +17 ☔ View full report in Codecov by Sentry. |
d3d89e0
to
729acab
Compare
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 great. One minor detail I flagged up is whether we should be using key
(the logical key) rather than code
(the physical key) here. I believe it is very rare that these would be different for the Space and Enter keys specifically but we should be in the habit of knowing which we are using.
729acab
to
ef6262a
Compare
092d0e9
to
a1c928a
Compare
I think the logic to use right/left arrows to move focus between the checkbox and the option, could potentially be implemented using However, I did a quick check and there are some limitations:
Because of these, I will consider refactoring it later, and keep it as is for now. |
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.
The value of KeyboardEvent.key
is ' '
for the space key. Otherwise LGTM. I should add that I'm not completely certain whether key
or code
is the "correct" choice here, though key
is definitely correct if you want to match printed keys. From a quick look at native select implementations, it seems they internally use the deprecated keyCode
.
a1c928a
to
6dcec19
Compare
I asked ChatGPT, and this was the answer:
Additionally, I quickly checked other keyboard event handler and we seem to use |
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.
LGTM
Depends on #1664
Part of #1658
Change
MultiSelect
behavior, so that clicking on an option only keeps that option selected, and you have to click on the checkbox specifically in order to select multiple options.The way the component interacts with the keyboard is by allowing you to focus the checkbox by pressing ArrowRight while an option is focused. This will focus the checkbox itself, allowing it to be changed by pressing Space.
While the checkbox is focused, pressing ArrowLeft will return focus to its surrounding option.
Grabacion.de.pantalla.desde.2024-08-19.17-25-32.webm
Todo
I implemented it like in Sentry. When the right arrow is pressed, the checkbox for the currently focused option is focused, and when the left arrow is pressed, the focus returns to the option.
Out of scope
The "All students" option is meant to reset selection, which means "all" are selected in the
MultiSelect
context. However, that option itself is never marked as selected.This is a known issue that already exists in current
MultiSelect
and I plan to address separately.