@limetech/lime-elements
Version:
174 lines (173 loc) • 6.69 kB
JavaScript
import { h } from '@stencil/core';
import { getIconColor, getIconName, getIconTitle, } from '../icon/get-icon-props';
import { isFunction } from 'lodash-es';
export class MenuListRenderer {
constructor() {
this.defaultConfig = {
isOpen: true,
badgeIcons: false,
};
/**
* Determine which MenuItem should have the `tab-index` attribute set,
* and return the index at which that MenuItem is located in `items`.
* Returns `undefined` if no item should have the attribute set.
* See https://github.com/material-components/material-components-web/tree/e66a43a75fef4f9179e24856649518e15e279a04/packages/mdc-list#accessibility
*
* @param items - the items of the list, including any `ListSeparator`:s
* @returns the index as per the description
*/
this.getIndexForWhichToApplyTabIndex = (items) => {
let result;
for (let i = 0, max = items.length; i < max; i += 1) {
if ('separator' in items[i]) {
// Ignore ListSeparator
}
else {
const item = items[i];
if (item.disabled) {
// Skip disabled items - they should never get tabindex
continue;
}
if (item.selected) {
result = i;
break;
}
if (result === undefined) {
result = i;
// Do NOT break, as any later item with
// `selected=true` should get the tab-index instead!
}
}
}
return result;
};
/**
* Render a single list item
*
* @param item - the item to render
* @param index - the index the item had in the `items` array
* @returns the list item
*/
this.renderMenuItem = (item, index) => {
if ('separator' in item) {
return (h("li", { class: "mdc-deprecated-list-divider", role: "separator" }, this.rendertext(item), h("div", { class: "limel-list-divider-line" })));
}
const classNames = {
'mdc-deprecated-list-item': true,
'mdc-deprecated-list-item--disabled': item.disabled,
'mdc-deprecated-list-item--selected': item.selected,
};
const attributes = {};
if (index === this.applyTabIndexToItemAtIndex) {
attributes.tabindex = '0';
}
return (h("li", Object.assign({ class: classNames, role: "menuitem", "aria-disabled": item.disabled ? 'true' : 'false', "aria-selected": item.selected ? 'true' : 'false', "data-index": index, "data-text": item.text }, attributes), this.renderIcon(this.config, item), this.renderText(item), this.renderSubMenuIcon(item), this.renderNotification(item), this.twoLines && this.avatarList ? this.renderDivider() : null));
};
/**
* Render the text of the list item
*
* @param item - the list item
* @returns the text for the list item
*/
this.renderText = (item) => {
if (this.isSimpleItem(item)) {
return (h("span", { class: "mdc-deprecated-list-item__text" }, item.text));
}
return (h("div", { class: "mdc-deprecated-list-item__text" }, h("div", { class: "mdc-deprecated-list-item__primary-command-text" }, h("div", { class: "mdc-deprecated-list-item__primary-text" }, item.text), this.renderCommandText(item)), h("div", { class: "mdc-deprecated-list-item__secondary-text" }, item.secondaryText)));
};
this.renderSubMenuIcon = (item) => {
if (!this.hasSubItems(item)) {
return;
}
return h("limel-icon", { class: "sub-menu-icon", name: "-lime-caret-right" });
};
this.rendertext = (item) => {
if ('text' in item) {
return h("h2", { class: "limel-list-divider-title" }, item.text);
}
};
this.renderCommandText = (item) => {
if (!('commandText' in item)) {
return;
}
return (h("div", { class: "mdc-deprecated-list-item__command-text" }, item.commandText));
};
this.isSimpleItem = (item) => {
if ('commandText' in item) {
return false;
}
return !('secondaryText' in item);
};
/**
* Render an icon for a list item
*
* @param config - the config object, passed on from the `renderMenuItem` function
* @param item - the list item
* @returns the icon element
*/
this.renderIcon = (config, item) => {
const style = {};
const name = getIconName(item.icon);
if (!name) {
return;
}
const color = getIconColor(item.icon, item.iconColor);
const title = getIconTitle(item.icon);
if (color) {
if (config.badgeIcons) {
style['--icon-background-color'] = color;
}
else {
style.color = color;
}
}
return (h("limel-icon", { badge: config.badgeIcons, class: "mdc-deprecated-list-item__graphic", name: name, style: style, size: config.iconSize, "aria-label": title, "aria-hidden": title ? null : 'true' }));
};
this.renderNotification = (item) => {
if (item.badge !== undefined) {
return h("limel-badge", { label: item.badge });
}
};
this.renderDivider = () => {
const classes = {
'mdc-deprecated-list-divider': true,
'mdc-deprecated-list-divider--inset': true,
};
if (this.config.iconSize) {
classes[this.config.iconSize] = true;
}
return h("hr", { class: classes });
};
this.hasSubItems = (item) => {
return ((Array.isArray(item.items) && item.items.length > 0) ||
isFunction(item.items));
};
}
render(items, config = {}) {
items = items || [];
this.config = Object.assign(Object.assign({}, this.defaultConfig), config);
this.twoLines = items.some((item) => {
return 'secondaryText' in item && !!item.secondaryText;
});
this.commandKey = items.some((item) => {
return 'commandText' in item && !!item.commandText;
});
this.hasIcons = items.some((item) => {
return 'icon' in item && !!item.icon;
});
this.avatarList = this.config.badgeIcons && this.hasIcons;
this.applyTabIndexToItemAtIndex =
this.getIndexForWhichToApplyTabIndex(items);
const classNames = {
'mdc-deprecated-list': true,
'mdc-deprecated-list--two-line': this.twoLines,
selectable: true,
'mdc-deprecated-list--avatar-list': this.avatarList,
'list--compact': this.twoLines &&
this.commandKey &&
['small', 'x-small'].includes(this.config.iconSize),
};
return (h("ul", { class: classNames, role: "menu", "aria-orientation": "vertical", style: { '--maxLinesSecondaryText': '2' } }, items.map(this.renderMenuItem)));
}
}
//# sourceMappingURL=menu-list-renderer.js.map