@ebay/ebayui-core
Version:
Collection of core eBay components; considered to be the building blocks for all composite structures, pages & apps.
90 lines (89 loc) • 2.99 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MenuUtils = void 0;
exports.default = setupMenu;
const Component = (typeof Marko === "object"
? Marko.Component
: function () { });
class MenuUtils extends Component {
isRadio() {
return this.type === "radio";
}
getCheckedValues() {
if (this.isRadio()) {
const item = this.items[this.state.checkedIndex] || {};
return [item.value];
}
return this.items
.filter((item, index) => this.state.checkedItems[index])
.map((item) => item.value);
}
getCheckedIndexes() {
if (this.isRadio()) {
return this.state.checkedIndex === undefined
? undefined
: [this.state.checkedIndex];
}
return this.items
.map((item, i) => this.state.checkedItems[i] && i)
.filter((item) => item !== false && item !== undefined);
}
getInputState(input) {
/*
ebay-menu uses separators and we need to exclude these
from items to pass correct indexes to state
Any other component that doesn't have separator should pass through
*/
this.items = [...(input.item || [])].filter((item) => !item.separator);
this.type = input.type;
if (this.isRadio()) {
return {
checkedIndex: (this.items || []).findIndex((item) => item.checked || false),
};
}
return {
checkedItems: (this.items || []).map((item) => item.checked || false),
};
}
isChecked(index) {
if (this.isRadio()) {
return index === this.state.checkedIndex;
}
return this.state.checkedItems[index];
}
isDisabled(index) {
return this.items[index].disabled;
}
toggleChecked(index) {
if (Array.isArray(index)) {
if (this.isRadio()) {
this.state.checkedIndex = index[0];
}
else {
this.state.checkedItems = this.state.checkedItems.map((item, i) => index.indexOf(i) !== -1);
}
return;
}
if (this.isRadio() && index !== this.state.checkedIndex) {
this.state.checkedIndex = index;
}
else if (this.type !== "radio") {
this.state.checkedItems[index] = !this.state.checkedItems[index];
this.setStateDirty("checkedItems");
}
}
getSeparatorMap(input) {
let separatorCount = 0;
return [...(input.item || [])].reduce((map, item, index) => {
if (item.separator) {
map[index - separatorCount] = true;
separatorCount++;
}
return map;
}, {});
}
}
exports.MenuUtils = MenuUtils;
function setupMenu(instance) {
Object.defineProperties(instance, Object.getOwnPropertyDescriptors(MenuUtils.prototype));
}