Skip to content
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

feat(what-input): Limit keyboard detection in inputs #25087

Merged
merged 7 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/fluentui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add a new comfy layout variation for `ChatMessage` @davezuko ([#23974](https://github.com/microsoft/fluentui/pull/23974))
- Add `FocusTrapZone` prop `preventScrollOnRestoreFocus` to prevent scroll on focus when `FocusTrapZone` releases @yuanboxue-amber ([#24632](https://github.com/microsoft/fluentui/pull/24632))
- Add new style to v0 Tooltip to match v9 Tooltip @GianoglioEnrico ([#24908](https://github.com/microsoft/fluentui/pull/24908))
- Limit keyboard detection in inputs @jurokapsiar ([#25087](https://github.com/microsoft/fluentui/pull/25087))

### Fixes
- Allow React 17 in `peerDependencies` of all packages and bump react-is to 17 @TristanWatanabe ([#24356](https://github.com/microsoft/fluentui/pull/24356))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
UIComponentProps,
isFromKeyboard as detectIsFromKeyboard,
createShorthand,
setWhatInputSource,
} from '../../utils';
import { List, ListProps } from '../List/List';
import { DropdownItem, DropdownItemProps } from './DropdownItem';
Expand Down Expand Up @@ -1167,12 +1168,22 @@ export const Dropdown = (React.forwardRef<HTMLDivElement, DropdownProps>((props,
case keyboardKey.ArrowLeft:
e.stopPropagation();
if (!context.rtl) {
// https://github.com/testing-library/user-event/issues/709
// JSDOM does not implement `event.view` so prune this code path in test
if (process.env.NODE_ENV !== 'test') {
setWhatInputSource(e.view.document, 'keyboard');
}
trySetLastSelectedItemAsActive();
}
break;
case keyboardKey.ArrowRight:
e.stopPropagation();
if (context.rtl) {
// https://github.com/testing-library/user-event/issues/709
// JSDOM does not implement `event.view` so prune this code path in test
if (process.env.NODE_ENV !== 'test') {
setWhatInputSource(e.view.document, 'keyboard');
}
trySetLastSelectedItemAsActive();
}
break;
Expand Down
27 changes: 25 additions & 2 deletions packages/fluentui/react-northstar/src/utils/whatInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isBrowser } from './isBrowser';
*/

// last used input type
let currentInput = 'initial';
let currentInput = 'mouse'; // assume happy path

// event buffer timer
let eventTimer = null;
Expand Down Expand Up @@ -108,6 +108,26 @@ const addListeners = (eventTarget: Window) => {
eventTarget.addEventListener('keyup', eventBuffer, true);
};

/**
*
* @param document document to apply the update to
* @param eventKey keyboard key passed from the event
* @returns true if mode should be switched, false if not (when focus is in inputs and key was not a navigational key)
jurokapsiar marked this conversation as resolved.
Show resolved Hide resolved
*/
const keyboardInputFocused = (document: Document, eventKey: number) => {
if (
document.activeElement.tagName === 'INPUT' ||
document.activeElement.tagName === 'TEXTAREA' ||
document.activeElement.getAttribute('contenteditable')
) {
return (
eventKey === 9 || // tab
eventKey === 117
); // F6
}
return true;
};

// checks conditions before updating new input
const setInput = (event: WhatInputEvents) => {
// only execute if the event buffer timer isn't running
Expand All @@ -120,7 +140,10 @@ const setInput = (event: WhatInputEvents) => {
}

const ignoreMatch = ignoreMap.indexOf(eventKey) === -1;
const shouldUpdate = (value === 'keyboard' && eventKey && ignoreMatch) || value === 'mouse' || value === 'touch';
const shouldUpdate =
(value === 'keyboard' && eventKey && ignoreMatch && keyboardInputFocused(event.view.document, eventKey)) ||
value === 'mouse' ||
value === 'touch';

if (currentInput !== value && shouldUpdate) {
currentInput = value;
Expand Down