-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved keyboard navigation for SingleSelectionBoxes
- Trigger validation whenever focus leaves component - Ignore key events fired by held-down keys
- Loading branch information
Showing
7 changed files
with
118 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @flow | ||
export type { KeyboardManager } from './withKeyboardNavigation'; |
38 changes: 38 additions & 0 deletions
38
src/core_modules/capture-ui/BooleanField/withKeyboardNavigation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// @flow | ||
import React, { type ComponentType, useRef, useMemo } from 'react'; | ||
|
||
const managedKeys = ['Tab', ' ', 'Enter', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'F5']; | ||
|
||
export type KeyboardManager = { | ||
keyDown: (key: string) => boolean, | ||
keyUp: (key: string) => void, | ||
clear: () => void, | ||
}; | ||
|
||
export const withKeyboardNavigation = () => (InnerComponent: ComponentType<any>) => (props: any) => { | ||
const pressedKeys = useRef(new Set); | ||
|
||
const keyboardManager = useMemo(() => ({ | ||
keyDown: (key: string) => { | ||
if (!managedKeys.includes(key)) { | ||
return false; | ||
} | ||
const count = pressedKeys.current.size; | ||
pressedKeys.current.add(key); | ||
return count === 0; | ||
}, | ||
keyUp: (key: string) => { | ||
pressedKeys.current.delete(key); | ||
}, | ||
clear: () => { | ||
pressedKeys.current.clear(); | ||
}, | ||
}), [pressedKeys]); | ||
|
||
return ( | ||
<InnerComponent | ||
keyboardManager={keyboardManager} | ||
{...props} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters