lucid-ui
Version:
A UI component library from Xandr.
142 lines • 7.07 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 __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 react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var Button_1 = __importDefault(require("../Button/Button"));
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: "Renders a `<Button`> inside the `ButtonGroup`.",
};
var defaultProps = {
onSelect: lodash_1.default.noop,
selectedIndices: [],
};
var nonPassThroughs = [
'onSelect',
'className',
'children',
'selectedIndices',
];
var ButtonGroup = /** @class */ (function (_super) {
__extends(ButtonGroup, _super);
function ButtonGroup() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.handleSelect = function (event, childProps, index) {
var clickedButtonProps = lodash_1.default.get((0, component_types_1.findTypes)(_this.props, ButtonGroup.Button)[index], '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(index, { 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((0, component_types_1.findTypes)(this.props, ButtonGroup.Button), 'props');
return (react_1.default.createElement("span", __assign({}, lodash_1.default.omit(passThroughs, nonPassThroughs), { 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, onClick: function (_a) {
var event = _a.event, props = _a.props;
return _this.handleSelect(event, props, index);
} })));
}),
children));
};
ButtonGroup.displayName = 'ButtonGroup';
ButtonGroup.peek = {
description: "`Button Group` allows you to pair `Buttons` together to form a seamless cluster. Any props not explicitly called out are spread on to the root component.",
categories: ['controls', 'buttons'],
madeFrom: ['Button'],
};
ButtonGroup.Button = ButtonGroupButton;
ButtonGroup.reducers = ButtonGroup_reducers_1.default;
ButtonGroup.defaultProps = defaultProps;
ButtonGroup.propTypes = {
/**
A function that is called with the index of the child button clicked.
\`props\` refers to the child button props. Signature:
\`(selectedIndex, { event, props }) => {}\`
*/
onSelect: func,
/**
Appended to the component-specific class names set on the root element.
Value is run through the \`classnames\` library.
*/
className: any,
/**
All children should be \`ButtonGroup.Button\`s and they support the same
props as \`Button\`s.
*/
children: any,
/**
An array of currently selected \`ButtonGroup.Button\`s indices. You can
also pass the prop \`isActive\` to individual \`ButtonGroup.Button\`
components.
*/
selectedIndices: arrayOf(number),
};
return ButtonGroup;
}(react_1.default.Component));
exports.ButtonGroupDumb = ButtonGroup;
exports.default = (0, state_management_1.buildModernHybridComponent)(ButtonGroup, { reducers: ButtonGroup_reducers_1.default });
//# sourceMappingURL=ButtonGroup.js.map