@ebay/ebayui-core
Version:
Collection of core eBay components; considered to be the building blocks for all composite structures, pages & apps.
174 lines (173 loc) • 6.73 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 scrollKeyPreventer = __importStar(require("makeup-prevent-scroll-keys"));
const makeup_roving_tabindex_1 = require("makeup-roving-tabindex");
const makeup_typeahead_1 = __importDefault(require("makeup-typeahead"));
const eventUtils = __importStar(require("../../common/event-utils"));
const menu_utils_1 = __importStar(require("../../common/menu-utils"));
const TYPEAHEAD_TIMEOUT_LENGTH = 1300;
class default_1 extends menu_utils_1.MenuUtils {
onCreate() {
(0, menu_utils_1.default)(this);
}
toggleItemChecked(index, originalEvent, itemEl) {
// 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) {
this.emitComponentEvent({
index,
eventType: "change",
el: itemEl,
originalEvent,
});
}
else if (this.type !== "radio") {
this.emitComponentEvent({
index,
eventType: !this.type ? "select" : "change",
el: itemEl,
originalEvent,
});
}
if (this.rovingTabindex) {
this.tabindexPosition = this.rovingTabindex.items.findIndex((el) => el.tabIndex === 0);
}
}
handleItemClick(index, originalEvent, itemEl) {
this.toggleItemChecked(index, originalEvent, itemEl);
}
handleItemKeydown(index, originalEvent, itemEl) {
eventUtils.handleEscapeKeydown(originalEvent, () => {
this.emitComponentEvent({
eventType: "keydown",
originalEvent,
index,
});
});
eventUtils.handleActionKeydown(originalEvent, () => this.toggleItemChecked(index, originalEvent, itemEl));
eventUtils.handleArrowsKeydown(originalEvent, () => {
setTimeout(() => {
if (index !== this.rovingTabindex.index) {
this.emitComponentEvent({
eventType: "keydown",
originalEvent,
index: this.rovingTabindex.index,
});
}
});
});
}
handleItemKeypress({ key }) {
const itemIndex = this.getTypeaheadIndex(this.getEl("menu").children, key, this.input.typeaheadTimeoutLength || TYPEAHEAD_TIMEOUT_LENGTH);
if (itemIndex !== -1) {
this.tabindexPosition = this.rovingTabindex.index = itemIndex;
}
}
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, {
index,
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 {
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.tabindexPosition = 0;
this._setupMakeup();
}
onUpdate() {
this._setupMakeup();
}
onDestroy() {
this._cleanupMakeup();
}
_setupMakeup() {
this.contentEl = this.getEl("menu");
this.rovingTabindex = (0, makeup_roving_tabindex_1.createLinear)(this.contentEl, "div", {
index: this.tabindexPosition,
autoReset: "interactive",
});
scrollKeyPreventer.add(this.contentEl);
const { getIndex: getTypeaheadIndex, destroy: destroyTypeahead } = (0, makeup_typeahead_1.default)();
this.getTypeaheadIndex = getTypeaheadIndex;
this.destroyTypeahead = destroyTypeahead;
}
_cleanupMakeup() {
if (this.rovingTabindex) {
this.rovingTabindex.destroy();
scrollKeyPreventer.remove(this.contentEl);
}
if (this.destroyTypeahead) {
this.destroyTypeahead();
}
}
}
module.exports = default_1;