@amaui/ui-react
Version:
UI for React
156 lines (155 loc) • 11.2 kB
JavaScript
"use strict";
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 });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const utils_1 = require("@amaui/utils");
const style_react_1 = require("@amaui/style-react");
const Type_1 = __importDefault(require("../Type"));
const Line_1 = __importDefault(require("../Line"));
const utils_2 = require("../utils");
const useStyle = (0, style_react_1.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: 'amaui-Label' });
const Label = react_1.default.forwardRef((props_, ref) => {
var _a, _b, _c, _d, _e, _f;
const theme = (0, style_react_1.useAmauiTheme)();
const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiLabel) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]);
const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]);
const Type = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Type) || Type_1.default; }, [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 = __rest(props, ["tonal", "color", "colorUnchecked", "version", "position", "size", "input", "name", "label", "valueDefault", "checkedDefault", "checked", "onChange", "error", "helperText", "footer", "disabled", "LabelProps", "TypeProps", "TextProps", "Component", "className", "children"]);
const { classes } = useStyle();
const checkedDefault = valueDefault_ !== undefined ? valueDefault_ : checkedDefault_;
const checked = checked_;
const [value, setValue] = react_1.default.useState((checkedDefault !== undefined ? checkedDefault : checked) || false);
const refs = {
value: react_1.default.useRef(undefined),
ids: {
label: react_1.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_1.is)('function', onChange))
onChange(event.target.checked, event);
}
};
react_1.default.useEffect(() => {
if (checked !== undefined && checked !== refs.value.current)
setValue(checked);
}, [checked]);
let position = position_;
const children = react_1.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 _a, _b; return (_b = (_a = Input === null || Input === void 0 ? void 0 : Input.type) === null || _a === void 0 ? void 0 : _a.displayName) === null || _b === void 0 ? void 0 : _b.toLowerCase().includes(item); }) || ((_a = Input === null || Input === void 0 ? void 0 : Input.type) === null || _a === void 0 ? void 0 : _a.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 ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 0, direction: 'column' }, other, { children: [(0, jsx_runtime_1.jsxs)(Line, Object.assign({ 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: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Label', theme) && [
'amaui-Label-root',
`amaui-Label-version-${version}`,
`amaui-Label-size-${size}`,
disabled && `amaui-Label-disabled`
],
LabelProps === null || LabelProps === void 0 ? void 0 : LabelProps.className,
className,
classes.root,
disabled && classes.disabled
]) }, { children: [Input && react_1.default.cloneElement(Input, {
className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Label', theme) && [
'amaui-Label-input'
],
classes.input
]),
tonal: ((_b = Input === null || Input === void 0 ? void 0 : Input.props) === null || _b === void 0 ? void 0 : _b.tonal) !== undefined ? Input.props.tonal : tonal,
color: ((_c = Input === null || Input === void 0 ? void 0 : Input.props) === null || _c === void 0 ? void 0 : _c.color) !== undefined ? Input.props.color : color,
colorUnchecked: ((_d = Input === null || Input === void 0 ? void 0 : Input.props) === null || _d === void 0 ? void 0 : _d.colorUnchecked) !== undefined ? Input.props.colorUnchecked : colorUnchecked,
version: ((_e = Input === null || Input === void 0 ? void 0 : Input.props) === null || _e === void 0 ? void 0 : _e.version) !== undefined ? Input.props.version : version,
size: ((_f = Input === null || Input === void 0 ? void 0 : Input.props) === null || _f === void 0 ? void 0 : _f.size) !== undefined ? Input.props.size : size,
onChange: onUpdate,
checked: value,
disabled
}), (Text === null || Text === void 0 ? void 0 : Text.length) === 1 && (0, utils_1.is)('simple', Text[0]) ? ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: size === 'large' ? 'b1' : size === 'regular' ? 'b2' : 'b3', id: refs.ids.label }, TypeProps, TextProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Label', theme) && [
'amaui-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] }))) : ((0, jsx_runtime_1.jsx)(Line, Object.assign({ direction: 'row', id: refs.ids.label }, TextProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Label', theme) && [
'amaui-Label-text-wrapper'
],
TextProps === null || TextProps === void 0 ? void 0 : TextProps.className
]) }, { children: Text === null || Text === void 0 ? void 0 : Text.map((item, index) => (0, utils_1.is)('simple', item) ?
(0, jsx_runtime_1.jsx)(Type, Object.assign({ version: size === 'large' ? 'b1' : size === 'regular' ? 'b2' : 'b3' }, { children: item })) :
react_1.default.cloneElement(item, { key: index })) })))] })), footer && (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(helperText !== undefined) && ((0, jsx_runtime_1.jsx)(Line, Object.assign({ gap: 2, direction: 'row', justify: 'space-between', className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Label', theme) && [
'amaui-Label-footer'
],
classes.footer
]) }, { children: helperText && ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'b3', className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Label', theme) && [
'amaui-Label-helper-text',
error && 'amaui-Label-error'
],
classes.helperText,
error && classes.error_color
]) }, { children: helperText }))) }))), footer_] })] })));
});
Label.displayName = 'amaui-Label';
exports.default = Label;