UNPKG

@onesy/ui-react

Version:
181 lines (180 loc) 10.9 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireDefault(require("react")); var _utils = require("@onesy/utils"); var _styleReact = require("@onesy/style-react"); var _Type = _interopRequireDefault(require("../Type")); var _Line = _interopRequireDefault(require("../Line")); var _utils2 = require("../utils"); var _jsxRuntime = require("react/jsx-runtime"); const _excluded = ["tonal", "color", "colorUnchecked", "version", "position", "size", "input", "name", "label", "valueDefault", "checkedDefault", "checked", "onChange", "error", "helperText", "footer", "disabled", "LabelProps", "TypeProps", "TextProps", "Component", "className", "children"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } const useStyle = (0, _styleReact.style)(theme => ({ root: { cursor: 'pointer', userSelect: 'none' }, text_disabled: { opacity: theme.palette.visual_contrast.default.opacity.disabled }, error_color: { color: [theme.palette.light ? theme.palette.color.error[40] : theme.palette.color.error[80], '!important'] }, footer: { marginTop: '4px', paddingInline: theme.methods.space.value(2, 'px') }, footer_version_text: { paddingInline: '0' }, helperText: { display: 'inline-flex', color: theme.palette.text.default.secondary, userSelect: 'none' }, disabled: { cursor: 'default', pointerEvents: 'none' } }), { name: 'onesy-Label' }); const Label = props_ => { var _theme$ui, _theme$ui2, _theme$elements, _theme$elements2, _Input$type2, _Input$props, _Input$props2, _Input$props3, _Input$props4, _Input$props5; const theme = (0, _styleReact.useOnesyTheme)(); const props = _objectSpread(_objectSpread(_objectSpread({}, theme === null || theme === void 0 || (_theme$ui = theme.ui) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.elements) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.all) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.props) === null || _theme$ui === void 0 ? void 0 : _theme$ui.default), theme === null || theme === void 0 || (_theme$ui2 = theme.ui) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.elements) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.onesyLabel) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.props) === null || _theme$ui2 === void 0 ? void 0 : _theme$ui2.default), props_); const Line = (theme === null || theme === void 0 || (_theme$elements = theme.elements) === null || _theme$elements === void 0 ? void 0 : _theme$elements.Line) || _Line.default; const Type = (theme === null || theme === void 0 || (_theme$elements2 = theme.elements) === null || _theme$elements2 === void 0 ? void 0 : _theme$elements2.Type) || _Type.default; const { tonal = true, color, colorUnchecked, version, position: position_, size = 'regular', input, name, label: label_, valueDefault: valueDefault_, checkedDefault: checkedDefault_, checked: checked_, onChange, error, helperText, footer: footer_, disabled, LabelProps, TypeProps, TextProps, Component = 'label', className, children: children_ } = props, other = (0, _objectWithoutProperties2.default)(props, _excluded); const { classes } = useStyle(); const checkedDefault = valueDefault_ !== undefined ? valueDefault_ : checkedDefault_; const checked = checked_; const [value, setValue] = _react.default.useState((checkedDefault !== undefined ? checkedDefault : checked) || false); const refs = { value: _react.default.useRef(undefined), ids: { label: _react.default.useId() } }; refs.value.current = value; const label = name !== undefined ? name : label_; const onUpdate = (value__, event) => { if (!disabled && event) { // Inner controlled checkbox if (!props.hasOwnProperty('checked')) setValue(event.target.checked); if ((0, _utils.is)('function', onChange)) onChange(event.target.checked, event); } }; _react.default.useEffect(() => { if (checked !== undefined && checked !== refs.value.current) setValue(checked); }, [checked]); let position = position_; const children = _react.default.Children.toArray(children_); const Input = input !== undefined ? input : children[0]; const Text = label !== undefined ? label : name !== undefined ? name : children.slice(1); const inlineElement = ['checkbox', 'radio', 'switch'].some(item => { var _Input$type; return Input === null || Input === void 0 || (_Input$type = Input.type) === null || _Input$type === void 0 || (_Input$type = _Input$type.displayName) === null || _Input$type === void 0 ? void 0 : _Input$type.toLowerCase().includes(item); }) || (Input === null || Input === void 0 || (_Input$type2 = Input.type) === null || _Input$type2 === void 0 ? void 0 : _Input$type2.displayName) === undefined; let align = 'center'; const justify = 'center'; if (position === undefined) position = inlineElement ? 'start' : 'bottom'; if (!inlineElement && !['start', 'end'].includes(position)) align = 'flex-start'; const footer = footer_ || helperText; let gap = size === 'small' ? 1 : size === 'regular' ? 1.5 : 2; if (version === 'text') gap = size === 'small' ? 0.25 : size === 'regular' ? 0.5 : 0.75; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Line, _objectSpread(_objectSpread({ gap: 0, direction: "column" }, other), {}, { children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(Line, _objectSpread(_objectSpread({ gap: gap, direction: ['top', 'bottom'].includes(position) ? position === 'bottom' ? 'column-reverse' : 'column' : position === 'end' ? 'row-reverse' : 'row', align: align, justify: justify, role: "group", "aria-labelledby": refs.ids.label, "aria-disabled": disabled, disabled: disabled, Component: Component }, LabelProps), {}, { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Label', theme) && ['onesy-Label-root', `onesy-Label-version-${version}`, `onesy-Label-size-${size}`, disabled && `onesy-Label-disabled`], LabelProps === null || LabelProps === void 0 ? void 0 : LabelProps.className, className, classes.root, disabled && classes.disabled]), children: [Input && /*#__PURE__*/_react.default.cloneElement(Input, { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Label', theme) && ['onesy-Label-input'], classes.input]), tonal: (Input === null || Input === void 0 || (_Input$props = Input.props) === null || _Input$props === void 0 ? void 0 : _Input$props.tonal) !== undefined ? Input.props.tonal : tonal, color: (Input === null || Input === void 0 || (_Input$props2 = Input.props) === null || _Input$props2 === void 0 ? void 0 : _Input$props2.color) !== undefined ? Input.props.color : color, colorUnchecked: (Input === null || Input === void 0 || (_Input$props3 = Input.props) === null || _Input$props3 === void 0 ? void 0 : _Input$props3.colorUnchecked) !== undefined ? Input.props.colorUnchecked : colorUnchecked, version: (Input === null || Input === void 0 || (_Input$props4 = Input.props) === null || _Input$props4 === void 0 ? void 0 : _Input$props4.version) !== undefined ? Input.props.version : version, size: (Input === null || Input === void 0 || (_Input$props5 = Input.props) === null || _Input$props5 === void 0 ? void 0 : _Input$props5.size) !== undefined ? Input.props.size : size, onChange: onUpdate, checked: value, disabled }), (Text === null || Text === void 0 ? void 0 : Text.length) === 1 && (0, _utils.is)('simple', Text[0]) ? /*#__PURE__*/(0, _jsxRuntime.jsx)(Type, _objectSpread(_objectSpread(_objectSpread({ version: size === 'large' ? 'b1' : size === 'regular' ? 'b2' : 'b3', id: refs.ids.label }, TypeProps), TextProps), {}, { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Label', theme) && ['onesy-Label-text'], TextProps === null || TextProps === void 0 ? void 0 : TextProps.className, TypeProps === null || TypeProps === void 0 ? void 0 : TypeProps.className, classes.text, disabled && classes.text_disabled]), children: Text[0] })) : /*#__PURE__*/(0, _jsxRuntime.jsx)(Line, _objectSpread(_objectSpread({ direction: "row", id: refs.ids.label }, TextProps), {}, { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Label', theme) && ['onesy-Label-text-wrapper'], TextProps === null || TextProps === void 0 ? void 0 : TextProps.className]), children: Text === null || Text === void 0 ? void 0 : Text.map((item_0, index) => (0, _utils.is)('simple', item_0) ? /*#__PURE__*/(0, _jsxRuntime.jsx)(Type, { version: size === 'large' ? 'b1' : size === 'regular' ? 'b2' : 'b3', children: item_0 }) : /*#__PURE__*/_react.default.cloneElement(item_0, { key: index })) }))] })), footer && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [helperText !== undefined && /*#__PURE__*/(0, _jsxRuntime.jsx)(Line, { gap: 2, direction: "row", justify: "space-between", className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Label', theme) && ['onesy-Label-footer'], classes.footer]), children: helperText && /*#__PURE__*/(0, _jsxRuntime.jsx)(Type, { version: "b3", className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Label', theme) && ['onesy-Label-helper-text', error && 'onesy-Label-error'], classes.helperText, error && classes.error_color]), children: helperText }) }), footer_] })] })); }; Label.displayName = 'onesy-Label'; var _default = exports.default = Label;