@@ -30,15 +30,13 @@ const TAB_KEY = 'Tab'
30
30
const RIGHT_MOUSE_BUTTON = 2
31
31
32
32
const SELECTOR_INPUT = '.c-select-search'
33
- const SELECTOR_LIST = '.c-select-options'
34
- const SELECTOR_MULTI_SELECT = '.c-select'
35
33
const SELECTOR_OPTGROUP = '.c-select-optgroup'
36
34
const SELECTOR_OPTION = '.c-select-option'
37
35
const SELECTOR_OPTIONS = '.c-select-options'
38
36
const SELECTOR_OPTIONS_EMPTY = '.c-select-options-empty'
37
+ const SELECTOR_SELECT = '.c-select'
39
38
const SELECTOR_SELECTED = '.c-selected'
40
39
const SELECTOR_SELECTION = '.c-select-selection'
41
- const SELECTOR_TAGS = '.c-select-tags'
42
40
43
41
const EVENT_CHANGE = `keyup${ EVENT_KEY } `
44
42
const EVENT_CLICK = `click${ EVENT_KEY } `
@@ -50,6 +48,7 @@ const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
50
48
const EVENT_KEYUP_DATA_API = `keyup${ EVENT_KEY } ${ DATA_API_KEY } `
51
49
52
50
const CLASS_NAME_SELECT = 'c-select'
51
+ const CLASS_NAME_SELECT_INLINE = 'c-select-inline'
53
52
const CLASS_NAME_SELECT_MULTIPLE = 'c-select-multiple'
54
53
const CLASS_NAME_OPTGROUP = 'c-select-optgroup'
55
54
const CLASS_NAME_OPTGROUP_LABEL = 'c-select-optgroup-label'
@@ -59,6 +58,7 @@ const CLASS_NAME_OPTIONS_EMPTY = 'c-select-options-empty'
59
58
const CLASS_NAME_SEARCH = 'c-select-search'
60
59
const CLASS_NAME_SELECTED = 'c-selected'
61
60
const CLASS_NAME_SELECTION = 'c-select-selection'
61
+ const CLASS_NAME_SELECTION_TAGS = 'c-select-selection-tags'
62
62
const CLASS_NAME_SHOW = 'c-show'
63
63
const CLASS_NAME_TAG = 'c-select-tag'
64
64
const CLASS_NAME_TAG_DELETE = 'c-select-tag-delete'
@@ -72,6 +72,7 @@ const Default = {
72
72
optionsEmptyPlaceholder : 'no items' ,
73
73
search : false ,
74
74
searchPlaceholder : 'Select...' ,
75
+ selection : true ,
75
76
selectionType : 'counter' ,
76
77
selected : [ ]
77
78
}
@@ -83,6 +84,7 @@ const DefaultType = {
83
84
optionsEmptyPlaceholder : 'string' ,
84
85
search : 'boolean' ,
85
86
searchPlaceholder : 'string' ,
87
+ selection : 'boolean' ,
86
88
selectionType : 'string' ,
87
89
selected : 'array'
88
90
}
@@ -315,8 +317,8 @@ class Select {
315
317
}
316
318
317
319
_hideNativeSelect ( ) {
318
- // this._element.tabIndex = '-1'
319
- // this._element.style.display = 'none'
320
+ this . _element . tabIndex = '-1'
321
+ this . _element . style . display = 'none'
320
322
}
321
323
322
324
_createSelect ( ) {
@@ -327,9 +329,16 @@ class Select {
327
329
div . classList . add ( CLASS_NAME_SELECT_MULTIPLE )
328
330
}
329
331
332
+ if ( this . _config . inline ) {
333
+ div . classList . add ( CLASS_NAME_SELECT_INLINE )
334
+ }
335
+
330
336
this . _clone = div
331
337
this . _element . parentNode . insertBefore ( div , this . _element . nextSibling )
332
- this . _createSelection ( )
338
+ if ( ! this . _config . inline || ( this . _config . inline && this . _config . selection ) ) {
339
+ this . _createSelection ( )
340
+ }
341
+
333
342
if ( this . _config . search ) {
334
343
this . _createSearchInput ( )
335
344
this . _updateSearch ( )
@@ -343,6 +352,9 @@ class Select {
343
352
_createSelection ( ) {
344
353
const span = document . createElement ( 'span' )
345
354
span . classList . add ( CLASS_NAME_SELECTION )
355
+ if ( this . _config . selectionType === 'tags' ) {
356
+ span . classList . add ( CLASS_NAME_SELECTION_TAGS )
357
+ }
346
358
this . _clone . append ( span )
347
359
348
360
this . _updateSelection ( )
@@ -365,6 +377,10 @@ class Select {
365
377
// input.placeholder = ''
366
378
// }
367
379
380
+ if ( this . _selection . length > 0 && ( this . _config . selectionType === 'tags' || this . _config . selectionType === 'text' ) ) {
381
+ input . size = 2
382
+ }
383
+
368
384
this . _clone . append ( input )
369
385
this . _searchElement = input
370
386
}
@@ -513,6 +529,10 @@ class Select {
513
529
// .c-select-selections
514
530
515
531
_updateSelection ( ) {
532
+ if ( this . _config . inline && ! this . _config . selection ) {
533
+ return
534
+ }
535
+
516
536
const selection = SelectorEngine . findOne ( SELECTOR_SELECTION , this . _clone )
517
537
518
538
if ( this . _config . multiple && this . _config . selectionType === 'counter' ) {
@@ -539,24 +559,36 @@ class Select {
539
559
}
540
560
541
561
_updateSearch ( ) {
542
- if ( this . _selection . length > 0 && ! this . _config . multiple ) {
562
+ if ( this . _selection . length === 0 && ( this . _config . selectionType === 'tags' || this . _config . selectionType === 'text' ) ) {
563
+ this . _searchElement . removeAttribute ( 'size' )
564
+ }
565
+
566
+ if ( this . _selection . length > 0 && ! this . _config . multiple && ! this . _config . inline ) {
543
567
this . _searchElement . placeholder = this . _selection [ 0 ] . text
544
568
this . _selectionElement . style . display = 'none'
569
+ return
545
570
}
546
571
547
- if ( this . _selection . length > 0 && this . _config . multiple && this . _config . selectionType !== 'counter' ) {
572
+ if ( this . _selection . length > 0 && this . _config . multiple && this . _config . selectionType !== 'counter' && ! this . _config . inline ) {
548
573
this . _searchElement . placeholder = ''
549
- this . _selectionElement . style . display = 'initial'
574
+ this . _selectionElement . style . removeProperty ( 'display' )
575
+ return
550
576
}
551
577
552
- if ( this . _selection . length === 0 && this . _config . multiple ) {
578
+ if ( this . _selection . length === 0 && this . _config . multiple && ! this . _config . inline ) {
553
579
this . _searchElement . placeholder = this . _config . searchPlaceholder
554
- this . _selectionElement . style . display = 'initial'
580
+ this . _selectionElement . style . display = 'none'
581
+ return
555
582
}
556
583
557
- if ( this . _config . multiple && this . _config . selectionType === 'counter' ) {
584
+ if ( this . _config . multiple && this . _config . selectionType === 'counter' && ! this . _config . inline ) {
558
585
this . _searchElement . placeholder = `${ this . _selection . length } item(s) selected`
559
586
this . _selectionElement . style . display = 'none'
587
+ return
588
+ }
589
+
590
+ if ( this . _config . inline ) {
591
+ this . _searchElement . placeholder = this . _config . searchPlaceholder
560
592
}
561
593
}
562
594
@@ -600,8 +632,18 @@ class Select {
600
632
// }
601
633
602
634
_onSearchChange ( element ) {
603
- if ( element )
635
+ if ( element ) {
604
636
this . search ( element . value )
637
+
638
+ if ( this . _selection . length > 0 && ( this . _config . selectionType === 'tags' || this . _config . selectionType === 'text' ) ) {
639
+ element . size = element . value . length + 1
640
+ return
641
+ }
642
+
643
+ if ( this . _selection . length === 0 && ( this . _config . selectionType === 'tags' || this . _config . selectionType === 'text' ) ) {
644
+ element . removeAttribute ( 'size' )
645
+ }
646
+ }
605
647
}
606
648
607
649
_updateOptionsList ( ) {
@@ -635,14 +677,14 @@ class Select {
635
677
if ( option . textContent . toLowerCase ( ) . indexOf ( this . _search ) === - 1 ) {
636
678
option . style . display = 'none'
637
679
} else {
638
- option . style . display = 'block'
680
+ option . style . removeProperty ( ' display' )
639
681
visibleOptions ++
640
682
}
641
683
642
684
const optgroup = option . closest ( SELECTOR_OPTGROUP )
643
685
if ( optgroup ) {
644
686
if ( SelectorEngine . children ( optgroup , SELECTOR_OPTION ) . filter ( element => this . _isVisible ( element ) ) . length > 0 ) {
645
- optgroup . style . display = 'block'
687
+ optgroup . style . removeProperty ( ' display' )
646
688
} else {
647
689
optgroup . style . display = 'none'
648
690
}
@@ -700,7 +742,7 @@ class Select {
700
742
return
701
743
}
702
744
703
- const selects = SelectorEngine . find ( SELECTOR_MULTI_SELECT )
745
+ const selects = SelectorEngine . find ( SELECTOR_SELECT )
704
746
705
747
for ( let i = 0 , len = selects . length ; i < len ; i ++ ) {
706
748
const context = Data . getData ( selects [ i ] , DATA_KEY )
0 commit comments