Added function closeOnSelect and closed in click for window
This commit is contained in:
parent
d1f87a6a13
commit
b9161d56c8
@ -89,6 +89,8 @@ export class DropDown {
|
|||||||
selected: 'Выбранный элемент',
|
selected: 'Выбранный элемент',
|
||||||
placeholder: '...',
|
placeholder: '...',
|
||||||
items: [string|number|object],
|
items: [string|number|object],
|
||||||
|
darkTheme: true/false,
|
||||||
|
closeOnSelect: true/false,
|
||||||
styles: {
|
styles: {
|
||||||
head: {
|
head: {
|
||||||
background: '...',
|
background: '...',
|
||||||
@ -97,6 +99,7 @@ export class DropDown {
|
|||||||
chips: {...},
|
chips: {...},
|
||||||
caret: {...},
|
caret: {...},
|
||||||
placeholder: {...},
|
placeholder: {...},
|
||||||
|
lable: {..},
|
||||||
},
|
},
|
||||||
event: '...',
|
event: '...',
|
||||||
url: 'http/...',
|
url: 'http/...',
|
||||||
@ -109,6 +112,7 @@ export class DropDown {
|
|||||||
this.#init(options);
|
this.#init(options);
|
||||||
this.#render();
|
this.#render();
|
||||||
this.#initEvent();
|
this.#initEvent();
|
||||||
|
this.#closeSelectClick();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -426,21 +430,28 @@ export class DropDown {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (darkTheme == false) {
|
if (darkTheme == false) {
|
||||||
this.checkTheme();
|
this.#checkTheme();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.#list = this.#element.querySelector('.list');
|
||||||
|
this.#caret = this.#element.querySelector('.caret');
|
||||||
|
|
||||||
this.#addOptionsBehaviour();
|
this.#addOptionsBehaviour();
|
||||||
}
|
}
|
||||||
|
|
||||||
checkTheme() {
|
/**
|
||||||
|
* Приватный метод рендера экземпляра класса DropDown
|
||||||
|
* @protected
|
||||||
|
* @method #checkTheme
|
||||||
|
* @description Изменяет цветовую схему с темной на светлую.
|
||||||
|
*/
|
||||||
|
#checkTheme() {
|
||||||
const { darkTheme } = this.#options;
|
const { darkTheme } = this.#options;
|
||||||
|
|
||||||
const select = this.#element.querySelector('.cg-select');
|
const select = this.#element.querySelector('.cg-select');
|
||||||
const caret = this.#element.querySelector('.caret');
|
const caret = this.#element.querySelector('.caret');
|
||||||
const list = this.#element.querySelector('ul.list');
|
const list = this.#element.querySelector('ul.list');
|
||||||
const listItem = this.#element.querySelectorAll('.list__item');
|
|
||||||
|
|
||||||
console.log(list);
|
|
||||||
if (darkTheme == false) {
|
if (darkTheme == false) {
|
||||||
select.classList.add('selectWhite');
|
select.classList.add('selectWhite');
|
||||||
caret.classList.add('caretWhite');
|
caret.classList.add('caretWhite');
|
||||||
@ -530,9 +541,6 @@ export class DropDown {
|
|||||||
* @method #open
|
* @method #open
|
||||||
*/
|
*/
|
||||||
#open(oneClick) {
|
#open(oneClick) {
|
||||||
this.#list = this.#element.querySelector('.list');
|
|
||||||
this.#caret = this.#element.querySelector('.caret');
|
|
||||||
|
|
||||||
if (oneClick === true) {
|
if (oneClick === true) {
|
||||||
this.#list.classList.add('open');
|
this.#list.classList.add('open');
|
||||||
this.#caret.classList.add('caret_rotate');
|
this.#caret.classList.add('caret_rotate');
|
||||||
@ -560,11 +568,13 @@ export class DropDown {
|
|||||||
* @method #addOptionsBehaviour
|
* @method #addOptionsBehaviour
|
||||||
*/
|
*/
|
||||||
#addOptionsBehaviour() {
|
#addOptionsBehaviour() {
|
||||||
const { multiselect, placeholder, selected, multiselectTag, searchMode } = this.#options;
|
const { multiselect, placeholder, selected, multiselectTag, searchMode, closeOnSelect } =
|
||||||
|
this.#options;
|
||||||
|
|
||||||
const options = this.#element.querySelectorAll('.list__item');
|
const options = this.#element.querySelectorAll('.list__item');
|
||||||
const select = this.#element.querySelector('.selected');
|
const select = this.#element.querySelector('.selected');
|
||||||
const nativOption = this.#element.querySelectorAll('.nativSelect__nativOption');
|
const nativOption = this.#element.querySelectorAll('.nativSelect__nativOption');
|
||||||
|
|
||||||
const ulMultipul = document.createElement('ul');
|
const ulMultipul = document.createElement('ul');
|
||||||
|
|
||||||
if (multiselect && multiselect == true) {
|
if (multiselect && multiselect == true) {
|
||||||
@ -580,11 +590,13 @@ export class DropDown {
|
|||||||
option.addEventListener('click', (event) => {
|
option.addEventListener('click', (event) => {
|
||||||
const item = this.#items[index];
|
const item = this.#items[index];
|
||||||
|
|
||||||
if (multiselect && multiselect == true) {
|
if (closeOnSelect == false || (multiselect && multiselect == true)) {
|
||||||
event.preventDefault();
|
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
option.classList.toggle('active');
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (multiselect && multiselect == true) {
|
||||||
|
option.classList.toggle('active');
|
||||||
const checkBox = option.querySelector('input[type="checkbox"]');
|
const checkBox = option.querySelector('input[type="checkbox"]');
|
||||||
|
|
||||||
if (checkBox) {
|
if (checkBox) {
|
||||||
@ -721,4 +733,21 @@ export class DropDown {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Приватный метод экземпляра класса DropDown
|
||||||
|
* @protected
|
||||||
|
* @description Закрывает список по клику вне элемента
|
||||||
|
* @method #closeSelectClick
|
||||||
|
*/
|
||||||
|
#closeSelectClick() {
|
||||||
|
const dropdown = document.querySelector(`${this.#options.selector}`);
|
||||||
|
|
||||||
|
document.addEventListener('click', (e) => {
|
||||||
|
const withinBoundaries = e.composedPath().includes(dropdown);
|
||||||
|
if (!withinBoundaries) {
|
||||||
|
this.#close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ const dropdown = new DropDown({
|
|||||||
placeholder: 'Выберите авто',
|
placeholder: 'Выберите авто',
|
||||||
lable: 'Выбор лучшего авто!',
|
lable: 'Выбор лучшего авто!',
|
||||||
darkTheme: false,
|
darkTheme: false,
|
||||||
|
closeOnSelect: false,
|
||||||
items: [
|
items: [
|
||||||
'BMW',
|
'BMW',
|
||||||
{
|
{
|
||||||
@ -26,8 +27,8 @@ const dropdown = new DropDown({
|
|||||||
borderRadius: '5px',
|
borderRadius: '5px',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
multiselect: true,
|
// multiselect: true,
|
||||||
multiselectTag: true,
|
// multiselectTag: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// ------------------------------URL--------------------
|
// ------------------------------URL--------------------
|
||||||
@ -42,8 +43,8 @@ const dropdown3 = new DropDown({
|
|||||||
width: '350px',
|
width: '350px',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
multiselect: true,
|
// multiselect: true,
|
||||||
multiselectTag: true,
|
// multiselectTag: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// --------------------------------Категории--------------------------
|
// --------------------------------Категории--------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user