UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

243 lines (194 loc) 12.2 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } import _isEmpty from "lodash/isEmpty"; import _partial from "lodash/partial"; import _includes from "lodash/includes"; import _has from "lodash/has"; import _get from "lodash/get"; import _filter from "lodash/filter"; import _keys from "lodash/keys"; import _map from "lodash/map"; import _noop from "lodash/noop"; function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import React from 'react'; import PropTypes from 'react-peek/prop-types'; import { lucidClassNames } from '../../util/style-helpers'; import { findTypes, omitProps } from '../../util/component-types'; import { buildModernHybridComponent } from '../../util/state-management'; import * as reducers from './VerticalListMenu.reducers'; import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon'; import Collapsible from '../Collapsible/Collapsible'; var cx = lucidClassNames.bind('&-VerticalListMenu'); var func = PropTypes.func, arrayOf = PropTypes.arrayOf, bool = PropTypes.bool, string = PropTypes.string, number = PropTypes.number, node = PropTypes.node, object = PropTypes.object, shape = PropTypes.shape; var Item = function Item(_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, isExpanded: bool, isSelected: bool, isActionable: bool, onSelect: func, onToggle: func, Collapsible: shape(Collapsible.propTypes) }; var defaultProps = { onSelect: _noop, onToggle: _noop, expandedIndices: [], selectedIndices: [] }; var VerticalListMenu = /*#__PURE__*/function (_React$Component) { _inherits(VerticalListMenu, _React$Component); var _super = _createSuper(VerticalListMenu); function VerticalListMenu() { var _this; _classCallCheck(this, VerticalListMenu); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_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 }); } }); _defineProperty(_assertThisInitialized(_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; } _createClass(VerticalListMenu, [{ key: "render", value: function render() { var _this2 = this; var _this$props = this.props, children = _this$props.children, className = _this$props.className, style = _this$props.style, selectedIndices = _this$props.selectedIndices, expandedIndices = _this$props.expandedIndices, passThroughs = _objectWithoutProperties(_this$props, ["children", "className", "style", "selectedIndices", "expandedIndices"]); var itemChildProps = _map(findTypes(this.props, VerticalListMenu.Item), 'props'); return /*#__PURE__*/React.createElement("ul", _extends({}, omitProps(passThroughs, undefined, _keys(VerticalListMenu.propTypes)), { className: cx('&', className), style: style }), _map(itemChildProps, function (itemChildProp, index) { var _itemChildProp$hasExp = itemChildProp.hasExpander, hasExpander = _itemChildProp$hasExp === void 0 ? false : _itemChildProp$hasExp, _itemChildProp$isActi = itemChildProp.isActionable, isActionable = _itemChildProp$isActi === void 0 ? true : _itemChildProp$isActi, _itemChildProp$Collap = itemChildProp.Collapsible, collapsibleProps = _itemChildProp$Collap === void 0 ? Collapsible.defaultProps : _itemChildProp$Collap; var itemChildrenAsArray = React.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 = _filter(itemChildrenAsArray, function (child) { return _get(child, 'type.displayName', '') === 'VerticalListMenu'; }); var otherChildren = _filter(itemChildrenAsArray, function (child) { return _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 = _has(itemChildProp, 'isExpanded') ? _get(itemChildProp, 'isExpanded', true) : _includes(expandedIndices, index); var actualIsSelected = _has(itemChildProp, 'isSelected') ? _get(itemChildProp, 'isSelected', false) : _includes(selectedIndices, index); return /*#__PURE__*/React.createElement("li", _extends({ key: index }, itemChildProp.passThroughs, { className: cx('&-Item', itemChildProp.className) }), /*#__PURE__*/React.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: _partial(_this2.handleClickItem, index, itemChildProp) }, /*#__PURE__*/React.createElement("div", { className: cx('&-Item-content-body') }, /*#__PURE__*/React.createElement("div", { className: cx('&-Item-content-text') }, otherChildren), hasExpander ? /*#__PURE__*/React.createElement("div", { className: cx('&-Item-expander'), onClick: _partial(_this2.handleToggle, index, itemChildProp) }, /*#__PURE__*/React.createElement(ChevronIcon, { size: 12, direction: actualIsExpanded ? 'up' : 'down' })) : null)), !_isEmpty(listChildren) ? /*#__PURE__*/React.createElement(Collapsible, _extends({}, collapsibleProps, { className: cx('&-Item-nested-list'), isExpanded: actualIsExpanded }), listChildren) : null); }), children); } }]); return VerticalListMenu; }(React.Component); _defineProperty(VerticalListMenu, "displayName", 'VerticalListMenu'); _defineProperty(VerticalListMenu, "Item", Item); _defineProperty(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'] }); _defineProperty(VerticalListMenu, "reducers", reducers); _defineProperty(VerticalListMenu, "definition", { statics: { Item: Item, reducers: reducers } }); _defineProperty(VerticalListMenu, "propTypes", { children: node, className: string, style: object, selectedIndices: arrayOf(number), expandedIndices: arrayOf(number), onSelect: func, onToggle: func }); _defineProperty(VerticalListMenu, "defaultProps", defaultProps); export default buildModernHybridComponent(VerticalListMenu, { reducers: reducers }); export { VerticalListMenu as VerticalListMenuDumb };