2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-03-31 17:46:35 +08:00
|
|
|
import classNames from 'classnames';
|
2017-06-05 11:39:59 +08:00
|
|
|
import TimelineItem, { TimeLineItemProps } from './TimelineItem';
|
2018-01-09 17:42:31 +08:00
|
|
|
import Icon from '../icon';
|
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;
|
2016-07-09 10:53:40 +08:00
|
|
|
}
|
2016-07-13 17:22:23 +08:00
|
|
|
|
2016-07-09 10:53:40 +08:00
|
|
|
export default class Timeline extends React.Component<TimelineProps, any> {
|
2017-06-05 11:39:59 +08:00
|
|
|
static Item = TimelineItem as React.ClassicComponentClass<TimeLineItemProps>;
|
2016-03-31 17:46:35 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-timeline',
|
2018-04-20 11:23:37 +08:00
|
|
|
reverse: false,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
render() {
|
2018-04-20 11:23:37 +08:00
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
pending = null, pendingDot,
|
|
|
|
children, className, reverse,
|
2018-06-02 20:12:09 +08:00
|
|
|
...restProps
|
2018-04-20 11:23:37 +08:00
|
|
|
} = this.props;
|
2016-03-31 17:46:35 +08:00
|
|
|
const pendingNode = typeof pending === 'boolean' ? null : pending;
|
2016-11-30 10:20:23 +08:00
|
|
|
const classString = classNames(prefixCls, {
|
2016-03-31 17:46:35 +08:00
|
|
|
[`${prefixCls}-pending`]: !!pending,
|
2018-04-20 11:23:37 +08:00
|
|
|
[`${prefixCls}-reverse`]: !!reverse,
|
2016-11-30 10:20:23 +08:00
|
|
|
}, className);
|
2018-04-20 11:23:37 +08:00
|
|
|
|
|
|
|
const pendingItem = !!pending ? (
|
2018-01-09 17:42:31 +08:00
|
|
|
<TimelineItem
|
|
|
|
pending={!!pending}
|
2018-04-15 16:51:05 +08:00
|
|
|
dot={pendingDot || <Icon type="loading" />}
|
2018-01-09 17:42:31 +08:00
|
|
|
>
|
|
|
|
{pendingNode}
|
|
|
|
</TimelineItem>
|
2016-11-25 12:03:39 +08:00
|
|
|
) : null;
|
2018-04-20 11:23:37 +08:00
|
|
|
|
|
|
|
const timeLineItems = !!reverse
|
|
|
|
? [pendingItem, ...React.Children.toArray(children).reverse()]
|
|
|
|
: [...React.Children.toArray(children), pendingItem];
|
|
|
|
|
|
|
|
// Remove falsy items
|
2018-04-28 11:04:10 +08:00
|
|
|
const truthyItems = timeLineItems.filter(item => !!item);
|
|
|
|
const itemsCount = React.Children.count(truthyItems);
|
2018-04-20 11:23:37 +08:00
|
|
|
const lastCls = `${prefixCls}-item-last`;
|
2018-04-28 11:04:10 +08:00
|
|
|
const items = React.Children.map(truthyItems, (ele: React.ReactElement<any>, idx) =>
|
2018-04-20 11:23:37 +08:00
|
|
|
React.cloneElement(ele, {
|
|
|
|
className: classNames([
|
|
|
|
ele.props.className,
|
|
|
|
(!reverse && !!pending)
|
|
|
|
? (idx === itemsCount - 2) ? lastCls : ''
|
|
|
|
: (idx === itemsCount - 1) ? lastCls : '',
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2016-03-31 17:46:35 +08:00
|
|
|
return (
|
2018-04-15 16:51:05 +08:00
|
|
|
<ul {...restProps} className={classString}>
|
2016-11-25 12:03:39 +08:00
|
|
|
{items}
|
2016-03-31 17:46:35 +08:00
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|