Added accessor get for value. Added multiselect option

This commit is contained in:
Макс Овсяников 2022-09-30 16:22:14 +03:00
parent d8b07d2e6c
commit 751e428e91
2 changed files with 67 additions and 12 deletions

View File

@ -4,6 +4,19 @@ export class DropDown {
#options; #options;
#caret; #caret;
#items; #items;
#value;
get value() {
const { multiselect } = this.#options;
if (multiselect) {
return this.#items.filter((item, index) => {
return this.#value.indexOf(index) !== -1;
});
}
return this.#value;
}
constructor(options = {}) { constructor(options = {}) {
this.#init(options); this.#init(options);
@ -60,8 +73,12 @@ export class DropDown {
this.#list = this.#element.querySelector('.list'); this.#list = this.#element.querySelector('.list');
this.#caret = this.#element.querySelector('.caret'); this.#caret = this.#element.querySelector('.caret');
const { items } = this.#options; const { items, multiselect } = this.#options;
this.#items = items; this.#items = items;
if (multiselect) {
this.#value = [];
}
} }
#initSelected(select) { #initSelected(select) {
@ -145,7 +162,7 @@ export class DropDown {
li.classList.add('list__item'); li.classList.add('list__item');
if (item && typeof item === 'object' && item.title) { if (this.#checkItemStruct(item)) {
text = item.title; text = item.title;
} else { } else {
text = item; text = item;
@ -219,8 +236,10 @@ export class DropDown {
const options = this.#element.querySelectorAll('.list__item'); const options = this.#element.querySelectorAll('.list__item');
const selected = this.#element.querySelector('.selected'); const selected = this.#element.querySelector('.selected');
options.forEach((option) => { options.forEach((option, index) => {
option.addEventListener('click', (event) => { option.addEventListener('click', (event) => {
const item = this.#items[index];
if (multiselect) { if (multiselect) {
event.stopPropagation(); event.stopPropagation();
option.classList.toggle('active'); option.classList.toggle('active');
@ -229,14 +248,27 @@ export class DropDown {
if (checkBox) { if (checkBox) {
checkBox.checked = !checkBox.checked; checkBox.checked = !checkBox.checked;
const indexValue = this.#value.indexOf(index);
if (indexValue === -1) {
this.#value.push(index);
} else {
this.#value.splice(indexValue, 1);
}
} }
} else { } else {
selected.innerText = option.innerText; if (this.#checkItemStruct(item)) {
selected.innerText = item.title;
} else {
selected.innerText = item;
}
this.#value = item;
options.forEach((option) => { options.forEach((option) => {
option.classList.remove('active'); option.classList.remove('active');
}); });
option.classList.add('active'); option.classList.add('active');
// this.getValue();
} }
}); });
}); });
@ -317,10 +349,13 @@ export class DropDown {
} }
} }
// getValue() { #checkItemStruct(item) {
// const selected = this.#element.querySelector('.selected'); if (item && typeof item !== 'object') {
// const value = selected.innerText; return false;
// console.log(value); }
// return value;
// } return (
item.hasOwnProperty('id') && item.hasOwnProperty('title') && item.hasOwnProperty('value')
);
}
} }

View File

@ -13,8 +13,28 @@ dropdown.addItem('LADA');
const dropdown2 = new DropDown({ const dropdown2 = new DropDown({
selector: '.cg-dropdown2', selector: '.cg-dropdown2',
placeholder: 'SELECT CAR', placeholder: 'SELECT CAR',
items: ['BMW', 'Opel', 'Mersedes', 'MAN', 'Kamaz'], items: [
{
id: 'addaw21',
title: 'BMW',
value: '1',
},
{
id: '2414q',
title: 'Opel',
value: '2',
},
{
id: '24qwds',
title: 'Kamaz',
value: '3',
},
],
}); });
setTimeout(() => {
console.log(dropdown.value);
}, 10000);
// let a = dropdown2.getValue(); // let a = dropdown2.getValue();
// console.log(a); // console.log(a);
//ToDo: paste the desired url; //ToDo: paste the desired url;