Skip to content

Instantly share code, notes, and snippets.

@alanschiobairesdev
Last active January 18, 2024 18:04
Show Gist options
  • Save alanschiobairesdev/478c33f834f74c0e559556708f2cb3be to your computer and use it in GitHub Desktop.
Save alanschiobairesdev/478c33f834f74c0e559556708f2cb3be to your computer and use it in GitHub Desktop.
select file
/**
* DropdownSelect - handle the select as dropdownlist
* @param {string} element -
*/
export class DropdownSelect {
constructor (element) {
this.element = element;
}
/**
* Get the dropdown List element (not the items)
*/
getDropdownSelectWrapElement() {
return this.element.siblings(`.list`).first();
}
/**
* Callback to be used on `selectOption` after the element found to click on a options selected
* @param {HTMLElement} element select option's HTML Element
* @private
*/
onElementFound(element) {
element.click();
}
/**
* Click on the element to open the dropdown list
* @private
*/
openDropdown() {
this.element.click();
}
/**
* Select an quantity
* @param {string} value - value to be selected on the dropdown
* @example
* const select = new Select(Element.get(selector))
* select.selectOption('341');
* select.selectOption('1');
*/
selectOption(value) {
this.openDropdown();
this.getDropdownSelectWrapElement()
.find(' ul li')
.then((elements) => {
const elementWithValue = [...elements].find((element) => {
return element.children[0].textContent === value;
});
elementWithValue && this.onElementFound(elementWithValue);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment