@@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
33import classnames from 'classnames'
44import { isEmpty } from 'lodash'
55
6- import { Help , Search , Close } from '../../Icons.es6.js'
6+ import { Help , Search , Close , RemoveCircle } from '../../Icons.es6.js'
77import Option from '../option/Option.es6.js'
88
99class Select extends React . Component {
@@ -19,24 +19,31 @@ class Select extends React.Component {
1919 this . state = {
2020 isOpened : false ,
2121 query : '' ,
22- filteredOptions : props . search ? props . filter ( this . options , '' ) : this . options
22+ filteredOptions : props . search ? props . filter ( this . options , '' ) : this . options ,
23+ initialPlaceholder : '' ,
24+ hasInitialPlaceholderChanged : false
2325 }
2426 }
2527
2628 componentDidMount ( ) {
29+ this . setState ( { initialPlaceholder : this . props . placeholder } )
2730 document . addEventListener ( 'click' , this . handleDocumentClick , false )
2831 document . addEventListener ( 'touchend' , this . handleDocumentClick , false )
2932 }
3033
31- componentWillReceiveProps ( nextProps ) {
34+ UNSAFE_componentWillReceiveProps ( nextProps ) {
3235 const { children, search, filter} = nextProps
3336 this . options = this . updateOptions ( children )
3437 this . setState ( {
3538 filteredOptions : search ? filter ( this . options , this . state . query ) : this . options
3639 } )
3740 }
3841
39- componentDidUpdate ( ) {
42+ componentDidUpdate ( prevProps ) {
43+ const { placeholder, search } = this . props
44+ if ( ( prevProps . placeholder !== placeholder ) && ( placeholder !== this . state . initialPlaceholder ) ) {
45+ this . setState ( { hasInitialPlaceholderChanged : true } )
46+ }
4047 if ( this . props . search && this . state . isOpened && this . searchInput ) {
4148 this . searchInput . focus ( )
4249 }
@@ -117,8 +124,43 @@ class Select extends React.Component {
117124 } )
118125 }
119126
127+ renderCloseIcon = ( query ) => {
128+ return (
129+ < div
130+ className = { classnames ( 'ds-form-control-select__search-clear' , {
131+ 'is-hidden' : ! query
132+ } ) }
133+ onClick = { this . handleClearSearch }
134+ >
135+ < Close color = "#ddd" />
136+ </ div >
137+ )
138+ }
139+
140+ resetHandler = ( ) => {
141+ this . handleClearSearch ( )
142+ this . props . onResetClick ( )
143+ this . setState ( { hasInitialPlaceholderChanged : false } )
144+ }
145+
146+ renderResetIcon = ( ) => {
147+ return (
148+ < div
149+ className = { classnames ( 'ds-form-control-select__reset' , {
150+ 'is-hidden' : ! this . state . hasInitialPlaceholderChanged
151+ } ) }
152+ onClick = { this . resetHandler }
153+ >
154+ < RemoveCircle color = "#ddd" />
155+ </ div >
156+ )
157+ }
158+
120159 render ( ) {
121- const { error, disabled, className, label, preview, inlineSearch, placeholder, search, help} = this . props
160+ const {
161+ error, disabled, className, label, preview, showResetIcon,
162+ inlineSearch, placeholder, search, help
163+ } = this . props
122164 const { query, filteredOptions, isOpened} = this . state
123165 return (
124166 < div className = { classnames ( {
@@ -146,6 +188,7 @@ class Select extends React.Component {
146188 { inlineSearch && < Search color = "#ddd" /> }
147189 { inlineSearch && query ? query : placeholder }
148190 { preview && < img src = { preview } alt = { placeholder } /> }
191+ { showResetIcon && this . renderResetIcon ( ) }
149192 </ div >
150193
151194 { /* Options */ }
@@ -159,16 +202,8 @@ class Select extends React.Component {
159202 ref = { ( node ) => { this . searchInput = node } }
160203 onChange = { ( e ) => this . onSearch ( e . target . value ) }
161204 />
162- { inlineSearch && < div
163- className = { classnames ( 'ds-form-control-select__search-clear' , {
164- 'is-hidden' : ! query
165- } ) }
166- onClick = { this . handleClearSearch }
167- >
168- < Close color = "#ddd" />
169- </ div > }
205+ { inlineSearch && this . renderCloseIcon ( query ) }
170206 </ div > }
171-
172207 < div className = "ds-form-control-select__dropdown-options" >
173208 { filteredOptions }
174209 </ div >
@@ -201,6 +236,7 @@ Select.defaultProps = {
201236 children : [ ] ,
202237 keepOpen : ( ) => { } ,
203238 inlineSearch : false ,
239+ showResetIcon : false ,
204240 onSearchQueryClear : ( ) => { } ,
205241 fillSelectedQuery : false
206242}
@@ -226,8 +262,10 @@ Select.propTypes = {
226262 search : PropTypes . bool ,
227263 preview : PropTypes . string ,
228264 keepOpen : PropTypes . func ,
265+ showResetIcon : PropTypes . bool ,
229266 inlineSearch : PropTypes . bool ,
230267 onSearchQueryClear : PropTypes . func ,
268+ onResetClick : PropTypes . func ,
231269 fillSelectedQuery : PropTypes . bool
232270}
233271
0 commit comments