@amaui/ui-react
Version:
UI for React
126 lines (125 loc) • 9.01 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 Line_1 = __importDefault(require("../Line"));
const Accordion_1 = __importDefault(require("../Accordion"));
const Type_1 = __importDefault(require("../Type"));
const utils_2 = require("../utils");
const useStyle = (0, style_react_1.style)(theme => ({
root: {},
root_accordion: {
'&.amaui-Surface-root': {
background: 'transparent'
}
},
aside: {
flex: '0 0 auto'
},
divider: {
position: 'relative',
paddingBottom: theme.methods.space.value(4, 'px'),
'&::after': {
content: "''",
position: 'absolute',
bottom: '0px',
width: '100%',
height: '1px',
background: theme.palette.text.default.primary,
opacity: theme.palette.light ? '0.07' : '0.1'
},
'&:last-child': {
paddingBottom: 0,
'&::after': {
display: 'none'
}
}
},
size_small_form_accordion: {
marginTop: '16px'
},
size_regular_form_accordion: {
marginTop: '24px'
},
size_large_form_accordion: {
marginTop: '32px'
}
}), { name: 'amaui-Form' });
const Form = react_1.default.forwardRef((props_, ref) => {
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.amauiForm) === 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 Accordion = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Accordion) || Accordion_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 { accordion, divider, name, description, size = 'regular', start, end, footer, onSubmit: onSubmit_, Component = Line, ComponentForm: ComponentForm_, HeaderProps, TextProps, AsideProps, StartProps, EndProps, AccordionProps, AccordionMainProps, wrapper, className, children } = props, other = __rest(props, ["accordion", "divider", "name", "description", "size", "start", "end", "footer", "onSubmit", "Component", "ComponentForm", "HeaderProps", "TextProps", "AsideProps", "StartProps", "EndProps", "AccordionProps", "AccordionMainProps", "wrapper", "className", "children"]);
const { classes } = useStyle();
const ComponentForm = ComponentForm_ !== undefined ? ComponentForm_ : wrapper ? 'div' : 'form';
const text = (name || description);
const actions = (start || end);
const header = text || actions;
let justify = 'unset';
if (start && end && !text)
justify = 'space-between';
if (!start && !text && end)
justify = 'flex-end';
const onSubmit = ComponentForm === 'form' ? onSubmit_ || react_1.default.useCallback((event) => {
event.preventDefault();
}, []) : undefined;
const elements = {
name: name && ((0, utils_1.is)('string', name) ? ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: size === 'large' ? 't1' : size === 'regular' ? 't2' : 't3' }, { children: name }))) : name),
description: description && ((0, utils_1.is)('string', description) ? ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: size === 'large' ? 'b1' : size === 'regular' ? 'b2' : 'b3' }, { children: description }))) : description)
};
if (accordion) {
return ((0, jsx_runtime_1.jsx)(Accordion, Object.assign({ tonal: true, color: 'primary', primary: elements.name, secondary: elements.description, expandedMarginVertical: 'none', expandedHeaderPaddingVertical: 'none', headerPaddingVertical: 'none', headerPaddingHorizontal: 'none', mainPaddingVertical: 'none', mainPaddingHorizontal: 'none', start: start, end: end, HeaderProps: {
direction: 'column',
gap: size === 'large' ? 1 : size === 'regular' ? 0.5 : 0.5
} }, AccordionProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Form', theme) && [
'amaui-Form-root'
],
AccordionProps === null || AccordionProps === void 0 ? void 0 : AccordionProps.className,
className,
classes.root,
classes.root_accordion,
divider && classes.divider
]) }, other, { children: (0, jsx_runtime_1.jsxs)(Line, Object.assign({ ref: ref, gap: size === 'large' ? 3 : size === 'regular' ? 2.5 : 2, direction: 'column', justify: 'unset', align: 'flex-start', Component: ComponentForm, onSubmit: onSubmit, fullWidth: true }, AccordionMainProps, { className: (0, style_react_1.classNames)([
AccordionMainProps === null || AccordionMainProps === void 0 ? void 0 : AccordionMainProps.className,
classes[`size_${size}_form_accordion`]
]) }, { children: [children, footer && ((0, jsx_runtime_1.jsx)(Line, Object.assign({ direction: 'row', justify: 'flex-end', fullWidth: true, className: classes.footer }, { children: footer })))] })) })));
}
return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({ ref: ref, gap: size === 'large' ? 3 : size === 'regular' ? 2.5 : 2, direction: 'column', justify: 'unset', align: 'flex-start', Component: ComponentForm, onSubmit: onSubmit, fullWidth: true, className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Form', theme) && [
'amaui-Form-root',
`amaui-Form-size-${size}`
],
className,
classes.root,
divider && classes.divider
]) }, other, { children: [header && ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1, direction: 'row', align: 'center', justify: justify, fullWidth: true }, HeaderProps, { children: [start && ((0, jsx_runtime_1.jsx)(Line, Object.assign({}, AsideProps, StartProps, { className: (0, style_react_1.classNames)([
AsideProps === null || AsideProps === void 0 ? void 0 : AsideProps.className,
StartProps === null || StartProps === void 0 ? void 0 : StartProps.className,
classes.aside
]) }, { children: start }))), text && ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: size === 'large' ? 1 : size === 'regular' ? 0.5 : 0.5, direction: 'column', flex: true }, TextProps, { children: [elements.name, elements.description] }))), end && ((0, jsx_runtime_1.jsx)(Line, Object.assign({ gap: 0, direction: 'row', align: 'center' }, AsideProps, EndProps, { className: (0, style_react_1.classNames)([
AsideProps === null || AsideProps === void 0 ? void 0 : AsideProps.className,
EndProps === null || EndProps === void 0 ? void 0 : EndProps.className,
classes.aside
]) }, { children: end })))] }))), children, footer && ((0, jsx_runtime_1.jsx)(Line, Object.assign({ direction: 'row', justify: 'flex-end', fullWidth: true, className: classes.footer }, { children: footer })))] })));
});
Form.displayName = 'amaui-Form';
exports.default = Form;