From d7ae149d05176ae62b7ed88d8717fcb01beced9a Mon Sep 17 00:00:00 2001 From: MaxOvs Date: Mon, 26 Sep 2022 19:45:12 +0300 Subject: [PATCH] Fixed methods in conv(DRY) --- src/cg-dropdown.js | 24 ++++++++++-------------- src/index.js | 4 ++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/cg-dropdown.js b/src/cg-dropdown.js index c433af0..5cb6a0c 100644 --- a/src/cg-dropdown.js +++ b/src/cg-dropdown.js @@ -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]; } } diff --git a/src/index.js b/src/index.js index dea89ed..075ae4b 100644 --- a/src/index.js +++ b/src/index.js @@ -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({