diff --git a/src/cg-dropdown.js b/src/cg-dropdown.js index fe38e67..b8d0a6b 100644 --- a/src/cg-dropdown.js +++ b/src/cg-dropdown.js @@ -5,6 +5,7 @@ import { customStylesFormat, nativOptionMultiple, nativOptionOrdinary, + createSelectedSearch, } from './components/utils'; import { createBreadcrumb, @@ -311,8 +312,9 @@ export class DropDown { * @protected */ #initSelected(select) { - const { styles, selected, placeholder } = this.#options; + const { styles, selected, placeholder, searchMode } = this.#options; + console.log(searchMode); if (selected) { createSelected(this.#element, selected); } else if (placeholder) { @@ -321,6 +323,9 @@ export class DropDown { createSelected(this.#element, 'Select...'); } + if (searchMode) { + createSelectedSearch(this.#element); + } if (styles) { customStyles(this.#element, styles); } @@ -515,7 +520,7 @@ export class DropDown { const options = this.#element.querySelectorAll('.list__item'); const select = this.#element.querySelector('.selected'); const nativOption = this.#element.querySelectorAll('.nativSelect__nativOption'); - + const search = this.#element.querySelector('#searchSelect'); const ulMultipul = document.createElement('ul'); if (multiselect) { @@ -523,6 +528,9 @@ export class DropDown { select.classList.add('overflow-hidden'); } + // console.log(search); + this.searchMode(); + options.forEach((option, index) => { option.addEventListener('click', (event) => { const item = this.#items[index]; @@ -603,6 +611,27 @@ export class DropDown { }); } + searchMode() { + document.querySelector('#searchSelect').oninput = function () { + let val = this.value.trim(); + let searchSelect = document.querySelectorAll('.list__item'); + console.log(searchSelect); + + if (val != '') { + searchSelect.forEach((elem) => { + if (elem.innerText.search(val) == -1) { + elem.classList.add('displayHide'); + } else { + elem.classList.remove('displayHide'); + } + }); + } else { + searchSelect.forEach((elem) => { + elem.classList.remove('displayHide'); + }); + } + }; + } /** * Приватный метод экземпляра класса DropDown * @protected diff --git a/src/components/utils.js b/src/components/utils.js index 7a3058e..889b2c9 100644 --- a/src/components/utils.js +++ b/src/components/utils.js @@ -31,6 +31,23 @@ export function createSelected(element, content, styles) { } } +export function createSelectedSearch(element) { + if (element) { + element.innerHTML = ` +