UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

169 lines (127 loc) 8.87 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 _includes from "lodash/includes"; import _keys from "lodash/keys"; import _map from "lodash/map"; import _isFunction from "lodash/isFunction"; import _get from "lodash/get"; 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 Button from '../Button/Button'; 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 reducers from './ButtonGroup.reducers'; import { buildModernHybridComponent } from '../../util/state-management'; var cx = lucidClassNames.bind('&-ButtonGroup'); var any = PropTypes.any, func = PropTypes.func, arrayOf = PropTypes.arrayOf, number = PropTypes.number; var ButtonGroupButton = function ButtonGroupButton(_props) { return null; }; ButtonGroupButton.displayName = 'ButtonGroup.Button'; ButtonGroupButton.peek = { description: "\n\t\tRenders a `<Button`> inside the `ButtonGroup`.\n\t" }; var defaultProps = { onSelect: _noop, selectedIndices: [] }; var ButtonGroup = /*#__PURE__*/function (_React$Component) { _inherits(ButtonGroup, _React$Component); var _super = _createSuper(ButtonGroup); function ButtonGroup() { var _this; _classCallCheck(this, ButtonGroup); 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), "handleSelect", function (_ref) { var event = _ref.event, childProps = _ref.props; var callbackId = childProps.callbackId; var clickedButtonProps = _get(findTypes(_this.props, ButtonGroup.Button)[callbackId], 'props', {}); // If the consumer passed in an `onClick` to the child `ButtonGroup.Button` // component, we should make sure to call that in addition to the // `ButtonGroup`'s `onSelect`. if (_isFunction(clickedButtonProps.onClick)) { clickedButtonProps.onClick({ event: event, props: childProps }); } _this.props.onSelect(callbackId, { event: event, props: childProps }); }); return _this; } _createClass(ButtonGroup, [{ key: "render", value: function render() { var _this2 = this; var _this$props = this.props, selectedIndices = _this$props.selectedIndices, className = _this$props.className, children = _this$props.children, passThroughs = _objectWithoutProperties(_this$props, ["selectedIndices", "className", "children"]); var buttonChildProps = _map(findTypes(this.props, ButtonGroup.Button), 'props'); return /*#__PURE__*/React.createElement("span", _extends({}, omitProps(passThroughs, undefined, _keys(ButtonGroup.propTypes)), { className: cx('&', className) }), _map(buttonChildProps, function (buttonChildProp, index) { return ( /*#__PURE__*/ // The order of the spread operator below is important. If the // consumer puts `isActive` directly on a `ButtonGroup.Button`, we // want that to take precedence over the `selectedIndices` prop on // the parent `ButtonGroup`. However, we want our `onClick` at the // bottom because we manually handle passing the event to the // `ButtonGroup.Button`'s `onClick` if it exists. React.createElement(Button, _extends({ isActive: _includes(selectedIndices, index) }, buttonChildProp, { key: index, callbackId: index, onClick: _this2.handleSelect })) ); }), children); } }]); return ButtonGroup; }(React.Component); _defineProperty(ButtonGroup, "displayName", 'ButtonGroup'); _defineProperty(ButtonGroup, "peek", { description: "\n\t\t\tButton groups allow you to pair buttons together to form a seamless\n\t\t\tcluster. Any props not explicitly called out are spread on to the root\n\t\t\tcomponent.\n\t\t", categories: ['controls', 'buttons'], madeFrom: ['Button'] }); _defineProperty(ButtonGroup, "Button", ButtonGroupButton); _defineProperty(ButtonGroup, "reducers", reducers); _defineProperty(ButtonGroup, "defaultProps", defaultProps); _defineProperty(ButtonGroup, "propTypes", { onSelect: func, className: any, children: any, selectedIndices: arrayOf(number) }); export default buildModernHybridComponent(ButtonGroup, { reducers: reducers }); export { ButtonGroup as ButtonGroupDumb };