mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-26 17:31:02 +02:00
e6882f3e4b
* Add wrapped element getters + fix some types * Remove comment * Add missing config options to types * Add types to constants * Rename sortFn to sorter * Update PR template * Add refactor to PR template * Add passed element types to constants * Add js doc comments to actions * Add "returns" to js doc comments * Add missing choice prop to type * Add types to store.js * Add jsdoc comments to components * Ignore strict null checks * Move loading action into misc.js * Further type def additions * Rename itemCompare to valueCompare * Update badges * Rename scrollToChoice to scrollToChildElement
62 lines
1.4 KiB
JavaScript
62 lines
1.4 KiB
JavaScript
/**
|
|
* @typedef {import('../../../types/index').Choices.passedElement} passedElement
|
|
* @typedef {import('../../../types/index').Choices.ClassNames} ClassNames
|
|
*/
|
|
|
|
export default class Dropdown {
|
|
/**
|
|
* @param {{
|
|
* element: HTMLElement,
|
|
* type: passedElement['type'],
|
|
* classNames: ClassNames,
|
|
* }} args
|
|
*/
|
|
constructor({ element, type, classNames }) {
|
|
this.element = element;
|
|
this.classNames = classNames;
|
|
this.type = type;
|
|
this.isActive = false;
|
|
}
|
|
|
|
/**
|
|
* Bottom position of dropdown in viewport coordinates
|
|
* @returns {number} Vertical position
|
|
*/
|
|
get distanceFromTopWindow() {
|
|
return this.element.getBoundingClientRect().bottom;
|
|
}
|
|
|
|
/**
|
|
* Find element that matches passed selector
|
|
* @param {string} selector
|
|
* @returns {HTMLElement | null}
|
|
*/
|
|
getChild(selector) {
|
|
return this.element.querySelector(selector);
|
|
}
|
|
|
|
/**
|
|
* Show dropdown to user by adding active state class
|
|
* @returns {this}
|
|
*/
|
|
show() {
|
|
this.element.classList.add(this.classNames.activeState);
|
|
this.element.setAttribute('aria-expanded', 'true');
|
|
this.isActive = true;
|
|
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Hide dropdown from user
|
|
* @returns {this}
|
|
*/
|
|
hide() {
|
|
this.element.classList.remove(this.classNames.activeState);
|
|
this.element.setAttribute('aria-expanded', 'false');
|
|
this.isActive = false;
|
|
|
|
return this;
|
|
}
|
|
}
|