mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-04 04:58:55 +08:00
452c5835ec
* feat: Tree `switcherIcon` prop support render-prop * add test for `switcherIcon` render-prop * change the `switcherIcon` prop type of `TreeSelect` * fix switcherIcon render-prop condition * fix switcherIcon
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
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, SwitcherIcon } from '../Tree';
|
|
import { isValidElement, cloneElement } from '../../_util/reactNode';
|
|
|
|
export default function renderSwitcherIcon(
|
|
prefixCls: string,
|
|
switcherIcon: SwitcherIcon | undefined,
|
|
showLine: boolean | { showLeafIcon: boolean } | undefined,
|
|
{ isLeaf, expanded, loading }: AntTreeNodeProps,
|
|
) {
|
|
if (loading) {
|
|
return <LoadingOutlined className={`${prefixCls}-switcher-loading-icon`} />;
|
|
}
|
|
let showLeafIcon;
|
|
if (showLine && typeof showLine === 'object') {
|
|
showLeafIcon = showLine.showLeafIcon;
|
|
}
|
|
if (isLeaf) {
|
|
if (showLine) {
|
|
if (typeof showLine === 'object' && !showLeafIcon) {
|
|
return <span className={`${prefixCls}-switcher-leaf-line`} />;
|
|
}
|
|
return <FileOutlined className={`${prefixCls}-switcher-line-icon`} />;
|
|
}
|
|
return null;
|
|
}
|
|
const switcherCls = `${prefixCls}-switcher-icon`;
|
|
|
|
let switcher = switcherIcon;
|
|
if (typeof switcher === 'function') {
|
|
switcher = switcher(Boolean(expanded));
|
|
}
|
|
|
|
if (isValidElement(switcher)) {
|
|
return cloneElement(switcher, {
|
|
className: classNames(switcher.props.className || '', switcherCls),
|
|
});
|
|
}
|
|
|
|
if (switcher) {
|
|
return switcher;
|
|
}
|
|
|
|
if (showLine) {
|
|
return expanded ? (
|
|
<MinusSquareOutlined className={`${prefixCls}-switcher-line-icon`} />
|
|
) : (
|
|
<PlusSquareOutlined className={`${prefixCls}-switcher-line-icon`} />
|
|
);
|
|
}
|
|
return <CaretDownFilled className={switcherCls} />;
|
|
}
|