Added methods in class(7/8)

This commit is contained in:
Макс Овсяников 2022-09-23 19:48:39 +03:00
parent bf0eb08cc6
commit 565f80280d
2 changed files with 46 additions and 10 deletions

View File

@ -32,7 +32,7 @@ export class DropDown {
this.#caret = this.#element.querySelector('.caret'); this.#caret = this.#element.querySelector('.caret');
} }
#initSelected() { #initSelected(select) {
const { styles } = this.#options; const { styles } = this.#options;
if (this.#options.selected) { if (this.#options.selected) {
@ -52,6 +52,10 @@ export class DropDown {
this.#createSelected('Select...'); this.#createSelected('Select...');
this.#customStyles(styles); this.#customStyles(styles);
} }
if (select) {
this.#createSelected(select);
}
} }
#initAmount() { #initAmount() {
@ -69,18 +73,22 @@ export class DropDown {
this.#element.innerHTML += `<ul class="list">${templete}</ul>`; this.#element.innerHTML += `<ul class="list">${templete}</ul>`;
} }
#render() { #render(select) {
const { items, styles } = this.#options; const { items, styles } = this.#options;
this.#initSelected(); if (select || (select && styles)) {
this.#element.querySelector(this.#options.selector); this.#initSelected(select);
this.#customStyles(styles);
} else {
this.#initSelected();
}
this.#element.querySelector(this.#options.selector);
let ul = document.createElement('ul'); let ul = document.createElement('ul');
if (styles) { if (styles) {
const { list } = styles; const { list } = styles;
console.log(styles);
ul.classList.add('list'); ul.classList.add('list');
if (ul) { if (ul) {
@ -141,12 +149,8 @@ export class DropDown {
return; return;
} }
this.#list = this.#element.querySelectorAll('.list');
this.#caret = this.#element.querySelector('.caret');
if (event) { if (event) {
let list = this.#element.querySelectorAll('.list'); let list = this.#element.querySelectorAll('.list');
console.log(list);
if (event === 'mouseenter') { if (event === 'mouseenter') {
this.#element.addEventListener(event, () => { this.#element.addEventListener(event, () => {
@ -220,4 +224,31 @@ export class DropDown {
items.push(item); items.push(item);
this.#render(); this.#render();
} }
deleteItem(item) {
const { items } = this.#options;
let index = items.indexOf(item);
items.splice(index, 1);
this.#render();
}
deleteItemAll() {
const { items } = this.#options;
items.splice(0, items.length);
console.log(items);
this.#render();
}
selectIndex(index) {
const options = this.#element.querySelectorAll('.list__item');
let select = options[index].innerText;
this.#render(select);
}
} }

View File

@ -10,7 +10,7 @@ dropdown.addItem('ZAZ');
const dropdown2 = new DropDown({ const dropdown2 = new DropDown({
selector: '.cg-dropdown2', selector: '.cg-dropdown2',
placeholder: 'Выберите авто', selected: '...',
items: ['BMW', 'Opel', 'Mersedes', 'MAN', 'Kamaz'], items: ['BMW', 'Opel', 'Mersedes', 'MAN', 'Kamaz'],
event: 'mouseenter', event: 'mouseenter',
styles: { styles: {
@ -34,6 +34,11 @@ const dropdown2 = new DropDown({
}); });
dropdown2.addItem('LADA'); dropdown2.addItem('LADA');
dropdown2.selectIndex(3);
// dropdown2.deleteItemAll();
// dropdown2.deleteItem('MAN');
// const dropdown3 = new DropDown({ // const dropdown3 = new DropDown({
// selector: '.cg-dropdown3', // selector: '.cg-dropdown3',