Added method On

This commit is contained in:
MaxOvs19 2023-01-30 17:57:13 +03:00
parent 1c5ee130f9
commit 63d5f0e35f
2 changed files with 32 additions and 0 deletions

View File

@ -25,6 +25,11 @@ const dropdown = new CGSelect({
width: '824px',
},
},
// multiselect: true,
});
dropdown.on('select', function (e) {
console.log(`this state: ${e}`);
});
// ------------------------------NativeSelect-----------------------

View File

@ -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;
}
}
}