ant-design-vue/components/timeline/Timeline.jsx

89 lines
2.7 KiB
Vue
Raw Normal View History

2018-04-03 15:51:44 +08: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'
export const TimelineProps = {
prefixCls: PropTypes.string,
/** 指定最后一个幽灵节点是否存在或内容 */
pending: PropTypes.any,
2018-04-07 14:29:59 +08:00
pendingDot: PropTypes.string,
reverse: PropTypes.bool,
mode: PropTypes.oneOf(['left', 'alternate', 'right']),
2018-04-03 15:51:44 +08:00
}
export default {
2018-04-08 21:17:20 +08:00
name: 'ATimeline',
2018-04-03 15:51:44 +08:00
props: initDefaultProps(TimelineProps, {
prefixCls: 'ant-timeline',
reverse: false,
2018-04-03 15:51:44 +08:00
}),
render () {
const { prefixCls, reverse, mode, ...restProps } = getOptionProps(this)
2018-04-07 14:29:59 +08:00
const pendingDot = getComponentFromProp(this, 'pendingDot')
2018-04-03 15:51:44 +08:00
const pending = getComponentFromProp(this, 'pending')
const pendingNode = typeof pending === 'boolean' ? null : pending
const classString = classNames(prefixCls, {
[`${prefixCls}-pending`]: !!pending,
[`${prefixCls}-reverse`]: !!reverse,
[`${prefixCls}-${mode}`]: !!mode,
2018-04-03 15:51:44 +08:00
})
const children = filterEmpty(this.$slots.default)
// // 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 ? (
2018-04-03 15:51:44 +08:00
<TimelineItem
pending={!!pending}
>
2018-04-07 14:29:59 +08:00
<template slot='dot'>
{pendingDot || <Icon type='loading' />}
</template>
2018-04-03 15:51:44 +08:00
{pendingNode}
</TimelineItem>
) : null
const timeLineItems = !!reverse
? [pendingItem, ...children.reverse()]
: [...children, pendingItem]
// Remove falsy items
const truthyItems = timeLineItems.filter(item => !!item)
const itemsCount = truthyItems.length
const lastCls = `${prefixCls}-item-last`
const items = truthyItems.map((ele, idx) =>
cloneElement(ele, {
class: classNames([
(!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-04-03 15:51:44 +08:00
const timelineProps = {
props: {
...restProps,
},
class: classString,
on: this.$listeners,
}
return (
<ul {...timelineProps}>
{items}
</ul>
)
},
}