ant-design/components/timeline/Timeline.tsx

101 lines
3.0 KiB
TypeScript
Raw Normal View History

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';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
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
/** 指定最后一个幽灵节点是否存在或内容 */
pending?: React.ReactNode;
pendingDot?: React.ReactNode;
2016-07-13 11:14:24 +08:00
style?: React.CSSProperties;
reverse?: boolean;
mode?: 'left' | 'alternate' | 'right';
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> {
static Item: React.SFC<TimeLineItemProps> = TimelineItem;
2016-03-31 17:46:35 +08:00
static defaultProps = {
reverse: false,
mode: '',
2016-07-13 11:14:24 +08:00
};
2016-03-31 17:46:35 +08:00
renderTimeline = ({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
2018-12-07 20:02:01 +08:00
pending = null,
pendingDot,
children,
className,
reverse,
mode,
...restProps
} = this.props;
const prefixCls = getPrefixCls('timeline', customizePrefixCls);
2016-03-31 17:46:35 +08:00
const pendingNode = typeof pending === 'boolean' ? null : pending;
2018-12-07 20:02:01 +08:00
const classString = classNames(
prefixCls,
{
[`${prefixCls}-pending`]: !!pending,
[`${prefixCls}-reverse`]: !!reverse,
[`${prefixCls}-${mode}`]: !!mode,
},
className,
);
const pendingItem = !!pending ? (
2018-12-07 20:02:01 +08:00
<TimelineItem pending={!!pending} dot={pendingDot || <Icon type="loading" />}>
2018-01-09 17:42:31 +08:00
{pendingNode}
</TimelineItem>
) : null;
const timeLineItems = !!reverse
? [pendingItem, ...React.Children.toArray(children).reverse()]
: [...React.Children.toArray(children), pendingItem];
const getPositionCls = (ele: React.ReactElement<any>, idx: number) => {
if (mode === 'alternate') {
if (ele.props.position === 'right') return `${prefixCls}-item-right`;
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 '';
};
// Remove falsy items
2018-04-28 11:04:10 +08:00
const truthyItems = timeLineItems.filter(item => !!item);
const itemsCount = React.Children.count(truthyItems);
const lastCls = `${prefixCls}-item-last`;
2018-04-28 11:04:10 +08:00
const items = React.Children.map(truthyItems, (ele: React.ReactElement<any>, idx) =>
React.cloneElement(ele, {
className: classNames([
ele.props.className,
2018-12-07 20:02:01 +08:00
!reverse && !!pending
? idx === itemsCount - 2
? lastCls
: ''
: idx === itemsCount - 1
? lastCls
: '',
getPositionCls(ele, idx),
]),
}),
);
2016-03-31 17:46:35 +08:00
return (
<ul {...restProps} className={classString}>
{items}
2016-03-31 17:46:35 +08:00
</ul>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderTimeline}</ConfigConsumer>;
}
2016-03-31 17:46:35 +08:00
}