ant-design/components/tree/utils/iconUtil.tsx

83 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-02-24 14:50:03 +08:00
import * as React from 'react';
2022-06-22 14:57:09 +08:00
import CaretDownFilled from '@ant-design/icons/CaretDownFilled';
import FileOutlined from '@ant-design/icons/FileOutlined';
2022-06-22 14:57:09 +08:00
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
import MinusSquareOutlined from '@ant-design/icons/MinusSquareOutlined';
import PlusSquareOutlined from '@ant-design/icons/PlusSquareOutlined';
2022-06-22 14:57:09 +08:00
import classNames from 'classnames';
2024-02-24 14:50:03 +08:00
import { cloneElement } from '../../_util/reactNode';
import type { AntTreeNodeProps, SwitcherIcon, TreeLeafIcon } from '../Tree';
interface SwitcherIconProps {
prefixCls: string;
treeNodeProps: AntTreeNodeProps;
switcherIcon?: SwitcherIcon;
showLine?: boolean | { showLeafIcon: boolean | TreeLeafIcon };
}
const SwitcherIconCom: React.FC<SwitcherIconProps> = (props) => {
const { prefixCls, switcherIcon, treeNodeProps, showLine } = props;
const { isLeaf, expanded, loading } = treeNodeProps;
if (loading) {
return <LoadingOutlined className={`${prefixCls}-switcher-loading-icon`} />;
}
let showLeafIcon: boolean | TreeLeafIcon;
2020-06-24 00:19:43 +08:00
if (showLine && typeof showLine === 'object') {
showLeafIcon = showLine.showLeafIcon;
}
if (isLeaf) {
if (!showLine) {
return null;
}
if (typeof showLeafIcon !== 'boolean' && !!showLeafIcon) {
const leafIcon =
typeof showLeafIcon === 'function' ? showLeafIcon(treeNodeProps) : showLeafIcon;
const leafCls = `${prefixCls}-switcher-line-custom-icon`;
2024-02-24 14:50:03 +08:00
if (React.isValidElement(leafIcon)) {
return cloneElement(leafIcon, {
className: classNames(leafIcon.props.className || '', leafCls),
});
2020-06-24 00:19:43 +08:00
}
return leafIcon as unknown as React.ReactElement;
2020-06-24 00:19:43 +08:00
}
return showLeafIcon ? (
<FileOutlined className={`${prefixCls}-switcher-line-icon`} />
) : (
<span className={`${prefixCls}-switcher-leaf-line`} />
);
}
2022-04-11 17:41:54 +08:00
const switcherCls = `${prefixCls}-switcher-icon`;
const switcher = typeof switcherIcon === 'function' ? switcherIcon(treeNodeProps) : switcherIcon;
2024-02-24 14:50:03 +08:00
if (React.isValidElement(switcher)) {
return cloneElement(switcher, {
className: classNames(switcher.props.className || '', switcherCls),
});
}
if (switcher !== undefined) {
return switcher as unknown as React.ReactElement;
2022-04-11 18:07:34 +08:00
}
if (showLine) {
return expanded ? (
<MinusSquareOutlined className={`${prefixCls}-switcher-line-icon`} />
) : (
<PlusSquareOutlined className={`${prefixCls}-switcher-line-icon`} />
);
}
return <CaretDownFilled className={switcherCls} />;
};
export default SwitcherIconCom;