2020-03-02 12:09:38 +08:00
|
|
|
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
|
2022-06-22 14:57:09 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import * as React from 'react';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2020-05-02 18:55:56 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { TimelineItemProps } from './TimelineItem';
|
|
|
|
import TimelineItem from './TimelineItem';
|
2016-07-09 10:53:40 +08:00
|
|
|
|
|
|
|
export interface TimelineProps {
|
2016-12-19 15:19:15 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 指定最后一个幽灵节点是否存在或内容 */
|
2017-01-20 20:10:50 +08:00
|
|
|
pending?: React.ReactNode;
|
2018-03-09 15:03:19 +08:00
|
|
|
pendingDot?: React.ReactNode;
|
2016-07-13 11:14:24 +08:00
|
|
|
style?: React.CSSProperties;
|
2018-04-20 11:23:37 +08:00
|
|
|
reverse?: boolean;
|
2018-07-28 00:25:23 +08:00
|
|
|
mode?: 'left' | 'alternate' | 'right';
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
2016-07-09 10:53:40 +08:00
|
|
|
}
|
2016-07-13 17:22:23 +08:00
|
|
|
|
2023-01-03 11:46:48 +08:00
|
|
|
type CompoundedComponent = React.FC<TimelineProps> & {
|
2020-10-03 22:03:09 +08:00
|
|
|
Item: React.FC<TimelineItemProps>;
|
2023-01-03 11:46:48 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2023-01-03 11:46:48 +08:00
|
|
|
const Timeline: CompoundedComponent = (props) => {
|
2020-05-02 18:55:56 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
pending = null,
|
|
|
|
pendingDot,
|
|
|
|
children,
|
|
|
|
className,
|
2020-12-01 12:18:13 +08:00
|
|
|
reverse = false,
|
|
|
|
mode = '' as TimelineProps['mode'],
|
2020-05-02 18:55:56 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
const prefixCls = getPrefixCls('timeline', customizePrefixCls);
|
|
|
|
const pendingNode = typeof pending === 'boolean' ? null : pending;
|
2018-04-20 11:23:37 +08:00
|
|
|
|
2020-05-02 18:55:56 +08:00
|
|
|
const pendingItem = pending ? (
|
|
|
|
<TimelineItem pending={!!pending} dot={pendingDot || <LoadingOutlined />}>
|
|
|
|
{pendingNode}
|
|
|
|
</TimelineItem>
|
|
|
|
) : null;
|
2018-04-20 11:23:37 +08:00
|
|
|
|
2020-12-01 12:13:41 +08:00
|
|
|
const timeLineItems = React.Children.toArray(children);
|
2022-11-09 17:36:49 +08:00
|
|
|
timeLineItems.push(pendingItem!);
|
2020-12-01 12:13:41 +08:00
|
|
|
if (reverse) {
|
|
|
|
timeLineItems.reverse();
|
|
|
|
}
|
2018-04-20 11:23:37 +08:00
|
|
|
|
2020-05-02 18:55:56 +08:00
|
|
|
const getPositionCls = (ele: React.ReactElement<any>, idx: number) => {
|
|
|
|
if (mode === 'alternate') {
|
2019-05-06 12:04:39 +08:00
|
|
|
if (ele.props.position === 'right') return `${prefixCls}-item-right`;
|
2020-05-02 18:55:56 +08:00
|
|
|
if (ele.props.position === 'left') return `${prefixCls}-item-left`;
|
|
|
|
return idx % 2 === 0 ? `${prefixCls}-item-left` : `${prefixCls}-item-right`;
|
|
|
|
}
|
|
|
|
if (mode === 'left') return `${prefixCls}-item-left`;
|
|
|
|
if (mode === 'right') return `${prefixCls}-item-right`;
|
|
|
|
if (ele.props.position === 'right') return `${prefixCls}-item-right`;
|
|
|
|
return '';
|
|
|
|
};
|
2019-05-06 12:04:39 +08:00
|
|
|
|
2020-05-02 18:55:56 +08:00
|
|
|
// Remove falsy items
|
|
|
|
const truthyItems = timeLineItems.filter(item => !!item);
|
|
|
|
const itemsCount = React.Children.count(truthyItems);
|
|
|
|
const lastCls = `${prefixCls}-item-last`;
|
|
|
|
const items = React.Children.map(truthyItems, (ele: React.ReactElement<any>, idx) => {
|
|
|
|
const pendingClass = idx === itemsCount - 2 ? lastCls : '';
|
|
|
|
const readyClass = idx === itemsCount - 1 ? lastCls : '';
|
2020-05-14 20:54:49 +08:00
|
|
|
return cloneElement(ele, {
|
2020-05-02 18:55:56 +08:00
|
|
|
className: classNames([
|
|
|
|
ele.props.className,
|
|
|
|
!reverse && !!pending ? pendingClass : readyClass,
|
|
|
|
getPositionCls(ele, idx),
|
|
|
|
]),
|
2019-08-05 18:38:10 +08:00
|
|
|
});
|
2020-05-02 18:55:56 +08:00
|
|
|
});
|
2018-04-20 11:23:37 +08:00
|
|
|
|
2020-05-02 18:55:56 +08:00
|
|
|
const hasLabelItem = timeLineItems.some((item: React.ReactElement<any>) => !!item?.props?.label);
|
2020-02-26 17:21:44 +08:00
|
|
|
|
2020-05-02 18:55:56 +08:00
|
|
|
const classString = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-pending`]: !!pending,
|
|
|
|
[`${prefixCls}-reverse`]: !!reverse,
|
|
|
|
[`${prefixCls}-${mode}`]: !!mode && !hasLabelItem,
|
|
|
|
[`${prefixCls}-label`]: hasLabelItem,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2020-02-26 17:21:44 +08:00
|
|
|
|
2020-05-02 18:55:56 +08:00
|
|
|
return (
|
|
|
|
<ul {...restProps} className={classString}>
|
|
|
|
{items}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-05-02 18:55:56 +08:00
|
|
|
Timeline.Item = TimelineItem;
|
|
|
|
|
|
|
|
export default Timeline;
|