@amaui/ui-react
Version:
UI for React
353 lines (351 loc) • 14.2 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", "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(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import React from 'react';
import { clamp, is, isEnvironment } from '@amaui/utils';
import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react';
import IconMaterialExpandMore from '@amaui/icons-material-rounded-react/IconMaterialExpandMoreW100';
import CheckboxElement from '../Checkbox';
import SurfaceElement from '../Surface';
import TypeElement from '../Type';
import FadeElement from '../Fade';
import ExpandElement from '../Expand';
import InteractionElement from '../Interaction';
import LineElement from '../Line';
import { staticClassName } from '../utils';
const useStyle = styleMethod(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['20']
},
'&:last-of-type': {
'&::before': {
height: '2px'
}
}
},
main: {
// Reset
fontFamily: 'inherit',
background: 'transparent',
border: 'none',
fontSize: '100%',
lineHeight: '1.15',
margin: '0',
position: 'relative',
color: 'inherit',
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',
borderColor: theme.palette.light ? theme.palette.color.neutral['90'] : theme.palette.color.neutral['20'],
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',
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: 'amaui-Tree'
});
const TreeDelays = {
Transition: {
enter: 70
}
};
const Tree = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useAmauiTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiTree?.props?.default), props_), [props_]);
const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]);
const Checkbox = React.useMemo(() => theme?.elements?.Checkbox || CheckboxElement, [theme]);
const Surface = React.useMemo(() => theme?.elements?.Surface || SurfaceElement, [theme]);
const Type = React.useMemo(() => theme?.elements?.Type || TypeElement, [theme]);
const Fade = React.useMemo(() => theme?.elements?.Fade || FadeElement, [theme]);
const Expand = React.useMemo(() => theme?.elements?.Expand || ExpandElement, [theme]);
const Interaction = React.useMemo(() => theme?.elements?.Interaction || InteractionElement, [theme]);
const {
tonal = true,
color = 'default',
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 = IconMaterialExpandMore,
TransitionComponent: TransitionComponent_ = Fade,
Component = 'div',
className,
style,
children: children_
} = props,
other = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const [init, setInit] = React.useState(false);
const [focus, setFocus] = React.useState(false);
const [open, setOpen] = React.useState(openDefault !== undefined ? openDefault : open_);
const refs = {
root: React.useRef(undefined),
ids: {
middle: React.useId()
},
inProgressTransition: React.useRef()
};
const styles = {
root: {}
};
let TransitionComponent = TransitionComponent_;
let TransitionComponentProps = _objectSpread({
add: true,
delay: {
enter: TreeDelays.Transition.enter
}
}, TransitionComponentProps_);
React.useEffect(() => {
setInit(true);
}, []);
React.useEffect(() => {
if (init) {
if (open_ !== open) setOpen(open_);
}
}, [open_]);
const onKeyDown = event => {
const rootDocument = isEnvironment('browser') ? 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 = clamp(allElements.findIndex(item => item === rootDocument.activeElement), 0);
event.key === 'ArrowDown' ? index++ : index--;
allElements[clamp(index, 0, allElements.length - 1)].focus();
event.preventDefault();
return;
default:
break;
}
}
};
const onClick = React.useCallback(event => {
if (is('function', MiddleProps?.onClick)) MiddleProps.onClick(event);
if (refs.inProgressTransition.current) return;
if (!disabled && !noExpand && children_) {
const valueNew = !open;
// Update inner or controlled
if (!props.hasOwnProperty('open')) setOpen(valueNew);
if (is('function', onChange)) onChange(valueNew);
}
}, [open, noExpand, children_, disabled]);
if (!noTransition) TransitionComponentProps.in = open;else {
TransitionComponent = React.Fragment;
TransitionComponentProps = {};
}
const onBlur = React.useCallback(event => {
if (!disabled) setFocus(false);
}, [disabled]);
const onFocus = React.useCallback(event => {
if (!disabled) setFocus(true);
}, [disabled]);
const start = React.Children.toArray(start_);
// Icon
if (!open ? icon : iconOpen || icon) start.push(!open ? icon : iconOpen || icon);
const end = React.Children.toArray(end_);
if (indicator && button && children_ && !noExpand) {
let Indicator;
if (arrow) {
Indicator = /*#__PURE__*/React.createElement(IconArrow, _extends({
className: classNames([classes.indicator, open && classes.indicator_open])
}, IndicatorProps));
}
if (checkbox) {
Indicator = /*#__PURE__*/React.createElement(Checkbox, _extends({
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.Children.toArray(children_).map(item => /*#__PURE__*/React.cloneElement(item, {
tonal: item.props.tonal !== undefined ? item.props.tonal : tonal,
color: item.props.color !== undefined ? item.props.color : color,
version: item.props.version !== undefined ? item.props.version : version,
line: item.props.line !== undefined ? item.props.line : line,
checkbox: item.props.checkbox !== undefined ? item.props.checkbox : checkbox,
IconArrow: item.props.IconArrow !== undefined ? item.props.IconArrow : IconArrow,
indicator: item.props.indicator !== undefined ? item.props.indicator : indicator,
indicatorPosition: item.props.indicatorPosition !== undefined ? item.props.indicatorPosition : indicatorPosition,
noExpand: item.props.noExpand !== undefined ? item.props.noExpand : noExpand,
noTransition: item.props.noTransition !== undefined ? item.props.noTransition : noTransition,
parentDisabled: item.props.parentDisabled !== undefined ? item.props.parentDisabled : parentDisabled || disabled,
level: level + 1
}));
return /*#__PURE__*/React.createElement(Surface, _extends({
ref: item => {
if (ref) {
if (is('function', ref)) ref(item);else if (ref?.current) ref.current = item;
}
refs.root.current = item;
},
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: classNames([staticClassName('Tree', theme) && ['amaui-Tree-root', children ? `amaui-Tree-children` : `amaui-Tree-empty`, parentDisabled && `amaui-Tree-parent-disabled`, disabled && `amaui-Tree-disabled`], className, classes.root, level > 0 && line && classes.line, disabled && classes.disabled]),
style: _objectSpread(_objectSpread({}, styles.root), style)
}, other), /*#__PURE__*/React.createElement(Line, _extends({
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: classNames([staticClassName('Tree', theme) && ['amaui-Tree-main'], MainProps?.className, classes.main, button && classes.button, level > 0 && line && classes.main_line])
}), button && /*#__PURE__*/React.createElement(Interaction, {
selected: selected,
pulse: focus
}), !!start.length && /*#__PURE__*/React.createElement(Line, _extends({
gap: 0,
direction: "row",
align: "center",
justify: "unset"
}, StartProps, {
className: classNames([staticClassName('Tree', theme) && ['amaui-Tree-item', 'amaui-Tree-aside', 'amaui-Tree-start'], StartProps?.className, classes.item, classes.aside, classes.start])
}), start.map((item, index) => /*#__PURE__*/React.cloneElement(item, {
key: index
}))), middle && /*#__PURE__*/React.createElement(Line, _extends({
gap: 0,
id: refs.ids.middle,
Component: is('simple', middle) ? Type : undefined
}, MiddleProps, {
className: classNames([staticClassName('Tree', theme) && ['amaui-Tree-item', 'amaui-Tree-middle', 'amaui-Tree-end'], MiddleProps?.className, classes.item, classes.middle])
}), middle), !!end.length && /*#__PURE__*/React.createElement(Line, _extends({
gap: 0,
direction: "row",
align: "center",
justify: "unset"
}, EndProps, {
className: classNames([staticClassName('Tree', theme) && ['amaui-Tree-item', 'amaui-Tree-aside', 'amaui-Tree-end'], EndProps?.className, classes.item, classes.aside, classes.end])
}), end.map((item, index) => /*#__PURE__*/React.cloneElement(item, {
key: index
})))), noExpand && children, !noExpand && children && /*#__PURE__*/React.createElement(Expand, _extends({
in: open,
parent: parent || refs.root.current,
onTransition: (element, status) => {
refs.inProgressTransition.current = !['entered', 'removed'].includes(status);
}
}, ExpandProps), /*#__PURE__*/React.createElement(TransitionComponent, TransitionComponentProps, /*#__PURE__*/React.createElement(Line, _extends({
gap: 0,
align: "unset",
justify: "unset",
role: "group"
}, TreeProps, {
className: classNames([staticClassName('Tree', theme) && ['amaui-Tree-tree'], TreeProps?.className, classes.tree])
}), children.map((item, index) => /*#__PURE__*/React.cloneElement(item, {
key: index
}))))));
});
Tree.displayName = 'amaui-Tree';
export default Tree;