UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

120 lines 7.97 kB
"use strict"; 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 __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; 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 __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.ButtonGroupDumb = void 0; var lodash_1 = __importDefault(require("lodash")); var Button_1 = __importDefault(require("../Button/Button")); 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 ButtonGroup_reducers_1 = __importDefault(require("./ButtonGroup.reducers")); var state_management_1 = require("../../util/state-management"); var cx = style_helpers_1.lucidClassNames.bind('&-ButtonGroup'); var any = prop_types_1.default.any, func = prop_types_1.default.func, arrayOf = prop_types_1.default.arrayOf, number = prop_types_1.default.number; var ButtonGroupButton = function (_props) { return null; }; ButtonGroupButton.displayName = 'ButtonGroup.Button'; ButtonGroupButton.peek = { description: "\n\t\tRenders a `<Button`> inside the `ButtonGroup`.\n\t", }; var defaultProps = { onSelect: lodash_1.default.noop, selectedIndices: [], }; var ButtonGroup = /** @class */ (function (_super) { __extends(ButtonGroup, _super); function ButtonGroup() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.handleSelect = function (_a) { var event = _a.event, childProps = _a.props; var callbackId = childProps.callbackId; var clickedButtonProps = lodash_1.default.get(component_types_1.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 (lodash_1.default.isFunction(clickedButtonProps.onClick)) { clickedButtonProps.onClick({ event: event, props: childProps }); } _this.props.onSelect(callbackId, { event: event, props: childProps }); }; return _this; } ButtonGroup.prototype.render = function () { var _this = this; var _a = this.props, selectedIndices = _a.selectedIndices, className = _a.className, children = _a.children, passThroughs = __rest(_a, ["selectedIndices", "className", "children"]); var buttonChildProps = lodash_1.default.map(component_types_1.findTypes(this.props, ButtonGroup.Button), 'props'); return (react_1.default.createElement("span", __assign({}, component_types_1.omitProps(passThroughs, undefined, lodash_1.default.keys(ButtonGroup.propTypes)), { className: cx('&', className) }), lodash_1.default.map(buttonChildProps, function (buttonChildProp, index) { return ( // 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_1.default.createElement(Button_1.default, __assign({ isActive: lodash_1.default.includes(selectedIndices, index) }, buttonChildProp, { key: index, callbackId: index, onClick: _this.handleSelect }))); }), children)); }; ButtonGroup.displayName = 'ButtonGroup'; 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'], }; ButtonGroup.Button = ButtonGroupButton; ButtonGroup.reducers = ButtonGroup_reducers_1.default; ButtonGroup.defaultProps = defaultProps; ButtonGroup.propTypes = { onSelect: func(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\t\tA function that is called with the index of the child button clicked.\n\t\t\t`props` refers to the child button props. Signature:\n\t\t\t`(selectedIndex, { event, props }) => {}`\n\t\t"], ["\n\t\t\tA function that is called with the index of the child button clicked.\n\t\t\t\\`props\\` refers to the child button props. Signature:\n\t\t\t\\`(selectedIndex, { event, props }) => {}\\`\n\t\t"]))), className: any(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\t\tAppended to the component-specific class names set on the root element.\n\t\t\tValue is run through the `classnames` library.\n\t\t"], ["\n\t\t\tAppended to the component-specific class names set on the root element.\n\t\t\tValue is run through the \\`classnames\\` library.\n\t\t"]))), children: any(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t\t\tAll children should be `ButtonGroup.Button`s and they support the same\n\t\t\tprops as `Button`s.\n\t\t"], ["\n\t\t\tAll children should be \\`ButtonGroup.Button\\`s and they support the same\n\t\t\tprops as \\`Button\\`s.\n\t\t"]))), selectedIndices: arrayOf(number)(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t\t\tAn array of currently selected `ButtonGroup.Button`s indices. You can\n\t\t\talso pass the prop `isActive` to individual `ButtonGroup.Button`\n\t\t\tcomponents.\n\t\t"], ["\n\t\t\tAn array of currently selected \\`ButtonGroup.Button\\`s indices. You can\n\t\t\talso pass the prop \\`isActive\\` to individual \\`ButtonGroup.Button\\`\n\t\t\tcomponents.\n\t\t"]))), }; return ButtonGroup; }(react_1.default.Component)); exports.ButtonGroupDumb = ButtonGroup; exports.default = state_management_1.buildModernHybridComponent(ButtonGroup, { reducers: ButtonGroup_reducers_1.default }); var templateObject_1, templateObject_2, templateObject_3, templateObject_4; //# sourceMappingURL=ButtonGroup.js.map