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

fixed auto submit form when enter key pressed in selection modals #30300

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function YearPickerModal(props) {
onSelectRow={(option) => props.onYearChange(option.value)}
initiallyFocusedOptionKey={props.currentYear.toString()}
showScrollIndicator
shouldStopPropagation
/>
</ScreenWrapper>
</Modal>
Expand Down
2 changes: 2 additions & 0 deletions src/components/SelectionList/BaseSelectionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function BaseSelectionList({
inputRef = null,
disableKeyboardShortcuts = false,
children,
shouldStopPropagation = false,
}) {
const {translate} = useLocalize();
const firstLayoutRef = useRef(true);
Expand Down Expand Up @@ -342,6 +343,7 @@ function BaseSelectionList({
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, selectFocusedOption, {
captureOnInputs: true,
shouldBubble: () => !flattenedSections.allOptions[focusedIndex],
shouldStopPropagation,
isActive: !disableKeyboardShortcuts && !disableEnterShortcut && isFocused,
});

Expand Down
3 changes: 3 additions & 0 deletions src/components/SelectionList/selectionListPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ const propTypes = {
/** Whether to show the default confirm button */
showConfirmButton: PropTypes.bool,

/** Whether to stop automatic form submission on pressing enter key or not */
shouldStopPropagation: PropTypes.bool,

/** Whether to prevent default focusing of options and focus the textinput when selecting an option */
shouldPreventDefaultFocusOnSelectRow: PropTypes.bool,

Expand Down
1 change: 1 addition & 0 deletions src/components/StatePicker/StateSelectorModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function StateSelectorModal({currentState, isVisible, onClose, onStateSelected,
onSelectRow={onStateSelected}
onChangeText={setSearchValue}
initiallyFocusedOptionKey={currentState}
shouldStopPropagation
/>
</ScreenWrapper>
</Modal>
Expand Down
1 change: 1 addition & 0 deletions src/components/ValuePicker/ValueSelectorModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function ValueSelectorModal({currentValue, items, selectedItem, label, isVisible
sections={[{data: sectionsData}]}
onSelectRow={onItemSelected}
initiallyFocusedOptionKey={currentValue}
shouldStopPropagation
/>
</ScreenWrapper>
</Modal>
Expand Down
16 changes: 15 additions & 1 deletion src/hooks/useKeyboardShortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function useKeyboardShortcut(shortcut, callback, config = {}) {
// Hence the use of CONST.EMPTY_ARRAY.
excludedNodes = CONST.EMPTY_ARRAY,
isActive = true,
shouldStopPropagation = false,
} = config;

useEffect(() => {
Expand All @@ -35,8 +36,21 @@ export default function useKeyboardShortcut(shortcut, callback, config = {}) {
priority,
shouldPreventDefault,
excludedNodes,
shouldStopPropagation,
);
}
return () => {};
}, [isActive, callback, captureOnInputs, excludedNodes, priority, shortcut.descriptionKey, shortcut.modifiers, shortcut.shortcutKey, shouldBubble, shouldPreventDefault]);
}, [
isActive,
callback,
captureOnInputs,
excludedNodes,
priority,
shortcut.descriptionKey,
shortcut.modifiers,
shortcut.shortcutKey,
shouldBubble,
shouldPreventDefault,
shouldStopPropagation,
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ const bindHandlerToKeydownEvent: BindHandlerToKeydownEvent = (getDisplayName, ev
if (callback.shouldPreventDefault) {
event.preventDefault();
}

if (callback.shouldStopPropagation) {
event.stopPropagation();
}
// If the event should not bubble, short-circuit the loop
return shouldBubble;
});
Expand Down
3 changes: 3 additions & 0 deletions src/libs/KeyboardShortcut/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type EventHandler = {
shouldPreventDefault: boolean;
shouldBubble: boolean | (() => void);
excludedNodes: string[];
shouldStopPropagation: boolean;
};

// Handlers for the various keyboard listeners we set up
Expand Down Expand Up @@ -135,6 +136,7 @@ function subscribe(
priority = 0,
shouldPreventDefault = true,
excludedNodes = [],
shouldStopPropagation = false,
) {
const platformAdjustedModifiers = getPlatformEquivalentForKeys(modifiers);
const displayName = getDisplayName(key, platformAdjustedModifiers);
Expand All @@ -150,6 +152,7 @@ function subscribe(
shouldPreventDefault,
shouldBubble,
excludedNodes,
shouldStopPropagation,
});

if (descriptionKey) {
Expand Down
Loading