UNPKG

@ebay/ebayui-core

Version:

Collection of core eBay components; considered to be the building blocks for all composite structures, pages & apps.

205 lines (204 loc) 7.63 kB
"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_expander_1 = __importDefault(require("makeup-expander")); const eventUtils = __importStar(require("../../common/event-utils")); const dropdown_1 = require("../../common/dropdown"); const menu_utils_1 = __importStar(require("../../common/menu-utils")); class default_1 extends menu_utils_1.MenuUtils { onCreate() { (0, menu_utils_1.default)(this); } toggleItemChecked(index, itemEl, originalEvent) { // This needs to be at start since toggleChecked swaps the checkedIndex // and then the right events will not fire correctly const shouldEmitRadio = this.isRadio() && index !== this.state.checkedIndex; this.toggleChecked(index); if (shouldEmitRadio) { if (this.input.collapseOnSelect) { this.expander.expanded = false; setTimeout(() => this.focus(), 0); } this.emitComponentEvent({ index, eventType: "change", el: itemEl, originalEvent, }); } else if (this.type !== "radio") { if (this.input.collapseOnSelect) { this.expander.expanded = false; setTimeout(() => this.focus(), 0); } this.emitComponentEvent({ index, eventType: !this.type ? "select" : "change", el: itemEl, originalEvent, }); } } handleItemClick(index, e, itemEl) { this.toggleItemChecked(index, itemEl, e); } handleMenuKeydown({ el, originalEvent, index }) { if (originalEvent === undefined) return; eventUtils.handleActionKeydown(originalEvent, () => { this.handleItemClick(index !== null && index !== void 0 ? index : 0, originalEvent, el); }); eventUtils.handleEscapeKeydown(originalEvent, () => { this.expander.expanded = false; this.focus(); }); } focus() { var _a; (_a = this.getEl("button")) === null || _a === void 0 ? void 0 : _a.focus(); } handleButtonEscape() { this.expander.expanded = false; } handleExpand() { if (this.input.disabled) { return; } this.dropdownUtil.show(); this.emitComponentEvent({ eventType: "expand" }); } handleCollapse() { if (this.input.disabled) { return; } this.dropdownUtil.hide(); this.emitComponentEvent({ eventType: "collapse" }); } handleMenuChange({ el, originalEvent, index }) { this.toggleItemChecked(index !== null && index !== void 0 ? index : 0, el, originalEvent); } handleMenuSelect({ el, originalEvent, index }) { if (this.input.collapseOnSelect) { this.expander.expanded = false; // setTimeout is done because otherwise expander stays open unless focus happens after expander is closed setTimeout(() => this.focus(), 0); } this.emitComponentEvent({ eventType: "select", el, originalEvent, index, }); } handleMousedown(originalEvent, el) { this.emitComponentEvent({ eventType: "mousedown", el, originalEvent }); } emitComponentEvent({ eventType, el, originalEvent, index, }) { const checkedIndexes = this.getCheckedIndexes(); const isCheckbox = this.type === "checkbox"; const eventObj = { el, originalEvent, }; if (isCheckbox && checkedIndexes && checkedIndexes.length > 1) { Object.assign(eventObj, { indexes: this.getCheckedIndexes(), // DEPRECATED in v5 checked: this.getCheckedIndexes(), // DEPRECATED in v5 (keep but change from indexes to values) checkedValues: this.getCheckedValues(), // DEPRECATED in v5 }); } else if (isCheckbox || this.isRadio()) { Object.assign(eventObj, { index, // DEPRECATED in v5 checked: this.getCheckedIndexes(), // DEPRECATED in v5 (keep but change from indexes to values) checkedValues: this.getCheckedValues(), // DEPRECATED in v5 }); } else if (eventType !== "expand" && eventType !== "collapse") { Object.assign(eventObj, { index, // DEPRECATED in v5 checked: [index], // DEPRECATED in v5 (keep but change from indexes to values) }); } this.emit(`${eventType}`, eventObj); } onInput(input) { this.state = this.getInputState(input); } onRender() { if (typeof window !== "undefined") { this._cleanupMakeup(); } } onMount() { this._setupMakeup(); } onUpdate() { this._setupMakeup(); } onDestroy() { this._cleanupMakeup(); } _setupMakeup() { this.expander = new makeup_expander_1.default(this.el, { hostSelector: ".menu-button__button", contentSelector: ".menu-button__menu", focusManagement: "focusable", expandOnClick: true, autoCollapse: true, alwaysDoFocusManagement: true, collapseOnHostReFocus: true, }); this.expander.expanded = this.isExpanded; delete this.isExpanded; this.dropdownUtil = new dropdown_1.DropdownUtil(this.getEl("button"), this.getEl("content"), { reverse: this.input.reverse, strategy: this.input.strategy, flip: this.input.flip, }); } _cleanupMakeup() { var _a, _b; if (this.expander) { this.expander.destroy(); this.isExpanded = this.expander.expanded; } (_b = (_a = this.dropdownUtil) === null || _a === void 0 ? void 0 : _a.cleanup) === null || _b === void 0 ? void 0 : _b.call(_a); } } module.exports = default_1;