Skip to content

Commit 7b3b080

Browse files
committed
refactor: improvements and new features
1 parent 39ebe26 commit 7b3b080

File tree

2 files changed

+165
-210
lines changed

2 files changed

+165
-210
lines changed

js/src/select.js

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ const TAB_KEY = 'Tab'
3030
const RIGHT_MOUSE_BUTTON = 2
3131

3232
const SELECTOR_INPUT = '.c-select-search'
33-
const SELECTOR_LIST = '.c-select-options'
34-
const SELECTOR_MULTI_SELECT = '.c-select'
3533
const SELECTOR_OPTGROUP = '.c-select-optgroup'
3634
const SELECTOR_OPTION = '.c-select-option'
3735
const SELECTOR_OPTIONS = '.c-select-options'
3836
const SELECTOR_OPTIONS_EMPTY = '.c-select-options-empty'
37+
const SELECTOR_SELECT = '.c-select'
3938
const SELECTOR_SELECTED = '.c-selected'
4039
const SELECTOR_SELECTION = '.c-select-selection'
41-
const SELECTOR_TAGS = '.c-select-tags'
4240

4341
const EVENT_CHANGE = `keyup${EVENT_KEY}`
4442
const EVENT_CLICK = `click${EVENT_KEY}`
@@ -50,6 +48,7 @@ const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
5048
const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`
5149

5250
const CLASS_NAME_SELECT = 'c-select'
51+
const CLASS_NAME_SELECT_INLINE = 'c-select-inline'
5352
const CLASS_NAME_SELECT_MULTIPLE = 'c-select-multiple'
5453
const CLASS_NAME_OPTGROUP = 'c-select-optgroup'
5554
const CLASS_NAME_OPTGROUP_LABEL = 'c-select-optgroup-label'
@@ -59,6 +58,7 @@ const CLASS_NAME_OPTIONS_EMPTY = 'c-select-options-empty'
5958
const CLASS_NAME_SEARCH = 'c-select-search'
6059
const CLASS_NAME_SELECTED = 'c-selected'
6160
const CLASS_NAME_SELECTION = 'c-select-selection'
61+
const CLASS_NAME_SELECTION_TAGS = 'c-select-selection-tags'
6262
const CLASS_NAME_SHOW = 'c-show'
6363
const CLASS_NAME_TAG = 'c-select-tag'
6464
const CLASS_NAME_TAG_DELETE = 'c-select-tag-delete'
@@ -72,6 +72,7 @@ const Default = {
7272
optionsEmptyPlaceholder: 'no items',
7373
search: false,
7474
searchPlaceholder: 'Select...',
75+
selection: true,
7576
selectionType: 'counter',
7677
selected: []
7778
}
@@ -83,6 +84,7 @@ const DefaultType = {
8384
optionsEmptyPlaceholder: 'string',
8485
search: 'boolean',
8586
searchPlaceholder: 'string',
87+
selection: 'boolean',
8688
selectionType: 'string',
8789
selected: 'array'
8890
}
@@ -315,8 +317,8 @@ class Select {
315317
}
316318

317319
_hideNativeSelect() {
318-
// this._element.tabIndex = '-1'
319-
// this._element.style.display = 'none'
320+
this._element.tabIndex = '-1'
321+
this._element.style.display = 'none'
320322
}
321323

322324
_createSelect() {
@@ -327,9 +329,16 @@ class Select {
327329
div.classList.add(CLASS_NAME_SELECT_MULTIPLE)
328330
}
329331

332+
if (this._config.inline) {
333+
div.classList.add(CLASS_NAME_SELECT_INLINE)
334+
}
335+
330336
this._clone = div
331337
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+
333342
if (this._config.search) {
334343
this._createSearchInput()
335344
this._updateSearch()
@@ -343,6 +352,9 @@ class Select {
343352
_createSelection() {
344353
const span = document.createElement('span')
345354
span.classList.add(CLASS_NAME_SELECTION)
355+
if (this._config.selectionType === 'tags') {
356+
span.classList.add(CLASS_NAME_SELECTION_TAGS)
357+
}
346358
this._clone.append(span)
347359

348360
this._updateSelection()
@@ -365,6 +377,10 @@ class Select {
365377
// input.placeholder = ''
366378
// }
367379

380+
if (this._selection.length > 0 && (this._config.selectionType === 'tags' || this._config.selectionType === 'text')) {
381+
input.size = 2
382+
}
383+
368384
this._clone.append(input)
369385
this._searchElement = input
370386
}
@@ -513,6 +529,10 @@ class Select {
513529
// .c-select-selections
514530

515531
_updateSelection() {
532+
if (this._config.inline && !this._config.selection) {
533+
return
534+
}
535+
516536
const selection = SelectorEngine.findOne(SELECTOR_SELECTION, this._clone)
517537

518538
if (this._config.multiple && this._config.selectionType === 'counter') {
@@ -539,24 +559,36 @@ class Select {
539559
}
540560

541561
_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) {
543567
this._searchElement.placeholder = this._selection[0].text
544568
this._selectionElement.style.display = 'none'
569+
return
545570
}
546571

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) {
548573
this._searchElement.placeholder = ''
549-
this._selectionElement.style.display = 'initial'
574+
this._selectionElement.style.removeProperty('display')
575+
return
550576
}
551577

552-
if (this._selection.length === 0 && this._config.multiple) {
578+
if (this._selection.length === 0 && this._config.multiple && !this._config.inline) {
553579
this._searchElement.placeholder = this._config.searchPlaceholder
554-
this._selectionElement.style.display = 'initial'
580+
this._selectionElement.style.display = 'none'
581+
return
555582
}
556583

557-
if (this._config.multiple && this._config.selectionType === 'counter') {
584+
if (this._config.multiple && this._config.selectionType === 'counter' && !this._config.inline) {
558585
this._searchElement.placeholder = `${this._selection.length} item(s) selected`
559586
this._selectionElement.style.display = 'none'
587+
return
588+
}
589+
590+
if (this._config.inline) {
591+
this._searchElement.placeholder = this._config.searchPlaceholder
560592
}
561593
}
562594

@@ -600,8 +632,18 @@ class Select {
600632
// }
601633

602634
_onSearchChange(element) {
603-
if (element)
635+
if (element) {
604636
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+
}
605647
}
606648

607649
_updateOptionsList() {
@@ -635,14 +677,14 @@ class Select {
635677
if (option.textContent.toLowerCase().indexOf(this._search) === -1) {
636678
option.style.display = 'none'
637679
} else {
638-
option.style.display = 'block'
680+
option.style.removeProperty('display')
639681
visibleOptions++
640682
}
641683

642684
const optgroup = option.closest(SELECTOR_OPTGROUP)
643685
if (optgroup) {
644686
if (SelectorEngine.children(optgroup, SELECTOR_OPTION).filter(element => this._isVisible(element)).length > 0) {
645-
optgroup.style.display = 'block'
687+
optgroup.style.removeProperty('display')
646688
} else {
647689
optgroup.style.display = 'none'
648690
}
@@ -700,7 +742,7 @@ class Select {
700742
return
701743
}
702744

703-
const selects = SelectorEngine.find(SELECTOR_MULTI_SELECT)
745+
const selects = SelectorEngine.find(SELECTOR_SELECT)
704746

705747
for (let i = 0, len = selects.length; i < len; i++) {
706748
const context = Data.getData(selects[i], DATA_KEY)

0 commit comments

Comments
 (0)