From 64218f12ae0ff612fadaaaa8dd013eb84cace1d6 Mon Sep 17 00:00:00 2001 From: Chris Topaloudis Date: Mon, 12 Oct 2020 15:52:53 +0200 Subject: [PATCH] components: Searchbar functions override - moved functionality from Element to the parent component SearchBar, so that we can supply them to the Element that overrides. - autofocus as a prop - support for actionProps and uiProps --- src/lib/components/SearchBar/SearchBar.js | 135 +++++++++++++++------- 1 file changed, 95 insertions(+), 40 deletions(-) diff --git a/src/lib/components/SearchBar/SearchBar.js b/src/lib/components/SearchBar/SearchBar.js index 00e3b4f7..63f991e9 100644 --- a/src/lib/components/SearchBar/SearchBar.js +++ b/src/lib/components/SearchBar/SearchBar.js @@ -31,70 +31,125 @@ 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; + const { + actionProps, + autofocus, + executeSearch, + onBtnSearchClick, + onInputChange, + onKeyPress, + overridableId, + placeholder, + uiProps, + } = this.props; return ( ); } } SearchBar.propTypes = { + actionProps: PropTypes.object, + autofocus: PropTypes.bool, + executeSearch: PropTypes.func, + onBtnSearchClick: PropTypes.func, + onInputChange: PropTypes.func, + onKeyPress: PropTypes.func, + overridableId: PropTypes.string, placeholder: PropTypes.string, - queryString: PropTypes.string.isRequired, + queryString: PropTypes.string, updateQueryString: PropTypes.func.isRequired, - overridableId: PropTypes.string, + uiProps: PropTypes.object, }; SearchBar.defaultProps = { + actionProps: null, + autofocus: false, + executeSearch: null, + onBtnSearchClick: null, + onInputChange: null, + onKeyPress: null, + overridableId: '', placeholder: '', queryString: '', - overridableId: '', + uiProps: null, }; +// 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) => ( ); -const Element = ({ overridableId, ...props }) => { - const { - placeholder: passedPlaceholder, - queryString, - onInputChange, - executeSearch, - } = props; - const placeholder = passedPlaceholder || 'Type something'; - const onBtnSearchClick = (event, input) => { - executeSearch(); - }; - const onKeyPress = (event, input) => { - if (event.key === 'Enter') { - executeSearch(); +class Element extends Component { + componentDidMount() { + const { autofocus } = this.props; + if (autofocus && this.focusInput) { + this.focusInput.focus(); } - }; - return ( - - { - onInputChange(value); - }} - value={queryString} - onKeyPress={onKeyPress} - /> - - ); -}; + } + + render() { + const { + actionProps, + onBtnSearchClick, + onInputChange, + onKeyPress, + overridableId, + placeholder, + queryString, + uiProps, + } = this.props; + + return ( + + { + onInputChange(value); + }} + value={queryString} + onKeyPress={onKeyPress} + ref={(input) => { + this.focusInput = input; + }} + /> + + ); + } +} export default Overridable.component('SearchBar', SearchBarUncontrolled);