Skip to content

Commit

Permalink
fix(masthead-search): react searchNoRedirect prop doesn't work on ent…
Browse files Browse the repository at this point in the history
…er (#8516)

### Related Ticket(s)

#8334

### Description

people using the `searchNoRedirect` prop and press enter within the search box are still being redirected. The `onKeyDown` was removed in this [PR](#7365), but it is actually necessary when the `searchNoRedirect` is used

### Changelog

**New**

- added back the `onKeyDown` function

<!-- React and Web Component deploy previews are enabled by default. -->
<!-- To enable additional available deploy previews, apply the following -->
<!-- labels for the corresponding package: -->
<!-- *** "test: e2e": Codesandbox examples and e2e integration tests -->
<!-- *** "package: services": Services -->
<!-- *** "package: utilities": Utilities -->
<!-- *** "RTL": React / Web Components (RTL) -->
<!-- *** "feature flag": React / Web Components (experimental) -->
  • Loading branch information
ariellalgilmore authored Mar 16, 2022
1 parent cb404ae commit a176c21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52871,6 +52871,7 @@ exports[`Storyshots Components|Dotcom shell Search open 1`] = `
"aria-label": "Search all of IBM",
"className": "bx--header__search--input",
"onChange": [Function],
"onKeyDown": [Function],
"placeholder": "Search all of IBM",
"role": "combobox",
"value": "",
Expand Down Expand Up @@ -123771,6 +123772,7 @@ exports[`Storyshots Components|Masthead Search open onload 1`] = `
"aria-label": "Search all of IBM",
"className": "bx--header__search--input",
"onChange": [Function],
"onKeyDown": [Function],
"placeholder": "Search all of IBM",
"role": "combobox",
"value": "",
Expand Down
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

0 comments on commit a176c21

Please sign in to comment.