2019-01-12 03:33:27 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import {
|
|
|
|
getOptionProps,
|
|
|
|
initDefaultProps,
|
|
|
|
filterEmpty,
|
|
|
|
getComponentFromProp,
|
|
|
|
} from '../_util/props-util';
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
|
|
|
import TimelineItem from './TimelineItem';
|
|
|
|
import Icon from '../icon';
|
2019-03-18 12:35:24 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2018-04-03 07:51:44 +00:00
|
|
|
|
|
|
|
export const TimelineProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
/** 指定最后一个幽灵节点是否存在或内容 */
|
|
|
|
pending: PropTypes.any,
|
2018-04-07 06:29:59 +00:00
|
|
|
pendingDot: PropTypes.string,
|
2018-12-05 04:32:46 +00:00
|
|
|
reverse: PropTypes.bool,
|
|
|
|
mode: PropTypes.oneOf(['left', 'alternate', 'right']),
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-04-03 07:51:44 +00:00
|
|
|
|
|
|
|
export default {
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ATimeline',
|
2018-04-03 07:51:44 +00:00
|
|
|
props: initDefaultProps(TimelineProps, {
|
2018-12-05 04:32:46 +00:00
|
|
|
reverse: false,
|
2018-04-03 07:51:44 +00:00
|
|
|
}),
|
2019-03-18 12:35:24 +00:00
|
|
|
inject: {
|
2019-09-11 14:35:25 +00:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-03-18 12:35:24 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2019-03-18 12:35:24 +00:00
|
|
|
const { prefixCls: customizePrefixCls, reverse, mode, ...restProps } = getOptionProps(this);
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-18 12:35:24 +00:00
|
|
|
const prefixCls = getPrefixCls('timeline', customizePrefixCls);
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const pendingDot = getComponentFromProp(this, 'pendingDot');
|
|
|
|
const pending = getComponentFromProp(this, 'pending');
|
|
|
|
const pendingNode = typeof pending === 'boolean' ? null : pending;
|
2018-04-03 07:51:44 +00:00
|
|
|
const classString = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-pending`]: !!pending,
|
2018-12-05 04:32:46 +00:00
|
|
|
[`${prefixCls}-reverse`]: !!reverse,
|
|
|
|
[`${prefixCls}-${mode}`]: !!mode,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
const children = filterEmpty(this.$slots.default);
|
2018-12-05 04:32:46 +00:00
|
|
|
// // Remove falsy items
|
|
|
|
// const falsylessItems = filterEmpty(this.$slots.default)
|
|
|
|
// const items = falsylessItems.map((item, idx) => {
|
|
|
|
// return cloneElement(item, {
|
|
|
|
// props: {
|
|
|
|
// last: falsylessItems.length - 1 === idx,
|
|
|
|
// },
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
const pendingItem = !!pending ? (
|
2019-01-12 03:33:27 +00:00
|
|
|
<TimelineItem pending={!!pending}>
|
|
|
|
<template slot="dot">{pendingDot || <Icon type="loading" />}</template>
|
2018-04-03 07:51:44 +00:00
|
|
|
{pendingNode}
|
|
|
|
</TimelineItem>
|
2019-01-12 03:33:27 +00:00
|
|
|
) : null;
|
2018-12-05 04:32:46 +00:00
|
|
|
|
|
|
|
const timeLineItems = !!reverse
|
|
|
|
? [pendingItem, ...children.reverse()]
|
2019-01-12 03:33:27 +00:00
|
|
|
: [...children, pendingItem];
|
2018-12-05 04:32:46 +00:00
|
|
|
|
|
|
|
// Remove falsy items
|
2019-01-12 03:33:27 +00:00
|
|
|
const truthyItems = timeLineItems.filter(item => !!item);
|
|
|
|
const itemsCount = truthyItems.length;
|
|
|
|
const lastCls = `${prefixCls}-item-last`;
|
2018-12-05 04:32:46 +00:00
|
|
|
const items = truthyItems.map((ele, idx) =>
|
|
|
|
cloneElement(ele, {
|
|
|
|
class: classNames([
|
2019-01-12 03:33:27 +00:00
|
|
|
!reverse && !!pending
|
|
|
|
? idx === itemsCount - 2
|
|
|
|
? lastCls
|
|
|
|
: ''
|
|
|
|
: idx === itemsCount - 1
|
|
|
|
? lastCls
|
|
|
|
: '',
|
|
|
|
mode === 'alternate'
|
|
|
|
? idx % 2 === 0
|
|
|
|
? `${prefixCls}-item-left`
|
|
|
|
: `${prefixCls}-item-right`
|
|
|
|
: mode === 'right'
|
|
|
|
? `${prefixCls}-item-right`
|
|
|
|
: '',
|
2018-12-05 04:32:46 +00:00
|
|
|
]),
|
|
|
|
}),
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-12-05 04:32:46 +00:00
|
|
|
|
2018-04-03 07:51:44 +00:00
|
|
|
const timelineProps = {
|
|
|
|
props: {
|
|
|
|
...restProps,
|
|
|
|
},
|
|
|
|
class: classString,
|
|
|
|
on: this.$listeners,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
return <ul {...timelineProps}>{items}</ul>;
|
2018-04-03 07:51:44 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|