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

react/DP-13169: Add default header search behavior. #519

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
3 changes: 3 additions & 0 deletions changelogs/DP-13169.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Minor
Added
- (React) [HeaderSearch] DP-13169: Added default behaviour to HeaderSearch for onClick action to direct to search.mass.gov. Can be overidden by passing a custom onClick function to HeaderSearch's buttonSearch props. #
avrilpearl marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 22 additions & 2 deletions react/src/components/molecules/HeaderSearch/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import is from 'is';
import ButtonWithIcon from '../../atoms/buttons/ButtonWithIcon';
import TypeAheadDropdown from '../../molecules/TypeAheadDropdown';
import componentPropTypeCheck from '../../utilities/componentPropTypeCheck';
Expand All @@ -19,16 +20,32 @@ class HeaderSearch extends React.Component {
handleChange(event) {
const query = event.target.value;
this.setState({ value: query });
// invokes custom function if passed in the component
/**
* Invokes a custom onChange function if passed.
* @param {string} query - The current query string of the input.
*/
if (typeof this.props.onChange === 'function') {
avrilpearl marked this conversation as resolved.
Show resolved Hide resolved
this.props.onChange(query);
}
}
handleClick(e) {
if (is.fn(this.props.buttonSearch.onClick)) {
avrilpearl marked this conversation as resolved.
Show resolved Hide resolved
/**
* Invokes a custom onClick function if passed to the buttonSearch component.
* @param {object} event - The click event.
* @param {string} query - The current query string input.
*/
this.props.buttonSearch.onClick({ event: e, query: this.state.value });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not be passing the event as it will have expired for whatever set buttonSearch.onClick.

} else if (this.state.value.length > 0) {
window.location.assign(`https://search.mass.gov/?q=${this.state.value}`);
}
}

render() {
const headerSearch = this.props;
const orgDropdown = headerSearch.orgDropdown;
const shouldShowTypeAhead = (orgDropdown && orgDropdown.dropdownButton && orgDropdown.inputText);
const { onClick, ...buttonSearchRest } = headerSearch.buttonSearch;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!, should we remove onSubmit since we decide not to use it

return(
<div className="ma__header-search__wrapper ma__header-search__wrapper--responsive">
{shouldShowTypeAhead &&
Expand All @@ -55,7 +72,10 @@ class HeaderSearch extends React.Component {
<div className="ma__header-search__post-filter">
{this.props.postInputFilter}
</div>
<ButtonWithIcon {...headerSearch.buttonSearch} />
<ButtonWithIcon
onClick={(e) => this.handleClick(e)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onClick={(e) => this.handleClick(e)}
onClick={this.handleClick}

{...buttonSearchRest}
/>
</form>
</div>
</div>
Expand Down