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

fix(masthead-search): react searchNoRedirect prop doesn't work on enter #8516

Merged
Changes from 2 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
22 changes: 22 additions & 0 deletions packages/react/src/components/Masthead/MastheadSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,27 @@ const MastheadSearch = forwardRef(
event.currentTarget.dispatchEvent(onSearchNoRedirect);
}

/**
* Custom onKeyDown event handlers
*
* @param {event} event The callback event
*/
function onKeyDown(event) {
switch (event.key) {
case 'Enter': {
// Disables Enter key if searchNoRirect is true
if (rest.searchNoRedirect) {
onSearchNoRedirect(event, state.val);
event.preventDefault();
}
// Disable search on enter key if the search field is empty
if (!state.val) {
event.preventDefault();
}
}
}
}

/**
* When the input field changes, we set the new val to our state
*
Expand All @@ -296,6 +317,7 @@ const MastheadSearch = forwardRef(
placeholder: placeHolderText,
value: state.val,
onChange,
onKeyDown,
className: `${prefix}--header__search--input`,
'aria-label': placeHolderText,
role: 'combobox',
Expand Down