@onesy/ui-react
Version:
UI for React
161 lines • 7.78 kB
JavaScript
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(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) { _defineProperty(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; }
import React from 'react';
import { is } from '@onesy/utils';
import { classNames, style as styleMethod, useOnesyTheme } from '@onesy/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: 'onesy-Label'
});
const Label = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useOnesyTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyLabel?.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) && ['onesy-Label-root', `onesy-Label-version-${version}`, `onesy-Label-size-${size}`, disabled && `onesy-Label-disabled`], LabelProps?.className, className, classes.root, disabled && classes.disabled])
}), Input && /*#__PURE__*/React.cloneElement(Input, {
className: classNames([staticClassName('Label', theme) && ['onesy-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) && ['onesy-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) && ['onesy-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) && ['onesy-Label-footer'], classes.footer])
}, helperText && /*#__PURE__*/React.createElement(Type, {
version: "b3",
className: classNames([staticClassName('Label', theme) && ['onesy-Label-helper-text', error && 'onesy-Label-error'], classes.helperText, error && classes.error_color])
}, helperText)), footer_));
});
Label.displayName = 'onesy-Label';
export default Label;