@amaui/ui-react
Version:
UI for React
115 lines (114 loc) • 6.67 kB
JavaScript
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 Surface_1 = __importDefault(require("../Surface"));
const utils_2 = require("../utils");
const useStyle = (0, style_react_1.style)(theme => ({
root: {
zIndex: theme.z_index.app_bar,
width: '100%',
padding: '0'
},
fixed: {
position: 'fixed',
insetInline: '0',
bottom: '0'
}
}), { name: 'amaui-NavigationBar' });
const NavigationBar = react_1.default.forwardRef((props_, ref) => {
var _a;
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.amauiNavigationBar) === 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 Surface = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Surface) || Surface_1.default; }, [theme]);
const { color = 'primary', tonal = true, version = 'regular', value, valueDefault, onChange, fixed, className, style, children: children_ } = props, other = __rest(props, ["color", "tonal", "version", "value", "valueDefault", "onChange", "fixed", "className", "style", "children"]);
const { classes } = useStyle();
const [init, setInit] = react_1.default.useState(false);
const [selected, setSelected] = react_1.default.useState(() => {
const valueNew = valueDefault !== undefined ? valueDefault : value;
return valueNew !== undefined ? (0, utils_1.is)('array', valueNew) ? valueNew : [valueNew] : [];
});
const styles = {
root: {},
icon: {}
};
react_1.default.useEffect(() => {
setInit(true);
}, []);
react_1.default.useEffect(() => {
if (init && value !== selected)
setSelected((0, utils_1.is)('array', value) ? value : [value]);
}, [value]);
const onSelect = (itemProps) => {
// Only one item at the time is selectable,
// and it cannot be unselected by clicking on the same item
const valueNew = [itemProps.value];
if (valueNew !== undefined) {
// Update inner or controlled
if (!props.hasOwnProperty('value'))
setSelected(valueNew);
if ((0, utils_1.is)('function', onChange))
onChange(valueNew);
}
};
let palette;
if (!theme.palette.color[color] && !['themed', 'inverted', 'default', 'inherit'].includes(color)) {
palette = theme.methods.color(color);
}
if (!theme.palette.color[color] && !['themed', 'inverted', 'default', 'inherit'].includes(color)) {
if (tonal)
styles.root.backgroundColor = theme.methods.palette.color.value(undefined, 95, true, palette);
else
styles.root.backgroundColor = palette.main;
}
if (!tonal) {
let background = (_a = theme.palette.color[color]) === null || _a === void 0 ? void 0 : _a.main;
if (color === 'default')
background = theme.palette.background.default.primary;
styles.icon.color = theme.methods.palette.color.text((palette === null || palette === void 0 ? void 0 : palette.main) || background, true, 'light');
}
else {
styles.icon.color = theme.methods.palette.color.value(color, 5, true, palette);
}
const children = react_1.default.Children
.toArray(children_)
.map((item, index) => {
var _a, _b;
return react_1.default.cloneElement(item, Object.assign(Object.assign({ key: index }, (['amaui-NavigationItem'].includes((_a = item.type) === null || _a === void 0 ? void 0 : _a.displayName) ? Object.assign(Object.assign({}, other), { version }) : {})), { color: item.props.color !== undefined ? item.props.color : (['amaui-NavigationItem'].includes((_b = item.type) === null || _b === void 0 ? void 0 : _b.displayName) ? color : styles.icon.color), tonal: item.props.tonal !== undefined ? item.props.tonal : tonal, selected: selected.includes(item.props.value), onClick: () => {
if (!item.props.disabled) {
onSelect(item.props);
// Invoke items on click method
if ((0, utils_1.is)('function', item.props.onClick))
item.props.onClick();
}
} }));
});
return ((0, jsx_runtime_1.jsx)(Surface, Object.assign({ ref: ref, Component: Line, color: color, tonal: tonal, direction: 'row', align: 'center', justify: 'initial', gap: 1, className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('NavigationBar', theme) && [
'amaui-NavigationBar-root'
],
className,
classes.root,
fixed && classes.fixed
]), style: Object.assign(Object.assign({}, style), styles.root) }, other, { children: children })));
});
NavigationBar.displayName = 'amaui-NavigationBar';
exports.default = NavigationBar;
;