|
| 1 | +import { Fragment } from 'react'; |
| 2 | +import { View, Text, StyleSheet, ViewProps } from 'react-native'; |
| 3 | +import { IconCustom } from './Icons'; |
| 4 | +import { Desc } from './Desc'; |
| 5 | +import { useTheme } from '@shopify/restyle'; |
| 6 | +import { Theme } from '../theme'; |
| 7 | +import type { TimelineItemsProps, TimelineItemsMode, TimelineProps } from './types'; |
| 8 | + |
| 9 | +interface LineItemProps { |
| 10 | + item: TimelineItemsProps; |
| 11 | + mode?: TimelineItemsMode; |
| 12 | + end?: boolean; |
| 13 | + index: number; |
| 14 | + renderItem?: TimelineProps['renderItem']; |
| 15 | +} |
| 16 | + |
| 17 | +export const Items = (props: LineItemProps) => { |
| 18 | + const theme = useTheme<Theme>(); |
| 19 | + const { item, end, mode, index, renderItem } = props; |
| 20 | + |
| 21 | + const styles = createStyles({ |
| 22 | + backgroundColor: theme.colors.mask, |
| 23 | + title: theme.colors.primary_text, |
| 24 | + line: theme.colors.primary_text, |
| 25 | + tips: theme.colors.primary_text, |
| 26 | + }); |
| 27 | + |
| 28 | + const render = () => { |
| 29 | + if (item.renderItem) { |
| 30 | + return item.renderItem(item, index); |
| 31 | + } else if (renderItem) { |
| 32 | + return renderItem(item, index); |
| 33 | + } |
| 34 | + return ( |
| 35 | + <Fragment> |
| 36 | + <View> |
| 37 | + <Text style={styles.title}>{item.title}</Text> |
| 38 | + </View> |
| 39 | + {item.tips && <Text style={styles.tips}>{item.tips}</Text>} |
| 40 | + {item.desc && <Desc theme={theme} desc={item.desc} />} |
| 41 | + </Fragment> |
| 42 | + ); |
| 43 | + }; |
| 44 | + |
| 45 | + return ( |
| 46 | + <View |
| 47 | + style={[ |
| 48 | + styles.item, |
| 49 | + mode === 'left' && styles.itemLeft, |
| 50 | + mode === 'alternate' && index % 2 === 0 && styles.itemEnd, |
| 51 | + ]} |
| 52 | + > |
| 53 | + <View style={[styles.left, mode === 'alternate' && styles.leftAlternate]}> |
| 54 | + {!end && <View style={styles.line} />} |
| 55 | + <View style={styles.icons}> |
| 56 | + <IconCustom theme={theme} icon={item.icon} size={item.size} color={item.color} /> |
| 57 | + </View> |
| 58 | + </View> |
| 59 | + |
| 60 | + <View |
| 61 | + style={[ |
| 62 | + !mode && styles.content, |
| 63 | + mode === 'left' && styles.contentLeft, |
| 64 | + mode === 'alternate' && styles.contentAlternate, |
| 65 | + mode === 'alternate' && index % 2 !== 0 && styles.contentAlternateLeft, |
| 66 | + mode === 'alternate' && index % 2 === 0 && styles.contentAlternateRight, |
| 67 | + ]} |
| 68 | + > |
| 69 | + {render()} |
| 70 | + </View> |
| 71 | + </View> |
| 72 | + ); |
| 73 | +}; |
| 74 | + |
| 75 | +function createStyles({ backgroundColor = '', title = '#666', tips = '#666', line = '#e4e7ed' }) { |
| 76 | + return StyleSheet.create({ |
| 77 | + item: { |
| 78 | + position: 'relative', |
| 79 | + paddingBottom: 20, |
| 80 | + display: 'flex', |
| 81 | + flexDirection: 'row', |
| 82 | + }, |
| 83 | + itemLeft: { |
| 84 | + flexDirection: 'row-reverse', |
| 85 | + }, |
| 86 | + itemEnd: { |
| 87 | + justifyContent: 'flex-end', |
| 88 | + }, |
| 89 | + |
| 90 | + left: { |
| 91 | + width: 24, |
| 92 | + position: 'relative', |
| 93 | + }, |
| 94 | + leftAlternate: { |
| 95 | + position: 'absolute', |
| 96 | + left: '50%', |
| 97 | + marginLeft: -12, |
| 98 | + top: 5, |
| 99 | + bottom: 10, |
| 100 | + }, |
| 101 | + content: { |
| 102 | + paddingLeft: 12, |
| 103 | + paddingRight: 12, |
| 104 | + flex: 1, |
| 105 | + }, |
| 106 | + contentLeft: { |
| 107 | + flex: 1, |
| 108 | + paddingLeft: 12, |
| 109 | + paddingRight: 12, |
| 110 | + alignItems: 'flex-end', |
| 111 | + }, |
| 112 | + contentAlternate: { |
| 113 | + paddingLeft: 12, |
| 114 | + paddingRight: 12, |
| 115 | + width: '47%', |
| 116 | + }, |
| 117 | + contentAlternateLeft: { |
| 118 | + alignItems: 'flex-end', |
| 119 | + textAlign: 'right', |
| 120 | + }, |
| 121 | + contentAlternateRight: {}, |
| 122 | + icons: { |
| 123 | + width: 24, |
| 124 | + display: 'flex', |
| 125 | + alignItems: 'center', |
| 126 | + }, |
| 127 | + |
| 128 | + line: { |
| 129 | + position: 'absolute', |
| 130 | + left: 12, |
| 131 | + top: 22, |
| 132 | + bottom: -17, |
| 133 | + width: 1, |
| 134 | + backgroundColor: line, |
| 135 | + }, |
| 136 | + wrapper: { |
| 137 | + paddingLeft: 20, |
| 138 | + }, |
| 139 | + top: {}, |
| 140 | + tips: { |
| 141 | + color: tips, |
| 142 | + marginTop: 8, |
| 143 | + }, |
| 144 | + title: { |
| 145 | + fontSize: 15, |
| 146 | + lineHeight: 20, |
| 147 | + color: title, |
| 148 | + }, |
| 149 | + }); |
| 150 | +} |
0 commit comments