UNPKG

@amaui/ui-react

Version:
161 lines 7.93 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } import React from 'react'; import { is } from '@amaui/utils'; import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react'; import TypeElement from '../Type'; import LineElement from '../Line'; import { staticClassName } from '../utils'; const useStyle = styleMethod(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: 'amaui-Label' }); const Label = /*#__PURE__*/React.forwardRef((props_, ref) => { const theme = useAmauiTheme(); const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiLabel?.props?.default), props_), [props_]); const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]); const Type = React.useMemo(() => theme?.elements?.Type || TypeElement, [theme]); 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 = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const checkedDefault = valueDefault_ !== undefined ? valueDefault_ : checkedDefault_; const checked = checked_; const [value, setValue] = React.useState((checkedDefault !== undefined ? checkedDefault : checked) || false); const refs = { value: React.useRef(undefined), ids: { label: React.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 (is('function', onChange)) onChange(event.target.checked, event); } }; React.useEffect(() => { if (checked !== undefined && checked !== refs.value.current) setValue(checked); }, [checked]); let position = position_; const children = React.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 => Input?.type?.displayName?.toLowerCase().includes(item)) || Input?.type?.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__*/React.createElement(Line, _extends({ gap: 0, direction: "column" }, other), /*#__PURE__*/React.createElement(Line, _extends({ ref: ref, 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: classNames([staticClassName('Label', theme) && ['amaui-Label-root', `amaui-Label-version-${version}`, `amaui-Label-size-${size}`, disabled && `amaui-Label-disabled`], LabelProps?.className, className, classes.root, disabled && classes.disabled]) }), Input && /*#__PURE__*/React.cloneElement(Input, { className: classNames([staticClassName('Label', theme) && ['amaui-Label-input'], classes.input]), tonal: Input?.props?.tonal !== undefined ? Input.props.tonal : tonal, color: Input?.props?.color !== undefined ? Input.props.color : color, colorUnchecked: Input?.props?.colorUnchecked !== undefined ? Input.props.colorUnchecked : colorUnchecked, version: Input?.props?.version !== undefined ? Input.props.version : version, size: Input?.props?.size !== undefined ? Input.props.size : size, onChange: onUpdate, checked: value, disabled }), Text?.length === 1 && is('simple', Text[0]) ? /*#__PURE__*/React.createElement(Type, _extends({ version: size === 'large' ? 'b1' : size === 'regular' ? 'b2' : 'b3', id: refs.ids.label }, TypeProps, TextProps, { className: classNames([staticClassName('Label', theme) && ['amaui-Label-text'], TextProps?.className, TypeProps?.className, classes.text, disabled && classes.text_disabled]) }), Text[0]) : /*#__PURE__*/React.createElement(Line, _extends({ direction: "row", id: refs.ids.label }, TextProps, { className: classNames([staticClassName('Label', theme) && ['amaui-Label-text-wrapper'], TextProps?.className]) }), Text?.map((item, index) => is('simple', item) ? /*#__PURE__*/React.createElement(Type, { version: size === 'large' ? 'b1' : size === 'regular' ? 'b2' : 'b3' }, item) : /*#__PURE__*/React.cloneElement(item, { key: index })))), footer && /*#__PURE__*/React.createElement(React.Fragment, null, helperText !== undefined && /*#__PURE__*/React.createElement(Line, { gap: 2, direction: "row", justify: "space-between", className: classNames([staticClassName('Label', theme) && ['amaui-Label-footer'], classes.footer]) }, helperText && /*#__PURE__*/React.createElement(Type, { version: "b3", className: classNames([staticClassName('Label', theme) && ['amaui-Label-helper-text', error && 'amaui-Label-error'], classes.helperText, error && classes.error_color]) }, helperText)), footer_)); }); Label.displayName = 'amaui-Label'; export default Label;