Fixed methods in conv(DRY)

This commit is contained in:
Макс Овсяников 2022-09-26 19:45:12 +03:00
parent f106116b8b
commit d7ae149d05
2 changed files with 14 additions and 14 deletions

View File

@ -3,6 +3,7 @@ export class DropDown {
#list;
#options;
#caret;
#items;
constructor(options = {}) {
this.#init(options);
@ -28,6 +29,9 @@ export class DropDown {
this.#list = this.#element.querySelector('.list');
this.#caret = this.#element.querySelector('.caret');
const { items } = this.#options;
this.#items = items;
}
#initSelected(select) {
@ -224,28 +228,22 @@ export class DropDown {
}
addItem(item) {
const { items } = this.#options;
items.push(item);
this.#items.push(item);
this.#render();
}
deleteItem(item) {
const { items } = this.#options;
let index = this.#items.indexOf(item);
let index = items.indexOf(item);
items.splice(index, 1);
this.#items.splice(index, 1);
this.#render();
}
deleteItemAll() {
const { items } = this.#options;
this.#items.splice(0, this.#items.length);
items.splice(0, items.length);
console.log(items);
console.log(this.#items);
this.#render();
}
@ -258,8 +256,6 @@ export class DropDown {
}
getElement(number) {
const { items } = this.#options;
return items[number];
return this.#items[number];
}
}

View File

@ -7,6 +7,7 @@ const dropdown = new DropDown({
});
dropdown.addItem('ZAZ');
dropdown.addItem('LADA');
const dropdown2 = new DropDown({
selector: '.cg-dropdown2',
@ -34,6 +35,9 @@ const dropdown2 = new DropDown({
});
dropdown2.addItem('LADA');
// dropdown.deleteItemAll();
// dropdown2.deleteItem('MAN');
// dropdown2.selectIndex(3);
const dropdown3 = new DropDown({