Skip to content

Commit 627ed06

Browse files
authored
fix: prevent Enter key from clearing selection when no option focused (#7999)
* fix: prevent Enter key from clearing selection when no option focused Signed-off-by: Iniubong Obonguko <iniubongobonguko2018+github@gmail.com> * chore: remove multiple prevent defaults * chore: lint --------- Signed-off-by: Iniubong Obonguko <iniubongobonguko2018+github@gmail.com>
1 parent 2b016a5 commit 627ed06

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

components/lib/dropdown/Dropdown.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,16 +496,29 @@ export const Dropdown = React.memo(
496496
};
497497

498498
const onEnterKey = (event) => {
499+
event.preventDefault();
500+
499501
if (!overlayVisibleState) {
500502
setFocusedOptionIndex(-1);
501503
onArrowDownKey(event);
502504
} else {
503-
if (focusedOptionIndex !== -1) {
504-
onOptionSelect(event, visibleOptions[focusedOptionIndex]);
505+
if (focusedOptionIndex === -1) {
506+
return;
505507
}
506-
}
507508

508-
event.preventDefault();
509+
const focusedOption = visibleOptions[focusedOptionIndex];
510+
const optionValue = getOptionValue(focusedOption);
511+
512+
if (optionValue == null || optionValue == undefined) {
513+
hide();
514+
resetFilter();
515+
updateEditableLabel(selectedOption);
516+
517+
return;
518+
}
519+
520+
onOptionSelect(event, focusedOption);
521+
}
509522
};
510523

511524
const onEscapeKey = (event) => {

0 commit comments

Comments
 (0)