UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

192 lines 16.5 kB
"use strict"; var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.VerticalListMenuDumb = void 0; var lodash_1 = __importDefault(require("lodash")); var react_1 = __importDefault(require("react")); var prop_types_1 = __importDefault(require("react-peek/prop-types")); var style_helpers_1 = require("../../util/style-helpers"); var component_types_1 = require("../../util/component-types"); var state_management_1 = require("../../util/state-management"); var reducers = __importStar(require("./VerticalListMenu.reducers")); var ChevronIcon_1 = __importDefault(require("../Icon/ChevronIcon/ChevronIcon")); var Collapsible_1 = __importDefault(require("../Collapsible/Collapsible")); var cx = style_helpers_1.lucidClassNames.bind('&-VerticalListMenu'); var func = prop_types_1.default.func, arrayOf = prop_types_1.default.arrayOf, bool = prop_types_1.default.bool, string = prop_types_1.default.string, number = prop_types_1.default.number, node = prop_types_1.default.node, object = prop_types_1.default.object, shape = prop_types_1.default.shape; var Item = function (_props) { return null; }; Item.peek = { description: "\n\t\tA child item that can contain content or another VerticalListMenu.\n\t", }; Item.displayName = 'VerticalListMenu.Item'; Item.propTypes = { hasExpander: bool(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\t\t\t\tShow or hide the expand button. Should be `true` if you want to\n\t\t\t\t\tnest menus.\n\t\t\t\t"], ["\n\t\t\t\t\tShow or hide the expand button. Should be \\`true\\` if you want to\n\t\t\t\t\tnest menus.\n\t\t\t\t"]))), isExpanded: bool(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\t\t\tDetermines the visibility of nested menus.\n\t\t\t"], ["\n\t\t\t\tDetermines the visibility of nested menus.\n\t\t\t"]))), isSelected: bool(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t\t\t\tIf `true` then a small bar on the left side of the item will be\n\t\t\t\tshown indicating this item is selected.\n\t\t\t"], ["\n\t\t\t\tIf \\`true\\` then a small bar on the left side of the item will be\n\t\t\t\tshown indicating this item is selected.\n\t\t\t"]))), isActionable: bool(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t\t\t\tDetermines the visibility of the small bar on the left when the user\n\t\t\t\thovers over the item. This should indicate to the user that an item\n\t\t\t\tis clickable.\n\t\t\t"], ["\n\t\t\t\tDetermines the visibility of the small bar on the left when the user\n\t\t\t\thovers over the item. This should indicate to the user that an item\n\t\t\t\tis clickable.\n\t\t\t"]))), onSelect: func(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n\t\t\t\tCalled when the user clicks the main body of the item. Signature:\n\t\t\t\t`(index, { event, props}) => {}`\n\t\t\t"], ["\n\t\t\t\tCalled when the user clicks the main body of the item. Signature:\n\t\t\t\t\\`(index, { event, props}) => {}\\`\n\t\t\t"]))), onToggle: func(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n\t\t\t\tCalled when the user clicks the expand button. Signature:\n\t\t\t\t`(index, { event, props}) => {}`\n\t\t\t"], ["\n\t\t\t\tCalled when the user clicks the expand button. Signature:\n\t\t\t\t\\`(index, { event, props}) => {}\\`\n\t\t\t"]))), Collapsible: shape(Collapsible_1.default.propTypes)(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n\t\t\t\tProps that are passed through to the underlying Collapsible component\n\t\t\t\tif the item has children.\n\t\t\t"], ["\n\t\t\t\tProps that are passed through to the underlying Collapsible component\n\t\t\t\tif the item has children.\n\t\t\t"]))), }; var defaultProps = { onSelect: lodash_1.default.noop, onToggle: lodash_1.default.noop, expandedIndices: [], selectedIndices: [], }; var VerticalListMenu = /** @class */ (function (_super) { __extends(VerticalListMenu, _super); function VerticalListMenu() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.handleToggle = function (index, itemChildProp, event) { var onToggle = itemChildProp.onToggle; // Prevent the user from also selecting the current item. event.stopPropagation(); _this.props.onToggle(index, { event: event, props: itemChildProp }); if (onToggle) { onToggle(index, { event: event, props: itemChildProp }); } }; _this.handleClickItem = function (index, itemChildProp, event) { var onSelect = itemChildProp.onSelect; _this.props.onSelect(index, { event: event, props: itemChildProp }); if (onSelect) { onSelect(index, { event: event, props: itemChildProp }); } }; return _this; } VerticalListMenu.prototype.render = function () { var _this = this; var _a = this.props, children = _a.children, className = _a.className, style = _a.style, selectedIndices = _a.selectedIndices, expandedIndices = _a.expandedIndices, passThroughs = __rest(_a, ["children", "className", "style", "selectedIndices", "expandedIndices"]); var itemChildProps = lodash_1.default.map(component_types_1.findTypes(this.props, VerticalListMenu.Item), 'props'); return (react_1.default.createElement("ul", __assign({}, component_types_1.omitProps(passThroughs, undefined, lodash_1.default.keys(VerticalListMenu.propTypes)), { className: cx('&', className), style: style }), lodash_1.default.map(itemChildProps, function (itemChildProp, index) { var _a = itemChildProp.hasExpander, hasExpander = _a === void 0 ? false : _a, _b = itemChildProp.isActionable, isActionable = _b === void 0 ? true : _b, _c = itemChildProp.Collapsible, collapsibleProps = _c === void 0 ? Collapsible_1.default.defaultProps : _c; var itemChildrenAsArray = react_1.default.Children.toArray(itemChildProp.children); // Was not able to get `child.Type` to work correctly, I suspect this // is due to the way we wrap components with createLucidComponentDefinition var listChildren = lodash_1.default.filter(itemChildrenAsArray, function (child) { return lodash_1.default.get(child, 'type.displayName', '') === 'VerticalListMenu'; }); var otherChildren = lodash_1.default.filter(itemChildrenAsArray, function (child) { return lodash_1.default.get(child, 'type.displayName', '') !== 'VerticalListMenu'; }); // If the prop is found on the child, it should override what was // passed in at the top level for selectedIndices and expandedIndices var actualIsExpanded = lodash_1.default.has(itemChildProp, 'isExpanded') ? lodash_1.default.get(itemChildProp, 'isExpanded', true) : lodash_1.default.includes(expandedIndices, index); var actualIsSelected = lodash_1.default.has(itemChildProp, 'isSelected') ? lodash_1.default.get(itemChildProp, 'isSelected', false) : lodash_1.default.includes(selectedIndices, index); return (react_1.default.createElement("li", __assign({ key: index }, itemChildProp.passThroughs, { className: cx('&-Item', itemChildProp.className) }), react_1.default.createElement("div", { className: cx('&-Item-content', { '&-Item-content-is-selected': actualIsSelected, '&-Item-content-is-not-selected': !actualIsSelected, '&-Item-content-is-expanded': actualIsExpanded, '&-Item-content-is-actionable': isActionable, }), onClick: lodash_1.default.partial(_this.handleClickItem, index, itemChildProp) }, react_1.default.createElement("div", { className: cx('&-Item-content-body') }, react_1.default.createElement("div", { className: cx('&-Item-content-text') }, otherChildren), hasExpander ? (react_1.default.createElement("div", { className: cx('&-Item-expander'), onClick: lodash_1.default.partial(_this.handleToggle, index, itemChildProp) }, react_1.default.createElement(ChevronIcon_1.default, { size: 12, direction: actualIsExpanded ? 'up' : 'down' }))) : null)), !lodash_1.default.isEmpty(listChildren) ? (react_1.default.createElement(Collapsible_1.default, __assign({}, collapsibleProps, { className: cx('&-Item-nested-list'), isExpanded: actualIsExpanded }), listChildren)) : null)); }), children)); }; VerticalListMenu.displayName = 'VerticalListMenu'; VerticalListMenu.Item = Item; VerticalListMenu.peek = { description: "\n\t\t\t\t\tUsed primarily for navigation lists. It supports nesting\n\t\t\t\t\t`VerticalListMenu`s below `VerticalListMenu.Item`s and animating\n\t\t\t\t\texpanding of those sub lists. The default reducer behavior is for only\n\t\t\t\t\tone `VerticalListMenu.Item` to be selected at any given time; that is\n\t\t\t\t\teasily overridden by handling `onSelect` yourself.\n\t\t\t\t", categories: ['navigation'], madeFrom: ['ChevronIcon'], }; VerticalListMenu.reducers = reducers; // TODO: remove this once we move to only buildModernHybridComponent VerticalListMenu.definition = { statics: { Item: Item, reducers: reducers, peek: { description: "\n\t\t\t\t\tUsed primarily for navigation lists. It supports nesting\n\t\t\t\t\t`VerticalListMenu`s below `VerticalListMenu.Item`s and animating\n\t\t\t\t\texpanding of those sub lists. The default reducer behavior is for only\n\t\t\t\t\tone `VerticalListMenu.Item` to be selected at any given time; that is\n\t\t\t\t\teasily overridden by handling `onSelect` yourself.\n\t\t\t\t", categories: ['navigation'], madeFrom: ['ChevronIcon'], }, }, }; VerticalListMenu.propTypes = { children: node(templateObject_8 || (templateObject_8 = __makeTemplateObject(["\n\t\t\tRegular `children` aren't really used in this component, but if you do\n\t\t\tadd them they will be placed at the end of the component. You should be\n\t\t\tusing `VerticalListMenu.Item`s instead of regular children.\n\t\t"], ["\n\t\t\tRegular \\`children\\` aren't really used in this component, but if you do\n\t\t\tadd them they will be placed at the end of the component. You should be\n\t\t\tusing \\`VerticalListMenu.Item\\`s instead of regular children.\n\t\t"]))), className: string(templateObject_9 || (templateObject_9 = __makeTemplateObject(["\n\t\t\tAppended to the component-specific class names set on the root element.\n\t\t"], ["\n\t\t\tAppended to the component-specific class names set on the root element.\n\t\t"]))), style: object(templateObject_10 || (templateObject_10 = __makeTemplateObject(["\n\t\t\tPassed through to the root element.\n\t\t"], ["\n\t\t\tPassed through to the root element.\n\t\t"]))), selectedIndices: arrayOf(number)(templateObject_11 || (templateObject_11 = __makeTemplateObject(["\n\t\t\tIndicates which of the `VerticalListMenu.Item` children are currently\n\t\t\tselected. You can also put the `isSelected` prop directly on the\n\t\t\t`VerticalListMenu.Item`s if you wish.\n\t\t"], ["\n\t\t\tIndicates which of the \\`VerticalListMenu.Item\\` children are currently\n\t\t\tselected. You can also put the \\`isSelected\\` prop directly on the\n\t\t\t\\`VerticalListMenu.Item\\`s if you wish.\n\t\t"]))), expandedIndices: arrayOf(number)(templateObject_12 || (templateObject_12 = __makeTemplateObject(["\n\t\t\tIndicates which of the `VerticalListMenu.Item` children are currently\n\t\t\texpanded. You can also put the `isExpanded` prop directly on the\n\t\t\t`VerticalListMenu.Item`s if you wish.\n\t\t"], ["\n\t\t\tIndicates which of the \\`VerticalListMenu.Item\\` children are currently\n\t\t\texpanded. You can also put the \\`isExpanded\\` prop directly on the\n\t\t\t\\`VerticalListMenu.Item\\`s if you wish.\n\t\t"]))), onSelect: func(templateObject_13 || (templateObject_13 = __makeTemplateObject(["\n\t\t\tCallback fired when the user selects a `VerticalListMenu.Item`.\n\t\t\tSignature: `(index, { event, props }) => {}`\n\t\t"], ["\n\t\t\tCallback fired when the user selects a \\`VerticalListMenu.Item\\`.\n\t\t\tSignature: \\`(index, { event, props }) => {}\\`\n\t\t"]))), onToggle: func(templateObject_14 || (templateObject_14 = __makeTemplateObject(["\n\t\t\tCallback fired when the user expands or collapses an expandable\n\t\t\t`VerticalListMenu.Item`. Signature:\n\t\t\t`(index, { event, props }) => {}`\n\t\t"], ["\n\t\t\tCallback fired when the user expands or collapses an expandable\n\t\t\t\\`VerticalListMenu.Item\\`. Signature:\n\t\t\t\\`(index, { event, props }) => {}\\`\n\t\t"]))), }; VerticalListMenu.defaultProps = defaultProps; return VerticalListMenu; }(react_1.default.Component)); exports.VerticalListMenuDumb = VerticalListMenu; exports.default = state_management_1.buildModernHybridComponent(VerticalListMenu, { reducers: reducers }); var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12, templateObject_13, templateObject_14; //# sourceMappingURL=VerticalListMenu.js.map