UNPKG

@onesy/ui-react

Version:
375 lines (372 loc) 17.4 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireDefault(require("react")); var _utils = require("@onesy/utils"); var _styleReact = require("@onesy/style-react"); var _IconMaterialExpandMoreW = _interopRequireDefault(require("@onesy/icons-material-rounded-react/IconMaterialExpandMoreW100")); var _Checkbox = _interopRequireDefault(require("../Checkbox")); var _Surface = _interopRequireDefault(require("../Surface")); var _Type = _interopRequireDefault(require("../Type")); var _Fade = _interopRequireDefault(require("../Fade")); var _Expand = _interopRequireDefault(require("../Expand")); var _Interaction = _interopRequireDefault(require("../Interaction")); var _Line = _interopRequireDefault(require("../Line")); var _utils2 = require("../utils"); var _jsxRuntime = require("react/jsx-runtime"); const _excluded = ["ref", "tonal", "color", "version", "open", "openDefault", "onChange", "selected", "line", "indicator", "arrow", "checkbox", "indicatorPosition", "level", "icon", "iconOpen", "start", "middle", "end", "button", "noTransition", "noExpand", "noPadding", "parentDisabled", "disabled", "ExpandProps", "MainProps", "StartProps", "MiddleProps", "EndProps", "IndicatorProps", "TransitionComponentProps", "TreeProps", "IconArrow", "TransitionComponent", "Component", "className", "style", "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) { (0, _defineProperty2.default)(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; } const useStyle = (0, _styleReact.style)(theme => ({ root: { position: 'relative' }, line: { '&::before': { content: "''", position: 'absolute', insetInlineStart: '-12px', height: '100%', width: '2px', background: theme.palette.light ? theme.palette.color.neutral[90] : theme.palette.color.neutral[40] }, '&:last-of-type': { '&::before': { height: '2px' } } }, main: { // Reset fontFamily: 'inherit', background: 'transparent', border: 'none', fontSize: '100%', lineHeight: '1.15', margin: '0', position: 'relative', padding: `${theme.methods.space.value(0.25, 'px')} ${theme.methods.space.value(1, 'px')}`, borderRadius: `${theme.shape.radius.unit * 0.5}px` }, main_line: { '&::before': { content: "''", position: 'absolute', top: 'calc(50% - 15px)', insetInlineStart: '-12px', width: '10px', height: '14px', borderLeft: '2px solid', borderBottom: '2px solid', color: theme.palette.light ? theme.palette.color.neutral[90] : theme.palette.color.neutral[40], borderColor: theme.palette.light ? theme.palette.color.neutral[90] : theme.palette.color.neutral[40], borderBottomLeftRadius: '6px', transform: `rotateY(${theme.direction === 'ltr' ? 0 : 180}deg)` } }, item: { padding: `${theme.methods.space.value(0.25, 'px')} 0` }, middle: { flex: '1 1 auto' }, indicator: { transition: theme.methods.transitions.make('transform') }, indicator_open: { transform: 'rotate(-180deg)' }, button: { // Reset fontFamily: 'inherit', '-webkit-appearance': 'none', appearance: 'none', color: 'inherit', background: 'transparent', border: 'none', fontSize: '100%', lineHeight: '1.15', padding: '0', margin: '0', overflow: 'visible', textTransform: 'none', userSelect: 'none', cursor: 'pointer' }, disabled: { pointerEvents: 'none', cursor: 'default', opacity: theme.palette.visual_contrast.default.opacity.disabled } }), { name: 'onesy-Tree' }); const TreeDelays = { Transition: { enter: 70 } }; const Tree = props_ => { var _theme$ui, _theme$ui2, _theme$elements, _theme$elements2, _theme$elements3, _theme$elements4, _theme$elements5, _theme$elements6, _theme$elements7; const theme = (0, _styleReact.useOnesyTheme)(); const props = _objectSpread(_objectSpread(_objectSpread({}, theme === null || theme === void 0 || (_theme$ui = theme.ui) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.elements) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.all) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.props) === null || _theme$ui === void 0 ? void 0 : _theme$ui.default), theme === null || theme === void 0 || (_theme$ui2 = theme.ui) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.elements) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.onesyTree) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.props) === null || _theme$ui2 === void 0 ? void 0 : _theme$ui2.default), props_); const Line = (theme === null || theme === void 0 || (_theme$elements = theme.elements) === null || _theme$elements === void 0 ? void 0 : _theme$elements.Line) || _Line.default; const Checkbox = (theme === null || theme === void 0 || (_theme$elements2 = theme.elements) === null || _theme$elements2 === void 0 ? void 0 : _theme$elements2.Checkbox) || _Checkbox.default; const Surface = (theme === null || theme === void 0 || (_theme$elements3 = theme.elements) === null || _theme$elements3 === void 0 ? void 0 : _theme$elements3.Surface) || _Surface.default; const Type = (theme === null || theme === void 0 || (_theme$elements4 = theme.elements) === null || _theme$elements4 === void 0 ? void 0 : _theme$elements4.Type) || _Type.default; const Fade = (theme === null || theme === void 0 || (_theme$elements5 = theme.elements) === null || _theme$elements5 === void 0 ? void 0 : _theme$elements5.Fade) || _Fade.default; const Expand = (theme === null || theme === void 0 || (_theme$elements6 = theme.elements) === null || _theme$elements6 === void 0 ? void 0 : _theme$elements6.Expand) || _Expand.default; const Interaction = (theme === null || theme === void 0 || (_theme$elements7 = theme.elements) === null || _theme$elements7 === void 0 ? void 0 : _theme$elements7.Interaction) || _Interaction.default; const { ref, tonal = true, color = 'inherit', version = 'text', open: open_, openDefault, onChange, selected, line, indicator, arrow = true, checkbox, indicatorPosition = 'start', level = 0, icon, iconOpen, start: start_, middle, end: end_, button = true, noTransition, noExpand, noPadding, parentDisabled, disabled, ExpandProps, MainProps: MainProps_, StartProps, MiddleProps, EndProps, IndicatorProps, TransitionComponentProps: TransitionComponentProps_, TreeProps, IconArrow = _IconMaterialExpandMoreW.default, TransitionComponent: TransitionComponent_ = Fade, Component = 'div', className, style, children: children_ } = props, other = (0, _objectWithoutProperties2.default)(props, _excluded); const { classes } = useStyle(); const [init, setInit] = _react.default.useState(false); const [focus, setFocus] = _react.default.useState(false); const [open, setOpen] = _react.default.useState(openDefault !== undefined ? openDefault : open_); const refs = { root: _react.default.useRef(undefined), ids: { middle: _react.default.useId() }, inProgressTransition: _react.default.useRef(null) }; const styles = { root: {} }; let TransitionComponent = TransitionComponent_; let TransitionComponentProps = _objectSpread({ add: true, delay: { enter: TreeDelays.Transition.enter } }, TransitionComponentProps_); _react.default.useEffect(() => { setInit(true); }, []); _react.default.useEffect(() => { if (init) { if (open_ !== open) setOpen(open_); } }, [open_]); const onKeyDown = event => { var _refs$root$current; const rootDocument = (0, _utils.isEnvironment)('browser') ? ((_refs$root$current = refs.root.current) === null || _refs$root$current === void 0 ? void 0 : _refs$root$current.ownerDocument) || window.document : undefined; if (level === 0) { let allElements = []; let index = 0; if (['ArrowUp', 'ArrowDown'].includes(event.key)) { allElements = Array.from(refs.root.current.querySelectorAll(`[tabindex='0']`)); } switch (event.key) { case 'ArrowUp': case 'ArrowDown': index = (0, _utils.clamp)(allElements.findIndex(item => item === rootDocument.activeElement), 0); event.key === 'ArrowDown' ? index++ : index--; allElements[(0, _utils.clamp)(index, 0, allElements.length - 1)].focus(); event.preventDefault(); return; default: break; } } }; const onClick = event_0 => { if ((0, _utils.is)('function', MiddleProps === null || MiddleProps === void 0 ? void 0 : MiddleProps.onClick)) MiddleProps.onClick(event_0); if (refs.inProgressTransition.current) return; if (!disabled && !noExpand && children_) { const valueNew = !open; // Update inner or controlled if (!props.hasOwnProperty('open')) setOpen(valueNew); if ((0, _utils.is)('function', onChange)) onChange(valueNew); } }; if (!noTransition) TransitionComponentProps.in = open;else { TransitionComponent = _react.default.Fragment; TransitionComponentProps = {}; } const onBlur = event_1 => { if (!disabled) setFocus(false); }; const onFocus = event_2 => { if (!disabled) setFocus(true); }; const start = _react.default.Children.toArray(start_); // Icon if (!open ? icon : iconOpen || icon) start.push(!open ? icon : iconOpen || icon); const end = _react.default.Children.toArray(end_); if (indicator && button && children_ && !noExpand) { let Indicator; if (arrow) { Indicator = /*#__PURE__*/(0, _jsxRuntime.jsx)(IconArrow, _objectSpread({ className: (0, _styleReact.classNames)([classes.indicator, open && classes.indicator_open]) }, IndicatorProps)); } if (checkbox) { Indicator = /*#__PURE__*/(0, _jsxRuntime.jsx)(Checkbox, _objectSpread({ size: "small", checked: open }, IndicatorProps)); } if (indicatorPosition === 'start') start.unshift(Indicator);else if (indicatorPosition === 'end') end.push(Indicator); } const MainProps = _objectSpread({}, MainProps_); if (!disabled) { MainProps.onBlur = onBlur; MainProps.onFocus = onFocus; } if (level > 0 && !noPadding) { styles.root.marginInlineStart = `${theme.space.unit * 2.5}px`; } const children = _react.default.Children.toArray(children_).map(item_0 => (/*#__PURE__*/_react.default.cloneElement(item_0, { tonal: item_0.props.tonal !== undefined ? item_0.props.tonal : tonal, color: item_0.props.color !== undefined ? item_0.props.color : color, version: item_0.props.version !== undefined ? item_0.props.version : version, line: item_0.props.line !== undefined ? item_0.props.line : line, checkbox: item_0.props.checkbox !== undefined ? item_0.props.checkbox : checkbox, IconArrow: item_0.props.IconArrow !== undefined ? item_0.props.IconArrow : IconArrow, indicator: item_0.props.indicator !== undefined ? item_0.props.indicator : indicator, indicatorPosition: item_0.props.indicatorPosition !== undefined ? item_0.props.indicatorPosition : indicatorPosition, noExpand: item_0.props.noExpand !== undefined ? item_0.props.noExpand : noExpand, noTransition: item_0.props.noTransition !== undefined ? item_0.props.noTransition : noTransition, parentDisabled: item_0.props.parentDisabled !== undefined ? item_0.props.parentDisabled : parentDisabled || disabled, level: level + 1 }))); return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Surface, _objectSpread(_objectSpread({ ref: item_1 => { if (ref) { if ((0, _utils.is)('function', ref)) ref(item_1);else if (ref !== null && ref !== void 0 && ref.current) ref.current = item_1; } refs.root.current = item_1; }, tonal: tonal, color: color, version: version, gap: 0, align: "unset", justify: "unset", onKeyDown: onKeyDown, role: level === 0 ? 'tree' : 'treeitem', "aria-labelledby": refs.ids.middle, "aria-expanded": open, "aria-selected": selected, Component: Line, AdditionalProps: { Component }, className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Tree', theme) && ['onesy-Tree-root', children ? `onesy-Tree-children` : `onesy-Tree-empty`, parentDisabled && `onesy-Tree-parent-disabled`, disabled && `onesy-Tree-disabled`], className, classes.root, level > 0 && line && classes.line, disabled && classes.disabled]), style: _objectSpread(_objectSpread({}, styles.root), style) }, other), {}, { children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(Line, _objectSpread(_objectSpread({ gap: 1, tabIndex: !disabled && !parentDisabled ? 0 : -1, onClick: onClick, direction: "row", align: "center", justify: "unset", role: button && children ? 'button' : undefined, Component: button && children ? 'button' : 'div' }, MainProps), {}, { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Tree', theme) && ['onesy-Tree-main'], MainProps === null || MainProps === void 0 ? void 0 : MainProps.className, classes.main, button && classes.button, level > 0 && line && classes.main_line]), children: [button && /*#__PURE__*/(0, _jsxRuntime.jsx)(Interaction, { selected: selected, pulse: focus }), !!start.length && /*#__PURE__*/(0, _jsxRuntime.jsx)(Line, _objectSpread(_objectSpread({ gap: 0, direction: "row", align: "center", justify: "unset" }, StartProps), {}, { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Tree', theme) && ['onesy-Tree-item', 'onesy-Tree-aside', 'onesy-Tree-start'], StartProps === null || StartProps === void 0 ? void 0 : StartProps.className, classes.item, classes.aside, classes.start]), children: start.map((item_2, index_0) => (/*#__PURE__*/_react.default.cloneElement(item_2, { key: index_0 }))) })), middle && /*#__PURE__*/(0, _jsxRuntime.jsx)(Line, _objectSpread(_objectSpread({ gap: 0, id: refs.ids.middle, Component: (0, _utils.is)('simple', middle) ? Type : undefined }, MiddleProps), {}, { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Tree', theme) && ['onesy-Tree-item', 'onesy-Tree-middle', 'onesy-Tree-end'], MiddleProps === null || MiddleProps === void 0 ? void 0 : MiddleProps.className, classes.item, classes.middle]), children: middle })), !!end.length && /*#__PURE__*/(0, _jsxRuntime.jsx)(Line, _objectSpread(_objectSpread({ gap: 0, direction: "row", align: "center", justify: "unset" }, EndProps), {}, { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Tree', theme) && ['onesy-Tree-item', 'onesy-Tree-aside', 'onesy-Tree-end'], EndProps === null || EndProps === void 0 ? void 0 : EndProps.className, classes.item, classes.aside, classes.end]), children: end.map((item_3, index_1) => (/*#__PURE__*/_react.default.cloneElement(item_3, { key: index_1 }))) }))] })), noExpand && children, !noExpand && children && /*#__PURE__*/(0, _jsxRuntime.jsx)(Expand, _objectSpread(_objectSpread({ in: open, parent: parent || refs.root.current, onTransition: (element, status) => { refs.inProgressTransition.current = !['entered', 'removed'].includes(status); } }, ExpandProps), {}, { children: /*#__PURE__*/(0, _jsxRuntime.jsx)(TransitionComponent, _objectSpread(_objectSpread({}, TransitionComponentProps), {}, { children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Line, _objectSpread(_objectSpread({ gap: 0, align: "unset", justify: "unset", role: "group" }, TreeProps), {}, { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Tree', theme) && ['onesy-Tree-tree'], TreeProps === null || TreeProps === void 0 ? void 0 : TreeProps.className, classes.tree]), children: children.map((item_4, index_2) => (/*#__PURE__*/_react.default.cloneElement(item_4, { key: index_2 }))) })) })) }))] })); }; Tree.displayName = 'onesy-Tree'; var _default = exports.default = Tree;