-
Notifications
You must be signed in to change notification settings - Fork 10
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
FEI-5533: Re-enable select keyboard tests for Dropdown and Clickable #2420
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@khanacademy/wonder-blocks-clickable": patch | ||
"@khanacademy/wonder-blocks-dropdown": patch | ||
"@khanacademy/wonder-blocks-core": patch | ||
--- | ||
|
||
Fixes keyboard tests in Dropdown and Clickable | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,25 @@ export const UsingAriaAttributes = { | |
render: SingleSelectAccessibility.bind({}), | ||
name: "Using aria attributes", | ||
}; | ||
|
||
const SingleSelectKeyboardSelection = () => { | ||
const [selectedValue, setSelectedValue] = React.useState(""); | ||
return ( | ||
<View> | ||
<SingleSelect | ||
placeholder="Choose" | ||
onChange={setSelectedValue} | ||
selectedValue={selectedValue} | ||
> | ||
<OptionItem label="apple" value="apple" /> | ||
<OptionItem label="orange" value="orange" /> | ||
<OptionItem label="pear" value="pear" /> | ||
</SingleSelect> | ||
</View> | ||
); | ||
}; | ||
|
||
export const UsingKeyboardSelection = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to disable this story in Chromatic tests? It seems similar to other SingleSelect stories! Also, do we want to highlight anything around keyboard selection for accessibility? We can reference stories in the |
||
render: SingleSelectKeyboardSelection.bind({}), | ||
name: "Using the keyboard", | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import * as React from "react"; | ||
import {keys} from "@khanacademy/wonder-blocks-core"; | ||
|
||
// NOTE: Potentially add to this as more cases come up. | ||
export type ClickableRole = | ||
|
@@ -229,11 +230,6 @@ const disabledHandlers = { | |
onKeyUp: () => void 0, | ||
} as const; | ||
|
||
const keyCodes = { | ||
enter: 13, | ||
space: 32, | ||
} as const; | ||
|
||
const startState: ClickableState = { | ||
hovered: false, | ||
focused: false, | ||
|
@@ -560,21 +556,21 @@ export default class ClickableBehavior extends React.Component< | |
if (onKeyDown) { | ||
onKeyDown(e); | ||
} | ||
|
||
const keyCode = e.which || e.keyCode; | ||
// Allow tests to use mixed case commands ("Space" or "space") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems interesting to modify the component logic to accommodate the casing used in tests! It looks like user-event will also use KeyboardEvent.key (or KeyboardEvent.code). I wonder if it's enough to solve the issue in tests by using the |
||
const keyCode = e.key.toLowerCase(); | ||
Comment on lines
+559
to
+560
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable should probably be called |
||
const {triggerOnEnter, triggerOnSpace} = | ||
getAppropriateTriggersForRole(role); | ||
if ( | ||
(triggerOnEnter && keyCode === keyCodes.enter) || | ||
(triggerOnSpace && keyCode === keyCodes.space) | ||
(triggerOnEnter && keyCode === keys.enter.toLowerCase()) || | ||
(triggerOnSpace && keyCode === keys.space.toLowerCase()) | ||
Comment on lines
+564
to
+565
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're normalizing the key name that we're getting from the event to be lowercase, why not have the constants in |
||
) { | ||
// This prevents space from scrolling down. It also prevents the | ||
// space and enter keys from triggering click events. We manually | ||
// call the supplied onClick and handle potential navigation in | ||
// handleKeyUp instead. | ||
e.preventDefault(); | ||
this.setState({pressed: true}); | ||
} else if (!triggerOnEnter && keyCode === keyCodes.enter) { | ||
} else if (!triggerOnEnter && keyCode === keys.enter.toLowerCase()) { | ||
// If the component isn't supposed to trigger on enter, we have to | ||
// keep track of the enter keydown to negate the onClick callback | ||
this.enterClick = true; | ||
|
@@ -587,17 +583,17 @@ export default class ClickableBehavior extends React.Component< | |
onKeyUp(e); | ||
} | ||
|
||
const keyCode = e.which || e.keyCode; | ||
const keyCode = e.key.toLowerCase(); | ||
const {triggerOnEnter, triggerOnSpace} = | ||
getAppropriateTriggersForRole(role); | ||
if ( | ||
(triggerOnEnter && keyCode === keyCodes.enter) || | ||
(triggerOnSpace && keyCode === keyCodes.space) | ||
(triggerOnEnter && keyCode === keys.enter.toLowerCase()) || | ||
(triggerOnSpace && keyCode === keys.space.toLowerCase()) | ||
) { | ||
this.setState({pressed: false, focused: true}); | ||
|
||
this.runCallbackAndMaybeNavigate(e); | ||
} else if (!triggerOnEnter && keyCode === keyCodes.enter) { | ||
} else if (!triggerOnEnter && keyCode === keys.enter) { | ||
this.enterClick = false; | ||
} | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const keys = { | ||
enter: "Enter", | ||
escape: "Escape", | ||
tab: "Tab", | ||
space: "Space", | ||
up: "ArrowUp", | ||
down: "ArrowDown", | ||
} as const; |
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.
suggestion: It would be helpful to include details in the changeset around the changes to the components since it will show up in the package changelog (dropdown package example)! That way, it can also communicate changes in behaviour that consuming apps may want to know about.
For example, in ClickableBehaviour, it looks like we now check
event.key
instead ofevent.which
orevent.keyCode
. This might impact tests in other projects so it would be helpful to come back to the changelog and see what changes could cause new behaviour!