profileSelector: Prevent wizard refresh on Enter key in OpenSCAP profile selector

Added event.preventDefault() in onKeyDown handler to stop default form
submission behavior when hitting Enter in the typeahead filter.
Also implemented auto-selection of single filtered results on Enter.
This commit is contained in:
Michal Gold 2025-06-19 13:23:43 +02:00 committed by Gianluca Zuccarelli
parent 42d96edd00
commit 6f9a34c972

View file

@ -274,6 +274,27 @@ const ProfileSelector = () => {
}
};
const onKeyDown = (event: React.KeyboardEvent) => {
if (event.key === 'Enter') {
event.preventDefault();
if (!isOpen) {
setIsOpen(true);
} else if (selectOptions.length === 1) {
const singleProfile = selectOptions[0];
const selection: OScapSelectOptionValueType = {
profileID: singleProfile.id,
toString: () => singleProfile.name || '',
};
setInputValue(singleProfile.name || '');
setFilterValue('');
applyChanges(selection);
setIsOpen(false);
}
}
};
const applyChanges = (selection: OScapSelectOptionValueType) => {
if (selection.profileID === undefined) {
// handle user has selected 'None' case
@ -337,6 +358,7 @@ const ProfileSelector = () => {
value={profileID ? profileID : inputValue}
onClick={onInputClick}
onChange={onTextInputChange}
onKeyDown={onKeyDown}
autoComplete="off"
placeholder="None"
isExpanded={isOpen}