Working as multiselectTag

This commit is contained in:
Макс Овсяников 2022-10-03 21:43:22 +03:00
parent 1cc688a50f
commit 72066a9c88
3 changed files with 75 additions and 10 deletions

View File

@ -207,6 +207,10 @@ export class DropDown {
const li = document.createElement('li'); const li = document.createElement('li');
const text = document.createTextNode(item.title); const text = document.createTextNode(item.title);
const checkBox = document.createElement('input');
checkBox.type = 'checkbox';
li.appendChild(checkBox);
li.classList.add('list__item'); li.classList.add('list__item');
li.appendChild(text); li.appendChild(text);
ul.appendChild(li); ul.appendChild(li);
@ -231,15 +235,34 @@ export class DropDown {
} }
#addOptionsBehaviour() { #addOptionsBehaviour() {
const { multiselect, placeholder } = this.#options; const { multiselect, placeholder, multiselectTag } = 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');
const ul = document.createElement('ul');
options.forEach((option, index) => { options.forEach((option, index) => {
option.addEventListener('click', (event) => { option.addEventListener('click', (event) => {
const item = this.#items[index]; const item = this.#items[index];
const li = document.createElement('li');
const btn = document.createElement('button');
const text = document.createTextNode('X');
// let textLi = document.createTextNode(item);
// t.toString();
// console.log(textLi);
btn.appendChild(text);
btn.addEventListener('click', () => {
console.log('aaaa');
ul.removeChild(li);
});
ul.classList.add('multiselectTag');
console.log(ul);
if (multiselect) { if (multiselect) {
event.stopPropagation(); event.stopPropagation();
option.classList.toggle('active'); option.classList.toggle('active');
@ -255,8 +278,23 @@ export class DropDown {
if (checkIndex === -1) { if (checkIndex === -1) {
this.#indexes.push(index); this.#indexes.push(index);
this.#value.push(item);
selected.innerText = this.#value; if (this.#checkItemStruct(item)) {
this.#value.push(item.title);
} else {
let textLi = document.createTextNode(item);
li.appendChild(textLi);
li.appendChild(btn);
ul.appendChild(li);
this.#value.push(item);
}
// selected.innerText = this.#value;
selected.innerText = '';
selected.appendChild(ul);
return; return;
} }

View File

@ -5,8 +5,7 @@ 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) => {}, multiselectTag: true,
itemRemoved: (item, index) => {},
}); });
dropdown.addItem('ZAZ'); dropdown.addItem('ZAZ');
@ -28,26 +27,37 @@ const dropdown2 = new DropDown({
}, },
{ {
id: '24qwds', id: '24qwds',
title: 'Kamaz', title: 'Kamaz 258',
value: '3', value: '3',
}, },
{
id: '28wds',
title: 'MAN',
value: '4',
},
{
id: '28qwds',
title: 'BOOT',
value: '5',
},
], ],
multiselect: true,
}); });
setTimeout(() => { setTimeout(() => {
console.log(dropdown.value); console.log(dropdown2.value);
}, 10000); }, 10000);
setTimeout(() => { setTimeout(() => {
console.log(dropdown.indexes); console.log(dropdown2.indexes);
}, 10000); }, 10000);
// let a = dropdown2.getValue();
// console.log(a);
//ToDo: paste the desired url; //ToDo: paste the desired url;
const dropdown3 = new DropDown({ const dropdown3 = new DropDown({
selector: '.cg-dropdown3', selector: '.cg-dropdown3',
placeholder: 'URL', placeholder: 'URL',
url: 'http://jsonplaceholder.typicode.com/users', url: 'http://jsonplaceholder.typicode.com/users',
multiselect: true,
}); });
// const dropdown3 = new DropDown({ // const dropdown3 = new DropDown({

View File

@ -21,17 +21,26 @@ body {
.cg-select { .cg-select {
padding: 14px; padding: 14px;
min-width: 200px; min-width: 200px;
max-width: 200px;
height: 30px; height: 30px;
color: #fff; color: #fff;
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
overflow: hidden;
text-overflow: ellipsis;
background: #2a2f3b; background: #2a2f3b;
cursor: pointer; cursor: pointer;
border-radius: 5px; border-radius: 5px;
transition: 0.5s; transition: 0.5s;
.selected {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
&:hover { &:hover {
transition: 0.5s; transition: 0.5s;
background: #394050; background: #394050;
@ -119,6 +128,14 @@ body {
z-index: 999; z-index: 999;
} }
.multiselectTag {
display: flex;
text-decoration: none;
flex-direction: row;
height: 15px;
list-style-type: none;
}
input[type='checkbox'] { input[type='checkbox'] {
cursor: pointer; cursor: pointer;
} }