From 63d5f0e35f7f235e34dcc3d0fbac8286aa052bed Mon Sep 17 00:00:00 2001 From: MaxOvs19 Date: Mon, 30 Jan 2023 17:57:13 +0300 Subject: [PATCH] Added method On --- example/index.js | 5 +++++ src/cg-select.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/example/index.js b/example/index.js index 55d9aaa..b964327 100644 --- a/example/index.js +++ b/example/index.js @@ -25,6 +25,11 @@ const dropdown = new CGSelect({ width: '824px', }, }, + // multiselect: true, +}); + +dropdown.on('select', function (e) { + console.log(`this state: ${e}`); }); // ------------------------------NativeSelect----------------------- diff --git a/src/cg-select.ts b/src/cg-select.ts index 371685d..9b250a3 100644 --- a/src/cg-select.ts +++ b/src/cg-select.ts @@ -914,4 +914,31 @@ export class CGSelect implements ICgSelect { const select: string = options[index].innerText; this.render(select); } + + public on(state: string, callback: (state: any) => any) { + const options = this.element?.querySelectorAll('.list__item'); + + switch (state) { + case 'select': + options?.forEach((option: Element) => { + option.addEventListener('click', () => { + console.log('option:select', option.textContent); + }); + }); + callback(state); + break; + case 'close': + this.element!.addEventListener('click', () => { + console.log('list:close', this.list!.classList.contains('close')); + }); + callback(state); + break; + case 'open': + this.element!.addEventListener('click', () => { + console.log('list:open', this.list!.classList.contains('open')); + }); + callback(state); + break; + } + } }