@ebay/ebayui-core
Version:
Collection of core eBay components; considered to be the building blocks for all composite structures, pages & apps.
156 lines (155 loc) • 5.77 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const makeup_active_descendant_1 = require("makeup-active-descendant");
const makeup_typeahead_1 = __importDefault(require("makeup-typeahead"));
const element_scroll_1 = require("../../common/element-scroll");
const eventUtils = __importStar(require("../../common/event-utils"));
const TYPEAHEAD_TIMEOUT_LENGTH = 1300;
class Listbox {
get isAutoSelection() {
return this.input.listSelection === "auto";
}
elementScroll() {
(0, element_scroll_1.scroll)(this.getEls("option")[this.state.selectedIndex]);
}
handleChange(index, wasClicked) {
if (this.state.selectedIndex !== index) {
const option = [...(this.input.option || [])][index];
if (option.disabled) {
return;
}
this.state.selectedIndex = index;
this.once("update", () => {
this.emit("change", {
index,
wasClicked,
selected: [option.value],
el: this.getEls("option")[index],
});
});
}
}
handleClick(index) {
this.handleChange(index, true);
}
handleMouseDown() {
this.wasClicked = true;
}
handleKeyDown(originalEvent) {
eventUtils.handleEscapeKeydown(originalEvent, () => {
this.emit("escape");
});
eventUtils.handleActionKeydown(originalEvent, () => this.handleChange(this._activeDescendant.index, false));
const itemIndex = this.getTypeaheadIndex(this.getEl("options").children, originalEvent.key, this.input.typeaheadTimeoutLength || TYPEAHEAD_TIMEOUT_LENGTH);
if (itemIndex !== -1) {
this._activeDescendant.index = itemIndex;
const container = this.getEl("options");
container.scrollTop =
this.getEls("option")[itemIndex].offsetTop -
container.offsetHeight / 2;
}
}
handleListboxChange(event) {
const selectedIndex = parseInt(event.detail.toIndex, 10);
const el = this.getEls("option")[selectedIndex];
const wasClicked = this.wasClicked;
(0, element_scroll_1.scroll)(el);
if (this.wasClicked) {
this.wasClicked = false;
}
this.handleChange(selectedIndex, wasClicked);
}
onCreate() {
this.state = {
selectedIndex: -1,
};
}
onInput(input) {
const { state } = this;
input.option = input.option || [];
state.selectedIndex = -1;
let i = 0;
for (const option of input.option || []) {
if (option.selected) {
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, state } = this;
if (((_a = input.option) === null || _a === void 0 ? void 0 : _a.length) && !input.disabled) {
const container = this.getEl("options");
const optionsContainer = this.getEl("options");
this._activeDescendant = (0, makeup_active_descendant_1.createLinear)(container, optionsContainer, optionsContainer, ".listbox__option[role=option]", {
activeDescendantClassName: "listbox__option--active",
autoInit: state.selectedIndex,
autoReset: null,
autoScroll: !this.isAutoSelection,
});
const { getIndex: getTypeaheadIndex, destroy: destroyTypeahead } = (0, makeup_typeahead_1.default)();
this.getTypeaheadIndex = getTypeaheadIndex;
this.destroyTypeahead = destroyTypeahead;
}
}
_cleanupMakeup() {
if (this._activeDescendant) {
this._activeDescendant.reset();
this._activeDescendant.destroy();
this._activeDescendant = undefined;
}
}
}
module.exports = Listbox;