Skip to content

Commit

Permalink
components: Searchbar functions override
Browse files Browse the repository at this point in the history
Moved functionality from Element to the parent component SearchBar, so that we can supply them to the Element that overrides.
  • Loading branch information
topless committed Oct 8, 2020
1 parent db939e9 commit 1518cd7
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/lib/components/SearchBar/SearchBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,54 @@ class SearchBar extends Component {
this.updateQueryString(this.state.currentValue);
};

onBtnSearchClick = (event, input) => {
this.executeSearch();
};

onKeyPress = (event, input) => {
if (event.key === 'Enter') {
this.executeSearch();
}
};

render() {
const { placeholder, overridableId } = this.props;
return (
<Element
placeholder={placeholder}
queryString={this.state.currentValue}
onBtnSearchClick={this.onBtnSearchClick}
onInputChange={this.onInputChange}
onKeyPress={this.onKeyPress}
executeSearch={this.executeSearch}
overridableId={overridableId}
placeholder={placeholder}
queryString={this.state.currentValue}
/>
);
}
}

SearchBar.propTypes = {
onBtnSearchClick: PropTypes.func,
onInputChange: PropTypes.func,
onKeyPress: PropTypes.func,
overridableId: PropTypes.string,
placeholder: PropTypes.string,
queryString: PropTypes.string.isRequired,
updateQueryString: PropTypes.func.isRequired,
overridableId: PropTypes.string,
};

SearchBar.defaultProps = {
onBtnSearchClick: SearchBar.onBtnSearchClick,
onInputChange: SearchBar.onInputChange,
onKeyPress: SearchBar.onKeyPress,
overridableId: '',
placeholder: '',
queryString: '',
overridableId: '',
};

// NOTE: Adding the key prop, will recreate the SearchBar in order to update
// state with the latest redux queryString value.
// https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key
const SearchBarUncontrolled = (props) => (
<SearchBar key={props.queryString} {...props} />
);
Expand All @@ -66,18 +87,11 @@ const Element = ({ overridableId, ...props }) => {
const {
placeholder: passedPlaceholder,
queryString,
onBtnSearchClick,
onInputChange,
executeSearch,
onKeyPress,
} = props;
const placeholder = passedPlaceholder || 'Type something';
const onBtnSearchClick = (event, input) => {
executeSearch();
};
const onKeyPress = (event, input) => {
if (event.key === 'Enter') {
executeSearch();
}
};
return (
<Overridable id={buildUID('SearchBar.element', overridableId)} {...props}>
<Input
Expand Down

0 comments on commit 1518cd7

Please sign in to comment.