From 7e784c699554dd85be3e9e219c59578995cadd38 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Wed, 3 Jan 2018 15:22:05 +0100 Subject: feat(Services): Improve user experience of service search --- src/components/ui/SearchInput.js | 48 +++++++++++++++------------------------- 1 file changed, 18 insertions(+), 30 deletions(-) (limited to 'src/components/ui/SearchInput.js') diff --git a/src/components/ui/SearchInput.js b/src/components/ui/SearchInput.js index bca412cef..a94cde201 100644 --- a/src/components/ui/SearchInput.js +++ b/src/components/ui/SearchInput.js @@ -9,36 +9,46 @@ import { debounce } from 'lodash'; export default class SearchInput extends Component { static propTypes = { value: PropTypes.string, - defaultValue: PropTypes.string, + placeholder: PropTypes.string, className: PropTypes.string, onChange: PropTypes.func, onReset: PropTypes.func, name: PropTypes.string, throttle: PropTypes.bool, throttleDelay: PropTypes.number, + autoFocus: PropTypes.bool, }; static defaultProps = { value: '', - defaultValue: '', + placeholder: '', className: '', name: uuidv1(), throttle: false, throttleDelay: 250, onChange: () => null, onReset: () => null, + autoFocus: false, } constructor(props) { super(props); this.state = { - value: props.value || props.defaultValue, + value: props.value, }; this.throttledOnChange = debounce(this.throttledOnChange, this.props.throttleDelay); } + componentDidMount() { + const { autoFocus } = this.props; + + if (autoFocus) { + this.input.focus(); + } + } + onChange(e) { const { throttle, onChange } = this.props; const { value } = e.target; @@ -52,26 +62,6 @@ export default class SearchInput extends Component { } } - onClick() { - const { defaultValue } = this.props; - const { value } = this.state; - - if (value === defaultValue) { - this.setState({ value: '' }); - } - - this.input.focus(); - } - - onBlur() { - const { defaultValue } = this.props; - const { value } = this.state; - - if (value === '') { - this.setState({ value: defaultValue }); - } - } - throttledOnChange(e) { const { onChange } = this.props; @@ -79,8 +69,8 @@ export default class SearchInput extends Component { } reset() { - const { defaultValue, onReset } = this.props; - this.setState({ value: defaultValue }); + const { onReset } = this.props; + this.setState({ value: '' }); onReset(); } @@ -88,7 +78,7 @@ export default class SearchInput extends Component { input = null; render() { - const { className, name, defaultValue } = this.props; + const { className, name, placeholder } = this.props; const { value } = this.state; return ( @@ -101,18 +91,16 @@ export default class SearchInput extends Component {