Multiselect refactoring

This commit is contained in:
Макс Овсяников 2022-09-30 22:14:39 +03:00
parent 751e428e91
commit 1cc688a50f
3 changed files with 48 additions and 26 deletions

View File

@ -5,17 +5,14 @@ export class DropDown {
#caret; #caret;
#items; #items;
#value; #value;
#indexes = [];
get value() { get value() {
const { multiselect } = this.#options; return this.#value ?? null;
if (multiselect) {
return this.#items.filter((item, index) => {
return this.#value.indexOf(index) !== -1;
});
} }
return this.#value; get indexes() {
return this.#indexes ?? [];
} }
constructor(options = {}) { constructor(options = {}) {
@ -66,7 +63,6 @@ export class DropDown {
this.#element = elem; this.#element = elem;
this.#element.addEventListener('click', () => { this.#element.addEventListener('click', () => {
this.#selectItem();
this.#open(); this.#open();
}); });
@ -153,6 +149,7 @@ export class DropDown {
if (!items && url) { if (!items && url) {
this.#renderUrl(); this.#renderUrl();
return; return;
} }
@ -170,12 +167,7 @@ export class DropDown {
if (multiselect) { if (multiselect) {
const checkBox = document.createElement('input'); const checkBox = document.createElement('input');
checkBox.type = 'checkbox'; checkBox.type = 'checkbox';
checkBox.addEventListener('click', (event) => {
event.stopPropagation();
});
li.appendChild(checkBox); li.appendChild(checkBox);
} }
@ -184,6 +176,8 @@ export class DropDown {
li.appendChild(textNode); li.appendChild(textNode);
ul.appendChild(li); ul.appendChild(li);
}); });
this.#addOptionsBehaviour();
} }
async #renderUrl() { async #renderUrl() {
@ -201,20 +195,26 @@ export class DropDown {
const data = await response.json(); const data = await response.json();
//ToDO: fix title(item.title!) //ToDO: fix title(item.title!)
data.forEach((item, index) => { this.#items = [];
const items = {
id: item.id, data.forEach((dataItem, index) => {
title: item.name, const item = {
id: dataItem.id,
title: dataItem.name,
value: index, value: index,
}; };
const ul = this.#element.querySelector('.list'); const ul = this.#element.querySelector('.list');
const li = document.createElement('li'); const li = document.createElement('li');
const text = document.createTextNode(items.title); const text = document.createTextNode(item.title);
li.classList.add('list__item'); li.classList.add('list__item');
li.appendChild(text); li.appendChild(text);
ul.appendChild(li); ul.appendChild(li);
this.#items.push(item);
}); });
this.#addOptionsBehaviour();
} }
#open() { #open() {
@ -230,8 +230,8 @@ export class DropDown {
this.#caret.classList.remove('caret_rotate'); this.#caret.classList.remove('caret_rotate');
} }
#selectItem() { #addOptionsBehaviour() {
const { multiselect } = this.#options; const { multiselect, placeholder } = this.#options;
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');
@ -247,13 +247,26 @@ export class DropDown {
const checkBox = option.querySelector('input[type="checkbox"]'); const checkBox = option.querySelector('input[type="checkbox"]');
if (checkBox) { if (checkBox) {
if (!(event.target instanceof HTMLInputElement)) {
checkBox.checked = !checkBox.checked; checkBox.checked = !checkBox.checked;
}
const indexValue = this.#value.indexOf(index); const checkIndex = this.#indexes.indexOf(index);
if (indexValue === -1) {
this.#value.push(index); if (checkIndex === -1) {
this.#indexes.push(index);
this.#value.push(item);
selected.innerText = this.#value;
return;
}
this.#indexes.splice(checkIndex, 1);
this.#value.splice(checkIndex, 1);
if (!this.#value.length) {
selected.innerText = placeholder;
} else { } else {
this.#value.splice(indexValue, 1); selected.innerText = this.#value;
} }
} }
} else { } else {

View File

@ -5,6 +5,8 @@ const dropdown = new DropDown({
placeholder: 'Выберите авто', placeholder: 'Выберите авто',
items: ['BMW', 'Opel', 'Mersedes', 'MAN', 'max'], items: ['BMW', 'Opel', 'Mersedes', 'MAN', 'max'],
multiselect: true, multiselect: true,
itemSelected: (item, index) => {},
itemRemoved: (item, index) => {},
}); });
dropdown.addItem('ZAZ'); dropdown.addItem('ZAZ');
@ -35,6 +37,9 @@ const dropdown2 = new DropDown({
setTimeout(() => { setTimeout(() => {
console.log(dropdown.value); console.log(dropdown.value);
}, 10000); }, 10000);
setTimeout(() => {
console.log(dropdown.indexes);
}, 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;

View File

@ -118,3 +118,7 @@ body {
} }
z-index: 999; z-index: 999;
} }
input[type='checkbox'] {
cursor: pointer;
}