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

59 lines
1.9 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import classNames from 'classnames';
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
import FileOutlined from '@ant-design/icons/FileOutlined';
import MinusSquareOutlined from '@ant-design/icons/MinusSquareOutlined';
import PlusSquareOutlined from '@ant-design/icons/PlusSquareOutlined';
import CaretDownFilled from '@ant-design/icons/CaretDownFilled';
import { AntTreeNodeProps } from '../Tree';
import { isValidElement, cloneElement } from '../../_util/reactNode';
export default function renderSwitcherIcon(
prefixCls: string,
switcherIcon: React.ReactNode | ((props: any) => React.ReactNode),
2020-06-24 00:19:43 +08:00
showLine: boolean | { showLeafIcon: boolean } | undefined,
treeNodeProps: AntTreeNodeProps,
): React.ReactNode {
const { isLeaf, expanded, loading } = treeNodeProps;
if (loading) {
return <LoadingOutlined className={`${prefixCls}-switcher-loading-icon`} />;
}
2020-06-24 00:19:43 +08:00
let showLeafIcon;
if (showLine && typeof showLine === 'object') {
showLeafIcon = showLine.showLeafIcon;
}
if (isLeaf) {
2020-06-24 00:19:43 +08:00
if (showLine) {
if (typeof showLine === 'object' && !showLeafIcon) {
return <span className={`${prefixCls}-switcher-leaf-line`} />;
}
return <FileOutlined className={`${prefixCls}-switcher-line-icon`} />;
}
return null;
}
2022-04-11 17:41:54 +08:00
const switcherCls = `${prefixCls}-switcher-icon`;
2022-04-11 17:41:54 +08:00
const switcher = typeof switcherIcon === 'function' ? switcherIcon({ expanded }) : switcherIcon;
if (isValidElement(switcher)) {
return cloneElement(switcher, {
className: classNames(switcher.props.className || '', switcherCls),
});
}
2022-04-11 18:07:34 +08:00
if (switcher) {
return switcher;
}
if (showLine) {
return expanded ? (
<MinusSquareOutlined className={`${prefixCls}-switcher-line-icon`} />
) : (
<PlusSquareOutlined className={`${prefixCls}-switcher-line-icon`} />
);
}
return <CaretDownFilled className={switcherCls} />;
}