@@ -2,6 +2,8 @@ import React from 'react';
2
2
import { View , Text , StyleSheet , Dimensions , ScrollView } from 'react-native' ;
3
3
import BodyRow from './BodyRow' ;
4
4
import { colors } from '../utils' ;
5
+ import { useTheme } from '@shopify/restyle' ;
6
+ import { Theme } from '../theme' ;
5
7
6
8
interface TableProps {
7
9
data : Array < Object > ;
@@ -20,6 +22,11 @@ interface columnsState {
20
22
}
21
23
// table组件
22
24
const Table = ( { data, columns, rowKey, horizontal = true , style } : TableProps ) => {
25
+ const theme = useTheme < Theme > ( ) ;
26
+ const styles = createStyles ( {
27
+ bgColor : theme . colors . background || '#F5F5F5' ,
28
+ textColor : theme . colors . primary_text || '#ccc' ,
29
+ } ) ;
23
30
const getRowKey : ( record : any ) => string = ( record ) => {
24
31
const key = typeof rowKey === 'function' ? rowKey ( record ) : record && record [ rowKey ] ;
25
32
return key ;
@@ -32,7 +39,11 @@ const Table = ({ data, columns, rowKey, horizontal = true, style }: TableProps)
32
39
{ columns . map ( ( itm , idx ) => (
33
40
< View
34
41
key = { itm . dataIndex + idx }
35
- style = { [ styles . contRight , { borderRightWidth : idx === columns . length - 1 ? 0 : 1 } , itm . style ? itm . style : styles . titleFlex ] }
42
+ style = { [
43
+ styles . contRight ,
44
+ { borderRightWidth : idx === columns . length - 1 ? 0 : 1 } ,
45
+ itm . style ? itm . style : styles . titleFlex ,
46
+ ] }
36
47
>
37
48
< Text style = { styles . content } > { itm . title } </ Text >
38
49
</ View >
@@ -41,13 +52,7 @@ const Table = ({ data, columns, rowKey, horizontal = true, style }: TableProps)
41
52
< View testID = "RNE__Table__body" >
42
53
{ data . map ( ( item , idx ) => {
43
54
const key = getRowKey ( item ) ;
44
- return (
45
- < BodyRow
46
- key = { key }
47
- columns = { columns }
48
- record = { item }
49
- />
50
- ) ;
55
+ return < BodyRow key = { key } columns = { columns } record = { item } /> ;
51
56
} ) }
52
57
</ View >
53
58
{ data . length === 0 && < Text style = { styles . noDataText } > 暂无数据...</ Text > }
@@ -56,42 +61,49 @@ const Table = ({ data, columns, rowKey, horizontal = true, style }: TableProps)
56
61
) ;
57
62
} ;
58
63
59
- const styles = StyleSheet . create ( {
60
- title : {
61
- backgroundColor : colors . white ,
62
- height : 30 ,
63
- } ,
64
- conTitle : {
65
- flexDirection : 'row' ,
66
- borderWidth : 1 ,
67
- borderColor : colors . colorsPalette . dark70 ,
68
- } ,
69
- content : {
70
- color : colors . colorsPalette . dark30 ,
71
- } ,
72
- contRight : {
73
- borderRightWidth : 1 ,
74
- borderRightColor : colors . colorsPalette . dark70 ,
75
- borderBottomColor : colors . colorsPalette . dark70 ,
76
- color : '#888888' ,
77
- flexDirection : 'row' ,
78
- alignItems : 'center' ,
79
- justifyContent : 'center' ,
80
- paddingTop : 5 ,
81
- paddingBottom : 5 ,
82
- } ,
83
- conW : {
84
- backgroundColor : colors . white ,
85
- } ,
86
- noDataText : {
87
- color : '#888888' ,
88
- textAlign : 'center' ,
89
- paddingTop : 4 ,
90
- paddingBottom : 4 ,
91
- } ,
92
- titleFlex : {
93
- flex : 1
94
- }
95
- } ) ;
64
+ type CreStyle = {
65
+ bgColor : string ;
66
+ textColor : string ;
67
+ } ;
68
+
69
+ function createStyles ( { bgColor, textColor } : CreStyle ) {
70
+ return StyleSheet . create ( {
71
+ title : {
72
+ backgroundColor : colors . white ,
73
+ height : 30 ,
74
+ } ,
75
+ conTitle : {
76
+ flexDirection : 'row' ,
77
+ borderWidth : 1 ,
78
+ borderColor : colors . colorsPalette . dark70 ,
79
+ } ,
80
+ content : {
81
+ color : textColor ,
82
+ } ,
83
+ contRight : {
84
+ borderRightWidth : 1 ,
85
+ borderRightColor : colors . colorsPalette . dark70 ,
86
+ borderBottomColor : colors . colorsPalette . dark70 ,
87
+ color : '#888888' ,
88
+ flexDirection : 'row' ,
89
+ alignItems : 'center' ,
90
+ justifyContent : 'center' ,
91
+ paddingTop : 5 ,
92
+ paddingBottom : 5 ,
93
+ } ,
94
+ conW : {
95
+ backgroundColor : bgColor ,
96
+ } ,
97
+ noDataText : {
98
+ color : '#888888' ,
99
+ textAlign : 'center' ,
100
+ paddingTop : 4 ,
101
+ paddingBottom : 4 ,
102
+ } ,
103
+ titleFlex : {
104
+ flex : 1 ,
105
+ } ,
106
+ } ) ;
107
+ }
96
108
97
109
export default Table ;
0 commit comments