From 38a8ffff951d121c7e579e2d841b09899de292ac Mon Sep 17 00:00:00 2001 From: Hartorn Date: Mon, 29 May 2017 16:19:40 +0200 Subject: [PATCH 1/2] Adding onClearSelect options --- .../input/autocomplete-select/edit.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/components/input/autocomplete-select/edit.js b/src/components/input/autocomplete-select/edit.js index f0537d506..610d5e4ee 100644 --- a/src/components/input/autocomplete-select/edit.js +++ b/src/components/input/autocomplete-select/edit.js @@ -22,13 +22,15 @@ const propTypes = { placeholder: PropTypes.string, querySearcher: PropTypes.func.isRequired, renderOptions: PropTypes.func, - value: PropTypes.string + value: PropTypes.string, + onSelectClear: PropTypes.bool }; const defaultProps = { keyName: 'key', labelName: 'label', - inputTimeout: 200 + inputTimeout: 200, + onSelectClear: false }; @MDBehaviour('loader') @@ -179,11 +181,17 @@ class Autocomplete extends Component { _select(key) { const {options} = this.state; - const {onChange, keyName, labelName} = this.props; + const {onChange} = this.props; const resolvedLabel = options.get(key) || ''; this.refs.htmlInput.blur(); - this.setState({inputValue: this.i18n(resolvedLabel), selected: key, focus: false}, () => { - if (onChange) onChange(key); + let newState = {inputValue: this.i18n(resolvedLabel), selected: key, focus: false}; + if (this.props.onSelectClear && this.props.onSelectClear === true) { + newState = {inputValue: null, selected: null, focus: false}; + } + this.setState(newState, () => { + if (onChange) { + onChange(key); + } }); } From 930a20a57f3485178bee6a4900ae0093fa3eee87 Mon Sep 17 00:00:00 2001 From: Hartorn Date: Mon, 29 May 2017 16:29:02 +0200 Subject: [PATCH 2/2] Add clearOnNullValue options --- src/components/input/autocomplete-select/edit.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/components/input/autocomplete-select/edit.js b/src/components/input/autocomplete-select/edit.js index 610d5e4ee..52e45eab6 100644 --- a/src/components/input/autocomplete-select/edit.js +++ b/src/components/input/autocomplete-select/edit.js @@ -23,14 +23,16 @@ const propTypes = { querySearcher: PropTypes.func.isRequired, renderOptions: PropTypes.func, value: PropTypes.string, - onSelectClear: PropTypes.bool + onSelectClear: PropTypes.bool, + clearOnNullValue: PropTypes.bool }; const defaultProps = { keyName: 'key', labelName: 'label', inputTimeout: 200, - onSelectClear: false + onSelectClear: false, + clearOnNullValue: true }; @MDBehaviour('loader') @@ -74,9 +76,14 @@ class Autocomplete extends Component { } else if (customError !== this.props.customError) { this.setState({customError}); } + if (error) { this.setState({customError: error}); } + + if (this.props.clearOnNullValue && this.props.clearOnNullValue === true && value === null && this.state.inputValue !== null) { + this.setState({inputValue: null}); + } } componentDidUpdate() {