lucid-ui
Version:
A UI component library from Xandr.
209 lines • 10.3 kB
JavaScript
;
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) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
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 = __importStar(require("lodash"));
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("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 = {
/**
The children to render within the \`Button\`.
*/
children: node,
/**
Disables selection of the \`Button\`.
*/
isDisabled: bool,
/**
Called when the user clicks the \`Button\`. Signature:
\`({ props, event }) => {}\`
*/
onClick: func,
};
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((0, 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((0, 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((0, 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, (0, lodash_1.omit)(passThroughs, [
'DropMenu',
'children',
'className',
'direction',
'kind',
'size',
'type',
'initialState',
'callbackId',
]), { 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(__spreadArray([primaryButtonProps], buttonChildProps, true), '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: "`SplitButton` allows you to combine a single main `Button` together with a list of additional `Buttons` with actions which will be rendered within a `DropMenu`.",
categories: ['controls', 'buttons'],
madeFrom: ['Button', 'DropMenu'],
};
SplitButton.reducers = reducers;
SplitButton.propTypes = {
/**
Object of DropMenu props which are passed through to the underlying
DropMenu component.
*/
DropMenu: shape(DropMenu_1.DropMenuDumb.propTypes),
/**
All children should be \`ButtonGroup.Button\`s and they support the same
props as \`Button\`s.
*/
children: node,
/**
Appended to the component-specific class names set on the root element.
Value is run through the \`classnames\` library.
*/
className: string,
/**
Sets the direction the flyout menu will render relative to the
SplitButton.
*/
direction: oneOf(['up', 'down']),
/**
Style variations of the SplitButton.
*/
kind: oneOf(['primary', 'danger']),
/**
Size variations of the SplitButton.
*/
size: oneOf(['short', 'small', 'large']),
/**
Form element type variations of SplitButton. Passed through to DOM
Button.
*/
type: string,
};
SplitButton.defaultProps = {
direction: 'down',
type: 'button',
DropMenu: DropMenu_1.DropMenuDumb.defaultProps,
};
return SplitButton;
}(react_1.default.Component));
exports.SplitButtonDumb = SplitButton;
exports.default = (0, state_management_1.buildModernHybridComponent)(SplitButton, { reducers: reducers });
//# sourceMappingURL=SplitButton.js.map