|
| 1 | +import type { TimelineItemProps } from './timeline-types' |
| 2 | +import { computed, defineComponent, h } from 'vue' |
| 3 | +import { useNamespace } from '../../shared/hooks/use-namespace' |
| 4 | +import { timelineItemProps } from './timeline-types' |
| 5 | + |
| 6 | +export default defineComponent({ |
| 7 | + name: 'CTimelineItem', |
| 8 | + props: timelineItemProps, |
| 9 | + emits: [], |
| 10 | + setup(props: TimelineItemProps, { slots }) { |
| 11 | + const ns = useNamespace('timeline-item') |
| 12 | + |
| 13 | + // 计算节点的样式类名 |
| 14 | + const nodeClasses = computed(() => { |
| 15 | + return [ |
| 16 | + ns.e('node'), |
| 17 | + ns.em('node', props.size), |
| 18 | + props.type && ns.em('node', props.type), |
| 19 | + props.hollow && ns.is('hollow'), |
| 20 | + ].filter(Boolean) |
| 21 | + }) |
| 22 | + |
| 23 | + // 计算时间戳的样式类名 |
| 24 | + const timestampClasses = computed(() => { |
| 25 | + return [ |
| 26 | + ns.e('timestamp'), |
| 27 | + ns.is(props.placement), |
| 28 | + ] |
| 29 | + }) |
| 30 | + |
| 31 | + // 渲染图标 |
| 32 | + const renderIcon = () => { |
| 33 | + if (props.icon) { |
| 34 | + if (typeof props.icon === 'string') { |
| 35 | + return <i class={[props.icon, ns.e('icon')]}></i> |
| 36 | + } |
| 37 | + else { |
| 38 | + // 如果是组件,使用 h 函数渲染 |
| 39 | + return h(props.icon, { class: ns.e('icon') }) |
| 40 | + } |
| 41 | + } |
| 42 | + return null |
| 43 | + } |
| 44 | + |
| 45 | + // 渲染节点 |
| 46 | + const renderNode = () => { |
| 47 | + if (slots.dot) { |
| 48 | + return ( |
| 49 | + <div class={ns.e('dot')}> |
| 50 | + {slots.dot()} |
| 51 | + </div> |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + return ( |
| 56 | + <div |
| 57 | + class={nodeClasses.value} |
| 58 | + style={props.color ? { backgroundColor: props.color, borderColor: props.color } : {}} |
| 59 | + > |
| 60 | + {renderIcon()} |
| 61 | + </div> |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + // 渲染时间戳 |
| 66 | + const renderTimestamp = () => { |
| 67 | + if (props.hideTimestamp) |
| 68 | + return null |
| 69 | + |
| 70 | + return ( |
| 71 | + <div class={timestampClasses.value}> |
| 72 | + {props.timestamp} |
| 73 | + </div> |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + return () => { |
| 78 | + return ( |
| 79 | + <li class={[ns.b(), props.center && ns.e('center')]}> |
| 80 | + {/* 连接线 */} |
| 81 | + <div class={ns.e('tail')}></div> |
| 82 | + |
| 83 | + {/* 节点 */} |
| 84 | + {renderNode()} |
| 85 | + |
| 86 | + {/* 内容区域 */} |
| 87 | + <div class={ns.e('wrapper')}> |
| 88 | + {/* 顶部时间戳 */} |
| 89 | + {props.placement === 'top' && renderTimestamp()} |
| 90 | + |
| 91 | + {/* 内容 */} |
| 92 | + <div class={ns.e('content')}> |
| 93 | + {slots.default && slots.default()} |
| 94 | + </div> |
| 95 | + |
| 96 | + {/* 底部时间戳 */} |
| 97 | + {props.placement === 'bottom' && renderTimestamp()} |
| 98 | + </div> |
| 99 | + </li> |
| 100 | + ) |
| 101 | + } |
| 102 | + }, |
| 103 | +}) |
0 commit comments