Skip to content

Commit 65389be

Browse files
committed
feat: add cleaner
1 parent f133c0b commit 65389be

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

js/src/select.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const SELECTOR_OPTIONS_EMPTY = '.c-select-options-empty'
3737
const SELECTOR_SELECT = '.c-select'
3838
const SELECTOR_SELECTED = '.c-selected'
3939
const SELECTOR_SELECTION = '.c-select-selection'
40+
const SELECTOR_SELECTION_CLEANER = '.c-select-selection-cleaner'
4041

4142
const EVENT_CHANGED = `changed${EVENT_KEY}`
4243
const EVENT_CLICK = `click${EVENT_KEY}`
@@ -61,6 +62,7 @@ const CLASS_NAME_OPTIONS_EMPTY = 'c-select-options-empty'
6162
const CLASS_NAME_SEARCH = 'c-select-search'
6263
const CLASS_NAME_SELECTED = 'c-selected'
6364
const CLASS_NAME_SELECTION = 'c-select-selection'
65+
const CLASS_NAME_SELECTION_CLEANER = 'c-select-selection-cleaner'
6466
const CLASS_NAME_SELECTION_TAGS = 'c-select-selection-tags'
6567
const CLASS_NAME_SHOW = 'c-show'
6668
const CLASS_NAME_TAG = 'c-select-tag'
@@ -104,6 +106,7 @@ class Select {
104106
constructor(element, config) {
105107
this._element = element
106108
this._selectionElement = null
109+
this._selectionCleanerElement = null
107110
this._searchElement = null
108111
this._optionsElement = null
109112
this._config = this._getConfig(config)
@@ -167,13 +170,6 @@ class Select {
167170
}
168171

169172
search(text) {
170-
// const rootElement = this._optionsElement
171-
// const customEvent = this._triggerSearchEvent(rootElement)
172-
173-
// if (customEvent === null || customEvent.defaultPrevented) {
174-
// return
175-
// }
176-
177173
this._search = text.length > 0 ? text.toLowerCase() : text
178174
this._filterOptionsList()
179175
EventHandler.trigger(this._element, EVENT_SEARCH)
@@ -197,12 +193,23 @@ class Select {
197193
this._selectionDeleteLast()
198194
}
199195
})
196+
200197
EventHandler.on(this._optionsElement, EVENT_CLICK, event => {
201198
event.preventDefault()
202199
event.stopPropagation()
203200
this._onOptionsClick(event.target)
204201
})
205202

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+
206213
EventHandler.on(this._optionsElement, EVENT_KEYDOWN, event => {
207214
const key = event.keyCode || event.charCode
208215

@@ -347,6 +354,7 @@ class Select {
347354
this._element.parentNode.insertBefore(div, this._element.nextSibling)
348355
if (!this._config.inline || (this._config.inline && this._config.selection)) {
349356
this._createSelection()
357+
// this._createSelectionCleaner()
350358
}
351359

352360
if (this._config.search) {
@@ -368,13 +376,20 @@ class Select {
368376
this._selectionElement = span
369377
}
370378

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+
371389
_createSearchInput() {
372390
const input = document.createElement('input')
373391
input.classList.add(CLASS_NAME_SEARCH)
374392

375-
// if (!this._config.inline && this._selection.length > 0 && (this._config.selectionType === 'tags' || this._config.selectionType === 'text')) {
376-
// input.size = 2
377-
// }
378393
this._searchElement = input
379394
this._updateSearchSize()
380395

@@ -460,6 +475,7 @@ class Select {
460475
}
461476

462477
this._updateSelection()
478+
// this._updateSelectionCleaner()
463479
this._updateSearch()
464480
this._updateSearchSize()
465481
}
@@ -495,6 +511,7 @@ class Select {
495511
const last = this._selection.pop()
496512
this._selectionDelete(last.value)
497513
this._updateSelection()
514+
// this._updateSelectionCleaner()
498515
this._updateSearch()
499516
}
500517
}
@@ -530,6 +547,21 @@ class Select {
530547
}
531548
}
532549

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+
533565
_updateSearch() {
534566
if (!this._config.search) {
535567
return
@@ -614,10 +646,6 @@ class Select {
614646
SelectorEngine.find(SELECTOR_SELECTED, this._clone).forEach(element => {
615647
element.classList.remove(CLASS_NAME_SELECTED)
616648
})
617-
618-
// EventHandler.trigger(this._element, EVENT_CHANGED, {
619-
// value: this._selection
620-
// })
621649
}
622650

623651
// TODO: poprawić tą nazwę

scss/_select.scss

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,41 @@
6060
padding: 0;
6161
}
6262

63+
.c-select-selection-cleaner {
64+
position: absolute;
65+
top: 50%;
66+
right: 1.5rem;
67+
display: inline-flex;
68+
align-items: center;
69+
justify-content: center;
70+
width: 2rem;
71+
height: 2rem;
72+
font-size: 1.5rem;
73+
font-weight: 700;
74+
cursor: pointer;
75+
opacity: .5;
76+
transform: translateY(-50%);
77+
78+
@include themes($close-theme-map, $create: parent) {
79+
color: themes-get-value("close-color");
80+
text-shadow: themes-get-value("close-text-shadow");
81+
}
82+
83+
// Override <a>'s hover style
84+
@include hover() {
85+
text-decoration: none;
86+
@include themes($close-theme-map, $create: parent) {
87+
color: themes-get-value("close-color");
88+
}
89+
}
90+
91+
&:not(:disabled):not(.disabled) {
92+
@include hover-focus() {
93+
opacity: .75;
94+
}
95+
}
96+
}
97+
6398
.c-select-search {
6499
max-width: 100%;
65100
padding: 0;

0 commit comments

Comments
 (0)