Skip to content

Commit

Permalink
Merge pull request #34313 from callstack-internal/hur/fix-34239
Browse files Browse the repository at this point in the history
fix: enter key not working
  • Loading branch information
Julesssss authored Jan 11, 2024
2 parents 5901e1c + 864dead commit 2a89886
Showing 1 changed file with 28 additions and 41 deletions.
69 changes: 28 additions & 41 deletions src/components/Button/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useFocusEffect} from '@react-navigation/native';
import {useIsFocused} from '@react-navigation/native';
import type {ForwardedRef} from 'react';
import React, {memo, useCallback, useMemo, useRef} from 'react';
import React, {useCallback, useMemo} from 'react';
import type {GestureResponderEvent, StyleProp, TextStyle, ViewStyle} from 'react-native';
import {ActivityIndicator, View} from 'react-native';
import Icon from '@components/Icon';
Expand Down Expand Up @@ -122,50 +122,37 @@ type KeyboardShortcutComponentProps = Pick<ButtonProps, 'isDisabled' | 'isLoadin

const accessibilityRoles: string[] = Object.values(CONST.ACCESSIBILITY_ROLE);

const KeyboardShortcutComponent = memo(
({isDisabled = false, isLoading = false, onPress = () => {}, pressOnEnter, allowBubble, enterKeyEventListenerPriority}: KeyboardShortcutComponentProps) => {
const isFocused = useRef(false);
const activeElementRole = useActiveElementRole();
function KeyboardShortcutComponent({isDisabled = false, isLoading = false, onPress = () => {}, pressOnEnter, allowBubble, enterKeyEventListenerPriority}: KeyboardShortcutComponentProps) {
const isFocused = useIsFocused();
const activeElementRole = useActiveElementRole();

const shouldDisableEnterShortcut = useMemo(() => accessibilityRoles.includes(activeElementRole ?? '') && activeElementRole !== CONST.ACCESSIBILITY_ROLE.TEXT, [activeElementRole]);
const shouldDisableEnterShortcut = useMemo(() => accessibilityRoles.includes(activeElementRole ?? '') && activeElementRole !== CONST.ACCESSIBILITY_ROLE.TEXT, [activeElementRole]);

useFocusEffect(
useCallback(() => {
isFocused.current = true;

return () => {
isFocused.current = false;
};
}, []),
);

const keyboardShortcutCallback = useCallback(
(event?: GestureResponderEvent | KeyboardEvent) => {
if (!validateSubmitShortcut(isDisabled, isLoading, event)) {
return;
}
onPress();
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[isDisabled, isLoading],
);
const keyboardShortcutCallback = useCallback(
(event?: GestureResponderEvent | KeyboardEvent) => {
if (!validateSubmitShortcut(isDisabled, isLoading, event)) {
return;
}
onPress();
},
[isDisabled, isLoading, onPress],
);

const config = useMemo(
() => ({
isActive: pressOnEnter && !shouldDisableEnterShortcut && isFocused.current,
shouldBubble: allowBubble,
priority: enterKeyEventListenerPriority,
shouldPreventDefault: false,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[shouldDisableEnterShortcut],
);
const config = useMemo(
() => ({
isActive: pressOnEnter && !shouldDisableEnterShortcut && isFocused,
shouldBubble: allowBubble,
priority: enterKeyEventListenerPriority,
shouldPreventDefault: false,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[shouldDisableEnterShortcut, isFocused],
);

useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, keyboardShortcutCallback, config);
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, keyboardShortcutCallback, config);

return null;
},
);
return null;
}

KeyboardShortcutComponent.displayName = 'KeyboardShortcutComponent';

Expand Down

0 comments on commit 2a89886

Please sign in to comment.