@@ -37,6 +37,7 @@ const SELECTOR_OPTIONS_EMPTY = '.c-select-options-empty'
37
37
const SELECTOR_SELECT = '.c-select'
38
38
const SELECTOR_SELECTED = '.c-selected'
39
39
const SELECTOR_SELECTION = '.c-select-selection'
40
+ const SELECTOR_SELECTION_CLEANER = '.c-select-selection-cleaner'
40
41
41
42
const EVENT_CHANGED = `changed${ EVENT_KEY } `
42
43
const EVENT_CLICK = `click${ EVENT_KEY } `
@@ -61,6 +62,7 @@ const CLASS_NAME_OPTIONS_EMPTY = 'c-select-options-empty'
61
62
const CLASS_NAME_SEARCH = 'c-select-search'
62
63
const CLASS_NAME_SELECTED = 'c-selected'
63
64
const CLASS_NAME_SELECTION = 'c-select-selection'
65
+ const CLASS_NAME_SELECTION_CLEANER = 'c-select-selection-cleaner'
64
66
const CLASS_NAME_SELECTION_TAGS = 'c-select-selection-tags'
65
67
const CLASS_NAME_SHOW = 'c-show'
66
68
const CLASS_NAME_TAG = 'c-select-tag'
@@ -104,6 +106,7 @@ class Select {
104
106
constructor ( element , config ) {
105
107
this . _element = element
106
108
this . _selectionElement = null
109
+ this . _selectionCleanerElement = null
107
110
this . _searchElement = null
108
111
this . _optionsElement = null
109
112
this . _config = this . _getConfig ( config )
@@ -167,13 +170,6 @@ class Select {
167
170
}
168
171
169
172
search ( text ) {
170
- // const rootElement = this._optionsElement
171
- // const customEvent = this._triggerSearchEvent(rootElement)
172
-
173
- // if (customEvent === null || customEvent.defaultPrevented) {
174
- // return
175
- // }
176
-
177
173
this . _search = text . length > 0 ? text . toLowerCase ( ) : text
178
174
this . _filterOptionsList ( )
179
175
EventHandler . trigger ( this . _element , EVENT_SEARCH )
@@ -197,12 +193,23 @@ class Select {
197
193
this . _selectionDeleteLast ( )
198
194
}
199
195
} )
196
+
200
197
EventHandler . on ( this . _optionsElement , EVENT_CLICK , event => {
201
198
event . preventDefault ( )
202
199
event . stopPropagation ( )
203
200
this . _onOptionsClick ( event . target )
204
201
} )
205
202
203
+ EventHandler . on ( this . _selectionCleanerElement , EVENT_CLICK , event => {
204
+ event . preventDefault ( )
205
+ event . stopPropagation ( )
206
+ this . _selectionClear ( )
207
+ this . _updateSelection ( )
208
+ // this._updateSelectionCleaner()
209
+ this . _updateSearch ( )
210
+ this . _updateSearchSize ( )
211
+ } )
212
+
206
213
EventHandler . on ( this . _optionsElement , EVENT_KEYDOWN , event => {
207
214
const key = event . keyCode || event . charCode
208
215
@@ -347,6 +354,7 @@ class Select {
347
354
this . _element . parentNode . insertBefore ( div , this . _element . nextSibling )
348
355
if ( ! this . _config . inline || ( this . _config . inline && this . _config . selection ) ) {
349
356
this . _createSelection ( )
357
+ // this._createSelectionCleaner()
350
358
}
351
359
352
360
if ( this . _config . search ) {
@@ -368,13 +376,20 @@ class Select {
368
376
this . _selectionElement = span
369
377
}
370
378
379
+ _createSelectionCleaner ( ) {
380
+ const cleaner = document . createElement ( 'span' )
381
+ cleaner . classList . add ( CLASS_NAME_SELECTION_CLEANER )
382
+ cleaner . innerHTML = '×'
383
+ this . _clone . append ( cleaner )
384
+
385
+ // this._updateSelectionCleaner()
386
+ this . _selectionCleanerElement = cleaner
387
+ }
388
+
371
389
_createSearchInput ( ) {
372
390
const input = document . createElement ( 'input' )
373
391
input . classList . add ( CLASS_NAME_SEARCH )
374
392
375
- // if (!this._config.inline && this._selection.length > 0 && (this._config.selectionType === 'tags' || this._config.selectionType === 'text')) {
376
- // input.size = 2
377
- // }
378
393
this . _searchElement = input
379
394
this . _updateSearchSize ( )
380
395
@@ -460,6 +475,7 @@ class Select {
460
475
}
461
476
462
477
this . _updateSelection ( )
478
+ // this._updateSelectionCleaner()
463
479
this . _updateSearch ( )
464
480
this . _updateSearchSize ( )
465
481
}
@@ -495,6 +511,7 @@ class Select {
495
511
const last = this . _selection . pop ( )
496
512
this . _selectionDelete ( last . value )
497
513
this . _updateSelection ( )
514
+ // this._updateSelectionCleaner()
498
515
this . _updateSearch ( )
499
516
}
500
517
}
@@ -530,6 +547,21 @@ class Select {
530
547
}
531
548
}
532
549
550
+ _updateSelectionCleaner ( ) {
551
+ if ( this . _selectionCleanerElement === null ) {
552
+ return
553
+ }
554
+
555
+ const selectionCleaner = SelectorEngine . findOne ( SELECTOR_SELECTION_CLEANER , this . _clone )
556
+
557
+ if ( this . _selection . length > 0 ) {
558
+ selectionCleaner . style . removeProperty ( 'display' )
559
+ return
560
+ }
561
+
562
+ selectionCleaner . style . display = 'none'
563
+ }
564
+
533
565
_updateSearch ( ) {
534
566
if ( ! this . _config . search ) {
535
567
return
@@ -614,10 +646,6 @@ class Select {
614
646
SelectorEngine . find ( SELECTOR_SELECTED , this . _clone ) . forEach ( element => {
615
647
element . classList . remove ( CLASS_NAME_SELECTED )
616
648
} )
617
-
618
- // EventHandler.trigger(this._element, EVENT_CHANGED, {
619
- // value: this._selection
620
- // })
621
649
}
622
650
623
651
// TODO: poprawić tą nazwę
0 commit comments