UNPKG

lucid-ui

Version:

A UI component library from Xandr.

275 lines 13.2 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) { 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TabsDumb = void 0; var lodash_1 = __importDefault(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 reducers = __importStar(require("./Tabs.reducers")); var Badge_1 = __importDefault(require("../Badge/Badge")); var cx = style_helpers_1.lucidClassNames.bind('&-Tabs'); var bool = prop_types_1.default.bool, func = prop_types_1.default.func, node = prop_types_1.default.node, number = prop_types_1.default.number, string = prop_types_1.default.string, any = prop_types_1.default.any; var Title = function (props) { return null; }; Title.peek = { description: "`Titles` can be provided as a child or prop to a `Tab`.", }; Title.displayName = 'Tabs.Title'; Title.propName = 'Title'; var Tab = function (props) { var isDisabled = props.isDisabled, isLastTab = props.isLastTab, isOpen = props.isOpen, isProgressive = props.isProgressive, isSelected = props.isSelected, Title = props.Title, className = props.className, count = props.count, isVariableCountWidth = props.isVariableCountWidth, _a = props.index, index = _a === void 0 ? 0 : _a, // this defaults should not be invoked unless someone is using `Tab` outside of `Tabs` _b = props.onSelect, // this defaults should not be invoked unless someone is using `Tab` outside of `Tabs` onSelect = _b === void 0 ? lodash_1.default.noop : _b, // this defaults should not be invoked unless someone is using `Tab` outside of `Tabs` passThroughs = __rest(props, ["isDisabled", "isLastTab", "isOpen", "isProgressive", "isSelected", "Title", "className", "count", "isVariableCountWidth", "index", "onSelect"]); var handleClick = function (event) { if (!isDisabled) { onSelect(index, props, event); } }; return (react_1.default.createElement("li", __assign({ className: cx('&-Tab', { '&-Tab-is-active': isSelected, '&-Tab-is-disabled': isDisabled, '&-Tab-is-active-and-open': isOpen && isSelected, '&-Tab-is-progressive': isProgressive && !isLastTab, }, className), onClick: handleClick }, passThroughs), react_1.default.createElement("span", { className: cx('&-Tab-content') }, Title, !lodash_1.default.isNil(count) && (react_1.default.createElement(Badge_1.default, { style: { marginLeft: '12px', width: isVariableCountWidth ? undefined : '20px', minWidth: '20px', }, type: 'stroke', kind: isSelected ? 'primary' : 'default' }, count))), isProgressive && !isLastTab && (react_1.default.createElement("span", { className: cx('&-Tab-arrow') }, react_1.default.createElement("svg", { className: cx('&-Tab-arrow-svg'), viewBox: '0 0 8 37', preserveAspectRatio: 'none' }, react_1.default.createElement("polyline", { className: cx('&-Tab-arrow-tab-line'), fill: '#fff', points: '0,0 1,1 0,1' }), react_1.default.createElement("polyline", { className: cx('&-Tab-arrow-line'), fill: 'none', stroke: '#fff', strokeWidth: '1', points: '0,37 7.3,18.5 0,0' })))))); }; Tab.peek = { description: "Content that will be rendered in a tab. Be sure to nest a `Title` inside each `Tab` or provide it as a prop. Props other than `isDisabled`, `isSelected`, and `Title` can be inferred from the parent `Tabs` component, but directly provided `props` will take precedence.", }; Tab.displayName = 'Tabs.Tab'; Tab.propName = 'Tab'; Tab.propTypes = { /** Class names that are appended to the defaults. */ className: string, /** The index of this \`Tab\` within the list of \`Tabs\`. */ index: number, /** Styles a \`Tab\` as disabled. This is typically used with \`isProgressive\` to disable steps that have not been completed and should not be selected until the current step has been completed. */ isDisabled: bool, /** If \`true\`, this \`Tab\` is the last \`Tab\` in the list of \`Tabs\`. */ isLastTab: bool, /** If \`true\` then the active \`Tab\` will appear open on the bottom. */ isOpen: bool, /** If \`true\`, the \`Tab\` will appear as a \`Progressive\` tab. */ isProgressive: bool, /** If \`true\`, the \`Tab\` will appear selected. */ isSelected: bool, /** Callback for when the user clicks a \`Tab\`. Called with the index of the \`Tab\` that was clicked. */ onSelect: func, /** The content to be rendered as the \`Title\` of the \`Tab\`. */ Title: node, /** Optional prop that will show a count number next to the tab's title. */ count: number, /** Defaults to false. Allows the count bubble to grow large. Useful if working with huge numbers. */ isVariableCountWidth: bool, }; /** TODO: Remove this constant when the component is converted to a functional component */ var nonPassThroughs = [ 'className', 'selectedIndex', 'onSelect', 'isOpen', 'isProgressive', 'isFloating', 'hasMultilineTitle', 'hasFullWidthTabs', 'Tab', ]; var Tabs = /** @class */ (function (_super) { __extends(Tabs, _super); function Tabs() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.handleClicked = function (index, tabProps, event) { var onSelect = _this.props.onSelect; onSelect && onSelect(index, { event: event, props: tabProps }); }; return _this; } Tabs.prototype.render = function () { var _this = this; var _a = this.props, className = _a.className, hasMultilineTitle = _a.hasMultilineTitle, isOpen = _a.isOpen, isProgressive = _a.isProgressive, selectedIndex = _a.selectedIndex, hasFullWidthTabs = _a.hasFullWidthTabs, isFloating = _a.isFloating, passThroughs = __rest(_a, ["className", "hasMultilineTitle", "isOpen", "isProgressive", "selectedIndex", "hasFullWidthTabs", "isFloating"]); // Grab props array from each Tab var tabChildProps = lodash_1.default.map((0, component_types_1.findTypes)(this.props, Tab), 'props'); var selectedIndexFromChildren = lodash_1.default.findLastIndex(tabChildProps, { isSelected: true, }); var actualSelectedIndex = selectedIndexFromChildren !== -1 ? selectedIndexFromChildren : selectedIndex; return (react_1.default.createElement("div", __assign({}, lodash_1.default.omit(passThroughs, nonPassThroughs), { className: cx('&', className) }), react_1.default.createElement("ul", { className: cx('&-bar', { '&-bar-is-multiline': hasMultilineTitle, '&-variable-width': !hasFullWidthTabs, '&-floating': isFloating, }) }, lodash_1.default.map(tabChildProps, function (tabProps, index) { return (react_1.default.createElement(Tab, __assign({ key: index, index: index, isLastTab: index === tabChildProps.length - 1, isOpen: isOpen, isProgressive: isProgressive, isSelected: index === actualSelectedIndex, onSelect: _this.handleClicked, Title: lodash_1.default.get((0, component_types_1.getFirst)(tabProps, Tabs.Title), 'props.children', '') }, tabProps))); })), react_1.default.createElement("div", { className: cx('&-content') }, lodash_1.default.get(tabChildProps, [ lodash_1.default.isUndefined(actualSelectedIndex) ? '' : actualSelectedIndex, 'children', ])))); }; Tabs.displayName = 'Tabs'; Tabs.Title = Title; Tabs.Tab = Tab; Tabs.reducers = reducers; Tabs.peek = { description: "`Tabs` provides tabbed navigation. It has a flexible interface that allows `tab` content to be passed as regular `React` children or through `props`.", categories: ['navigation'], }; Tabs.propTypes = { /** Class names that are appended to the defaults. */ className: string, /** Indicates which of the \`Tabs.Tab\` children is currently selected. The index of the last \`Tabs.Tab\` child with \`isSelected\` equal to \`true\` takes precedence over this prop. */ selectedIndex: number, /** Callback for when the user clicks a tab. Called with the index of the tab that was clicked. */ onSelect: func, /** If \`true\` then the active tab will appear open on the bottom. */ isOpen: bool, /** Style the tabs as a progression. This is typically used for a work flow where the user needs to move forward and backward through a series of steps. */ isProgressive: bool, /** Provides a small bottom border that offers a barrier between the tab group and the rest of the page. Useful if the tabs are not anchored to anything. */ isFloating: bool, /** Set the multiline className. This is typically used for styling the Tab.Title bar for improved readability when there are multiple React elements in the tab headers. */ hasMultilineTitle: bool, /** If \`true\` the width will be evenly distributed to all tabs. \`False\` typically used in conjunction with \`Tab.width\` */ hasFullWidthTabs: bool, Tab: any /** *Child Element* Can be used to define one or more individual \`Tab\`s in the sequence of \`Tabs\`. */, }; Tabs.defaultProps = { selectedIndex: 0, onSelect: lodash_1.default.noop, isOpen: true, isProgressive: false, isFloating: false, hasMultilineTitle: false, hasFullWidthTabs: false, }; return Tabs; }(react_1.default.Component)); exports.TabsDumb = Tabs; exports.default = (0, state_management_1.buildModernHybridComponent)(Tabs, { reducers: reducers }); //# sourceMappingURL=Tabs.js.map