|
| 1 | +import React from 'react'; |
| 2 | +import { View, Text, StyleSheet, Dimensions } from 'react-native'; |
| 3 | +import BodyRow from './BodyRow'; |
| 4 | + |
| 5 | + |
| 6 | +interface TableProps { |
| 7 | + data: Array<Object>; |
| 8 | + columns: Array<columnsState>; |
| 9 | + rowKey: any |
| 10 | +} |
| 11 | + |
| 12 | +interface columnsState { |
| 13 | + dataIndex: string |
| 14 | + title: string |
| 15 | + style?: Object; |
| 16 | + render?: (record: any) => React.ReactNode; |
| 17 | + ellipsis?: boolean |
| 18 | +} |
| 19 | +// table组件 |
| 20 | +const Table = ({ |
| 21 | + data, |
| 22 | + columns, |
| 23 | + rowKey |
| 24 | +}: TableProps) => { |
| 25 | + |
| 26 | + const getRowKey: (record: any) => string = (record) => { |
| 27 | + const key = typeof rowKey === 'function' ? rowKey(record) : record && record[rowKey]; |
| 28 | + return key; |
| 29 | + } |
| 30 | + |
| 31 | + return ( |
| 32 | + <View style={styles.conW}> |
| 33 | + <View style={styles.conTitle}> |
| 34 | + {columns.map((itm, idx) => ( |
| 35 | + <View key={itm.dataIndex + idx} style={[styles.contRight, { borderRightWidth: idx === columns.length - 1 ? 0 : 1 }, itm.style]}> |
| 36 | + <Text style={styles.content}>{itm.title}</Text> |
| 37 | + </View> |
| 38 | + ))} |
| 39 | + </View> |
| 40 | + {data.map((item, idx) => { |
| 41 | + const key = getRowKey(item); |
| 42 | + return <BodyRow key={key} columns={columns} record={item} style={{ borderBottomWidth: idx === data.length - 1 ? 0 : 1 }} />; |
| 43 | + })} |
| 44 | + {data.length === 0 && <Text style={styles.noDataText}>暂无数据...</Text>} |
| 45 | + </View> |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +const styles = StyleSheet.create({ |
| 50 | + title: { |
| 51 | + backgroundColor: '#fff', |
| 52 | + height: 30, |
| 53 | + }, |
| 54 | + conTitle: { |
| 55 | + flexDirection: 'row', |
| 56 | + borderBottomWidth: 1, |
| 57 | + borderColor: '#E5E5E5', |
| 58 | + }, |
| 59 | + content: { |
| 60 | + color: '#888888', |
| 61 | + }, |
| 62 | + conn: { |
| 63 | + flexDirection: 'row', |
| 64 | + borderBottomWidth: 1, |
| 65 | + borderColor: '#E5E5E5', |
| 66 | + }, |
| 67 | + |
| 68 | + contRight: { |
| 69 | + borderRightWidth: 1, |
| 70 | + flex: 1, |
| 71 | + borderRightColor: '#E5E5E5', |
| 72 | + borderBottomColor: '#E5E5E5', |
| 73 | + color: '#888888', |
| 74 | + flexDirection: 'row', |
| 75 | + alignItems: 'center', |
| 76 | + justifyContent: 'center', |
| 77 | + paddingTop: 5, |
| 78 | + paddingBottom: 5, |
| 79 | + }, |
| 80 | + conW: { |
| 81 | + borderTopLeftRadius: 5, |
| 82 | + borderTopRightRadius: 5, |
| 83 | + // overflow: 'hidden', |
| 84 | + borderWidth: 1, |
| 85 | + borderTopWidth: 0, |
| 86 | + borderColor: '#E5E5E5', |
| 87 | + // marginTop: 10, |
| 88 | + width: Dimensions.get('window').width, |
| 89 | + backgroundColor: '#fff', |
| 90 | + // marginBottom: 20 |
| 91 | + }, |
| 92 | + noDataText: { |
| 93 | + color: '#888888', |
| 94 | + textAlign: 'center', |
| 95 | + paddingTop: 4, |
| 96 | + paddingBottom: 4, |
| 97 | + }, |
| 98 | + row: { |
| 99 | + flex: 1, |
| 100 | + flexDirection: 'row', |
| 101 | + justifyContent: 'center', |
| 102 | + borderBottomWidth: 1, |
| 103 | + borderColor: '#E5E5E5', |
| 104 | + }, |
| 105 | +}); |
| 106 | + |
| 107 | +export default Table; |
0 commit comments