@ebay/ebayui-core
Version:
Collection of core eBay components; considered to be the building blocks for all composite structures, pages & apps.
95 lines (94 loc) • 3.1 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const makeup_expander_1 = __importDefault(require("makeup-expander"));
const dropdown_1 = require("../../common/dropdown");
class ListboxButton {
handleExpand() {
this.getComponent("options").elementScroll();
this.dropdownUtil.show();
this.emit("expand");
}
handleCollapse() {
this.getEl("button").focus();
this.dropdownUtil.hide();
this.emit("collapse");
}
handleListboxChange(event) {
if (this.input.collapseOnSelect !== false) {
this._expander.expanded = false;
}
const selectedIndex = event.index;
this.state.selectedIndex = selectedIndex;
this.emit("change", event);
}
handleListboxEscape() {
this._expander.expanded = false;
}
onCreate() {
this.state = {
selectedIndex: -1,
};
}
onInput(input) {
input.option = input.option || [];
this.state.selectedIndex = -1;
let i = 0;
for (const option of input.option || []) {
if (option.selected) {
this.state.selectedIndex = i;
break;
}
i++;
}
}
onMount() {
this._setupMakeup();
}
onUpdate() {
this._setupMakeup();
}
onRender() {
if (typeof window !== "undefined") {
this._cleanupMakeup();
}
}
onDestroy() {
this._cleanupMakeup();
}
_setupMakeup() {
var _a;
const { input } = this;
// This `as any` is here for while `options` is coerced into an array from `marko-tag.json`.
// After we move to the full `iterator` we can switch to `if (input.options && !input.disabled)`
if (((_a = input.option) === null || _a === void 0 ? void 0 : _a.length) && !input.disabled) {
const container = this.getEl("container");
this._expander = new makeup_expander_1.default(container, {
alwaysDoFocusManagement: true,
autoCollapse: true,
expandOnClick: true,
simulateSpacebarClick: true,
contentSelector: ".listbox-button__listbox",
hostSelector: ".listbox-button__control",
expandedClass: "listbox-button--expanded",
focusManagement: "content",
collapseOnHostReFocus: true,
});
}
this.dropdownUtil = new dropdown_1.DropdownUtil(this.getEl("button"), this.getEl("options"), {
strategy: input.strategy,
flip: input.flip,
});
}
_cleanupMakeup() {
var _a;
if (this._expander) {
this._expander.destroy();
this._expander = undefined;
}
(_a = this.dropdownUtil) === null || _a === void 0 ? void 0 : _a.cleanup();
}
}
module.exports = ListboxButton;