Skip to content

Commit

Permalink
Merge pull request #30300 from devrari/fix-24198-dob-error
Browse files Browse the repository at this point in the history
fixed auto submit form when enter key pressed in selection modals
  • Loading branch information
Li357 authored Oct 30, 2023
2 parents 443bb82 + bf0a912 commit fa63485
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 2 deletions.
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,
]);
}
4 changes: 3 additions & 1 deletion src/libs/KeyboardShortcut/bindHandlerToKeydownEvent/index.ts
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

0 comments on commit fa63485

Please sign in to comment.