UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

164 lines 11.8 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 __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SplitButtonDumb = 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 Button_1 = require("../Button/Button"); var ButtonGroup_1 = require("../ButtonGroup/ButtonGroup"); var ChevronIcon_1 = __importDefault(require("../Icon/ChevronIcon/ChevronIcon")); var DropMenu_1 = require("../DropMenu/DropMenu"); var reducers = __importStar(require("./SplitButton.reducers")); var cx = style_helpers_1.lucidClassNames.bind('&-SplitButton'); var bool = prop_types_1.default.bool, func = prop_types_1.default.func, node = prop_types_1.default.node, oneOf = prop_types_1.default.oneOf, shape = prop_types_1.default.shape, string = prop_types_1.default.string; var ButtonChild = function (_props) { return null; }; ButtonChild.displayName = 'SplitButton.ButtonChild'; ButtonChild.peek = { description: "\n\t\tOne of many potential `Button`s to render in this\n\t\t`SplitButton`. The first `Button` will be used as the Primary\n\t\tbutton, while all others will be rendered within the `DropMenu`\n\t\tbelow.\n\t", }; ButtonChild.propTypes = { children: node(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\tThe children to render within the `Button`.\n\t"], ["\n\t\tThe children to render within the \\`Button\\`.\n\t"]))), isDisabled: bool(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\tDisables selection of the `Button`.\n\t"], ["\n\t\tDisables selection of the \\`Button\\`.\n\t"]))), onClick: func(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t\tCalled when the user clicks the `Button`. Signature:\n\t\t`({ props, event }) => {}`\n\t"], ["\n\t\tCalled when the user clicks the \\`Button\\`. Signature:\n\t\t\\`({ props, event }) => {}\\`\n\t"]))), }; var SplitButton = /** @class */ (function (_super) { __extends(SplitButton, _super); function SplitButton() { var _this = _super !== null && _super.apply(this, arguments) || this; // Handles select events in the DropMenu _this.handleSelect = function (optionIndex, _a) { var event = _a.event; var buttonChildProps = lodash_1.default.map(component_types_1.filterTypes(_this.props.children, SplitButton.Button), 'props'); if (optionIndex !== null) { _this.handleButtonClick(buttonChildProps[optionIndex + 1], event); } }; // Handles clicks on the Primary Button _this.handleClick = function (_a) { var event = _a.event; var clickedButtonProps = lodash_1.default.get(component_types_1.getFirst(_this.props, SplitButton.Button), 'props'); // Stop propagation to prevent this `Click` from expanding the `DropMenu` event.stopPropagation(); _this.handleButtonClick(clickedButtonProps, event); }; // Handles clicks within handleClick and handleSelect _this.handleButtonClick = function (buttonProps, event) { var onCollapse = _this.props.DropMenu.onCollapse; onCollapse && onCollapse({ props: _this.props.DropMenu, event: event }); if (lodash_1.default.has(buttonProps, 'onClick')) { buttonProps.onClick({ event: event, props: buttonProps }); } }; return _this; } SplitButton.prototype.render = function () { var _a = this.props, className = _a.className, kind = _a.kind, direction = _a.direction, type = _a.type, size = _a.size, dropMenuProps = _a.DropMenu, passThroughs = __rest(_a, ["className", "kind", "direction", "type", "size", "DropMenu"]); var isExpanded = dropMenuProps.isExpanded; var _b = lodash_1.default.map(component_types_1.filterTypes(this.props.children, SplitButton.Button), 'props'), primaryButtonProps = _b[0], buttonChildProps = _b.slice(1); return (react_1.default.createElement(DropMenu_1.DropMenuDumb, __assign({}, dropMenuProps, component_types_1.omitProps(passThroughs, undefined, lodash_1.default.keys(DropMenu_1.DropMenuDumb.Option.propTypes)), { direction: direction, className: cx('&', className), onSelect: this.handleSelect }), react_1.default.createElement(DropMenu_1.DropMenuDumb.Control, null, react_1.default.createElement(ButtonGroup_1.ButtonGroupDumb, null, react_1.default.createElement(Button_1.Button, __assign({}, primaryButtonProps, { className: cx('&-Button-primary', lodash_1.default.get(primaryButtonProps, 'className')), kind: kind, type: type, size: size, onClick: this.handleClick })), react_1.default.createElement(Button_1.Button, { className: cx('&-Button-drop'), size: size, hasOnlyIcon: true, isActive: isExpanded, kind: kind, isDisabled: lodash_1.default.every(__spreadArrays([primaryButtonProps], buttonChildProps), 'isDisabled') }, react_1.default.createElement(ChevronIcon_1.default, { className: cx('&-ChevronIcon'), direction: direction, size: 10 })))), lodash_1.default.map(buttonChildProps, function (buttonChildProp, index) { return (react_1.default.createElement(DropMenu_1.DropMenuDumb.Option, __assign({}, buttonChildProp, { key: index }))); }))); }; SplitButton.displayName = 'SplitButton'; SplitButton.Button = ButtonChild; SplitButton.peek = { description: "\n\t\t\t`SplitButton` allow you to combine a single main `Button` together\n\t\t\twith a list of additional `Buttons` with actions which will be rendered\n\t\t\twithin a `DropMenu`.\n\t\t", categories: ['controls', 'buttons'], madeFrom: ['Button', 'DropMenu'], }; SplitButton.reducers = reducers; SplitButton.propTypes = { DropMenu: shape(DropMenu_1.DropMenuDumb.propTypes)(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t\t\tObject of DropMenu props which are passed through to the underlying\n\t\t\tDropMenu component.\n\t\t"], ["\n\t\t\tObject of DropMenu props which are passed through to the underlying\n\t\t\tDropMenu component.\n\t\t"]))), children: node(templateObject_5 || (templateObject_5 = __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"]))), className: string(templateObject_6 || (templateObject_6 = __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"]))), direction: oneOf(['up', 'down'])(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n\t\t\tSets the direction the flyout menu will render relative to the\n\t\t\tSplitButton.\n\t\t"], ["\n\t\t\tSets the direction the flyout menu will render relative to the\n\t\t\tSplitButton.\n\t\t"]))), kind: oneOf(['primary', 'danger'])(templateObject_8 || (templateObject_8 = __makeTemplateObject(["\n\t\t\tStyle variations of the SplitButton.\n\t\t"], ["\n\t\t\tStyle variations of the SplitButton.\n\t\t"]))), size: oneOf(['short', 'small', 'large'])(templateObject_9 || (templateObject_9 = __makeTemplateObject(["\n\t\t\tSize variations of the SplitButton.\n\t\t"], ["\n\t\t\tSize variations of the SplitButton.\n\t\t"]))), type: string(templateObject_10 || (templateObject_10 = __makeTemplateObject(["\n\t\t\tForm element type variations of SplitButton. Passed through to DOM\n\t\t\tButton.\n\t\t"], ["\n\t\t\tForm element type variations of SplitButton. Passed through to DOM\n\t\t\tButton.\n\t\t"]))), }; SplitButton.defaultProps = { direction: 'down', type: 'button', DropMenu: DropMenu_1.DropMenuDumb.defaultProps, }; return SplitButton; }(react_1.default.Component)); exports.SplitButtonDumb = SplitButton; exports.default = state_management_1.buildModernHybridComponent(SplitButton, { reducers: reducers }); var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10; //# sourceMappingURL=SplitButton.js.map