UNPKG

lucid-ui

Version:

A UI component library from Xandr.

129 lines 5.66 kB
"use strict"; 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 }); var lodash_1 = __importStar(require("lodash")); var react_1 = __importDefault(require("react")); var prop_types_1 = __importDefault(require("prop-types")); var react_transition_group_1 = require("react-transition-group"); var style_helpers_1 = require("../../util/style-helpers"); var component_types_1 = require("../../util/component-types"); var Switch_1 = __importDefault(require("../Switch/Switch")); var react_2 = require("react"); var react_3 = require("react"); var cx = style_helpers_1.lucidClassNames.bind('&-SwitchLabeled'); var any = prop_types_1.default.any, node = prop_types_1.default.node, object = prop_types_1.default.object, string = prop_types_1.default.string; var defaultProps = { isDisabled: false, isSelected: false, onSelect: lodash_1.default.noop, }; var defaultState = { _labelKey: 0, }; var SwitchLabeled = function (props) { var className = props.className, isDisabled = props.isDisabled, isSelected = props.isSelected, onSelect = props.onSelect, style = props.style, passThroughs = __rest(props, ["className", "isDisabled", "isSelected", "onSelect", "style"]); var _a = (0, react_2.useState)(defaultState), state = _a[0], setState = _a[1]; var currentLabel = lodash_1.default.get((0, component_types_1.getFirst)(props, SwitchLabeled.Label), 'props.children', null); (0, react_3.useEffect)(function () { setState({ _labelKey: state._labelKey + 1 }); }, [currentLabel]); var labelChildProps = lodash_1.default.get((0, component_types_1.getFirst)(props, SwitchLabeled.Label), 'props'); var isShown = !!labelChildProps; return (react_1.default.createElement("label", { className: cx('&', { '&-is-disabled': isDisabled, '&-is-selected': isSelected, }, className), style: style }, react_1.default.createElement(Switch_1.default, __assign({ className: className, isDisabled: isDisabled, isSelected: isSelected, onSelect: onSelect }, (0, lodash_1.omit)(passThroughs, [ 'className', 'style', 'Label', 'isDisabled', 'isSelected', 'onSelect', 'isIncludeExclude', ].concat('initialState')))), labelChildProps && (react_1.default.createElement(react_transition_group_1.CSSTransition, { in: isShown, classNames: cx('&-text'), timeout: 100, style: { position: 'relative' }, className: cx('&-text'), unmountOnExit: true }, react_1.default.createElement("span", { key: state._labelKey }, labelChildProps.children || labelChildProps))))); }; SwitchLabeled.defaultProps = defaultProps; SwitchLabeled.displayName = 'SwitchLabeled'; SwitchLabeled.peek = { description: "A composite of the `Switch` component and the native `label` element.", categories: ['controls', 'toggles'], madeFrom: ['Switch'], }; var Label = function () { return null; }; Label.displayName = 'SwitchLabeled.Label'; Label.peek = { description: "Label to be shown alongside the `Switch`.", }; Label.propName = 'Label'; Label.propTypes = { /** Used to identify the purpose of this switch to the user -- can be any renderable content. */ children: node, }; SwitchLabeled.propTypes = __assign(__assign({}, Switch_1.default.propTypes), { /** Appended to the component-specific class names set on the root element. */ className: string, /** Passed through to the root element. */ style: object, /** Child element whose children are used to identify the purpose of this switch to the user. */ Label: any }); SwitchLabeled.Label = Label; exports.default = SwitchLabeled; //# sourceMappingURL=SwitchLabeled.js.map