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

Prevent enter key event if the focused element is a clickable element #32464

Merged
merged 16 commits into from
Jan 12, 2024
Merged
4 changes: 2 additions & 2 deletions src/components/OptionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ function OptionRow({
<PressableWithFeedback
onPress={() => onSelectedStatePressed(option)}
disabled={isDisabled}
role={CONST.ROLE.CHECKBOX}
accessibilityLabel={CONST.ROLE.CHECKBOX}
role={CONST.ROLE.BUTTON}
accessibilityLabel={CONST.ROLE.BUTTON}
>
<SelectCircle isChecked={isSelected} />
</PressableWithFeedback>
Expand Down
56 changes: 52 additions & 4 deletions src/components/OptionsSelector/BaseOptionsSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ class BaseOptionsSelector extends Component {
this.incrementPage = this.incrementPage.bind(this);
this.sliceSections = this.sliceSections.bind(this);
this.calculateAllVisibleOptionsCount = this.calculateAllVisibleOptionsCount.bind(this);
this.handleFocusIn = this.handleFocusIn.bind(this);
this.handleFocusOut = this.handleFocusOut.bind(this);
this.debouncedUpdateSearchValue = _.debounce(this.updateSearchValue, CONST.TIMING.SEARCH_OPTION_LIST_DEBOUNCE_TIME);
this.relatedTarget = null;
this.accessibilityRoles = _.values(CONST.ROLE);
this.isWebOrDesktop = [CONST.PLATFORM.DESKTOP, CONST.PLATFORM.WEB].includes(getPlatform());

const allOptions = this.flattenSections();
const sections = this.sliceSections();
Expand All @@ -95,12 +99,15 @@ class BaseOptionsSelector extends Component {
shouldShowReferralModal: false,
errorMessage: '',
paginationPage: 1,
disableEnterShortCut: false,
value: '',
};
}

componentDidMount() {
this.subscribeToKeyboardShortcut();
this.subscribeToEnterShortcut();
this.subscribeToCtrlEnterShortcut();
this.subscribeActiveElement();

if (this.props.isFocused && this.props.autoFocus && this.textInput) {
this.focusTimeout = setTimeout(() => {
Expand All @@ -114,16 +121,25 @@ class BaseOptionsSelector extends Component {
componentDidUpdate(prevProps, prevState) {
if (prevProps.isFocused !== this.props.isFocused) {
if (this.props.isFocused) {
this.subscribeToKeyboardShortcut();
this.subscribeToEnterShortcut();
this.subscribeToCtrlEnterShortcut();
} else {
this.unSubscribeFromKeyboardShortcut();
}
}

if (prevState.disableEnterShortCut !== this.state.disableEnterShortCut) {
if (this.state.disableEnterShortCut) {
this.unsubscribeEnter();
} else {
this.subscribeToEnterShortcut();
}
}

// Screen coming back into focus, for example
// when doing Cmd+Shift+K, then Cmd+K, then Cmd+Shift+K.
// Only applies to platforms that support keyboard shortcuts
if ([CONST.PLATFORM.DESKTOP, CONST.PLATFORM.WEB].includes(getPlatform()) && !prevProps.isFocused && this.props.isFocused && this.props.autoFocus && this.textInput) {
if (this.isWebOrDesktop && !prevProps.isFocused && this.props.isFocused && this.props.autoFocus && this.textInput) {
setTimeout(() => {
this.textInput.focus();
}, CONST.ANIMATED_TRANSITION);
Expand Down Expand Up @@ -183,6 +199,7 @@ class BaseOptionsSelector extends Component {
}

this.unSubscribeFromKeyboardShortcut();
this.unSubscribeActiveElement();
}

/**
Expand Down Expand Up @@ -259,7 +276,36 @@ class BaseOptionsSelector extends Component {
this.setState((prevState) => ({shouldShowReferralModal: !prevState.shouldShowReferralModal}));
}

subscribeToKeyboardShortcut() {
handleFocusIn() {
const activeElement = document.activeElement;
this.setState({
disableEnterShortCut: activeElement && this.accessibilityRoles.includes(activeElement.role) && activeElement.role !== CONST.ROLE.PRESENTATION,
});
}

handleFocusOut() {
this.setState({
disableEnterShortCut: false,
});
}

subscribeActiveElement() {
if (!this.isWebOrDesktop) {
return;
}
document.addEventListener('focusin', this.handleFocusIn);
document.addEventListener('focusout', this.handleFocusOut);
}

unSubscribeActiveElement() {
if (!this.isWebOrDesktop) {
return;
}
document.removeEventListener('focusin', this.handleFocusIn);
document.removeEventListener('focusout', this.handleFocusOut);
}

subscribeToEnterShortcut() {
const enterConfig = CONST.KEYBOARD_SHORTCUTS.ENTER;
this.unsubscribeEnter = KeyboardShortcut.subscribe(
enterConfig.shortcutKey,
Expand All @@ -269,7 +315,9 @@ class BaseOptionsSelector extends Component {
true,
() => !this.state.allOptions[this.state.focusedIndex],
);
}

subscribeToCtrlEnterShortcut() {
const CTRLEnterConfig = CONST.KEYBOARD_SHORTCUTS.CTRL_ENTER;
this.unsubscribeCTRLEnter = KeyboardShortcut.subscribe(
CTRLEnterConfig.shortcutKey,
Expand Down