Skip to content

Commit c8d5bea

Browse files
ChenlingasMxjaywcjlove
authored andcommitted
fix:Table表格
1 parent 0663cf6 commit c8d5bea

File tree

5 files changed

+302
-0
lines changed

5 files changed

+302
-0
lines changed

components/Table/BodyRow.tsx

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import React from 'react';
2+
import { View, Text, StyleSheet } from 'react-native';
3+
4+
interface BodyRowProps {
5+
columns: Array<columnsState>;
6+
record: Object | any;
7+
style?: Object
8+
}
9+
10+
interface columnsState {
11+
dataIndex: string
12+
title: string
13+
style?: Object
14+
render?: (record: any) => React.ReactNode;
15+
ellipsis?: boolean
16+
}
17+
interface textEllipsizeState {
18+
numberOfLines: number;
19+
ellipsizeMode: string;
20+
}
21+
export default function BodyRow({ columns, record, style }: BodyRowProps) {
22+
return (
23+
<View style={[styles.row, style]}>
24+
{columns.map((itm, idx) => {
25+
// row渲染的内容
26+
let val = '';
27+
if (itm.dataIndex.indexOf('.') > -1) {
28+
const firstKey = itm.dataIndex.split('.')[0];
29+
const secondKey = itm.dataIndex.split('.')[1];
30+
val = record[firstKey][secondKey];
31+
} else {
32+
val = record[itm.dataIndex];
33+
}
34+
35+
// 是否省略多余文字
36+
let textEllipsize: textEllipsizeState | any =
37+
itm.ellipsis && itm.ellipsis
38+
? {
39+
numberOfLines: 1,
40+
ellipsizeMode: 'tail',
41+
}
42+
: null;
43+
44+
return (
45+
<View key={itm.dataIndex} style={[styles.cell, itm.style]}>
46+
{itm.render ? (
47+
itm.render(record)
48+
) : (
49+
<Text selectable={true} {...textEllipsize} style={styles.content}>
50+
{val}
51+
</Text>
52+
)}
53+
</View>
54+
);
55+
})}
56+
</View>
57+
);
58+
}
59+
60+
const styles = StyleSheet.create({
61+
row: {
62+
flexDirection: 'row',
63+
justifyContent: 'center',
64+
alignContent: 'center',
65+
borderBottomWidth: 1,
66+
borderColor: '#E5E5E5',
67+
},
68+
cell: {
69+
flex: 1,
70+
flexDirection: 'row',
71+
justifyContent: 'center',
72+
borderRightWidth: 1,
73+
borderRightColor: '#E5E5E5',
74+
paddingTop: 4,
75+
paddingBottom: 4,
76+
backgroundColor: '#fff',
77+
},
78+
content: {
79+
color: '#888888',
80+
},
81+
});

components/Table/index.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
### Table组件
2+
3+
```
4+
<Table
5+
columns={[
6+
{
7+
title: '类型',
8+
dataIndex: 'reportType',
9+
ellipsis: true,
10+
},
11+
{
12+
title: '备注',
13+
dataIndex: 'remark',
14+
ellipsis: true,
15+
},
16+
{
17+
title: '操作',
18+
dataIndex: 'id',
19+
render: record => {
20+
return (
21+
<TouchableHighlight onPress={()=>{ }}>
22+
<Text style={{color: '#888'}}>查看</Text>
23+
</TouchableHighlight>
24+
);
25+
},
26+
},
27+
]}
28+
data={[
29+
{id: '1', reportType: '❤️爱永恒', remark: 'ff爱zz,三生三世用相随'},
30+
{id: '2', reportType: '❤️爱永恒', remark: 'ff爱zz,三生三世用相随'},
31+
{id: '3', reportType: '❤️爱永恒', remark: 'ff爱zz,三生三世用相随'},
32+
{id: '4', reportType: '❤️爱永恒', remark: 'ff爱zz,三生三世用相随'},
33+
]}
34+
rowKey="id"
35+
/>
36+
```
37+
### props
38+
39+
| 参数 | 说明 | 类型 | 默认值 |
40+
| -------------------- | ------------ | ------- | ------- |
41+
| `columns` | 表格列的配置描述,具体项见下表 | ColumnsType[] | - |
42+
| `data` | 数据数组 | object[] | - |
43+
| `rowKey` | 表格行 key 的取值,可以是字符串或一个函数 | string | function(record): string | key |
44+
45+
### Column
46+
47+
| 参数 | 说明 | 类型 | 默认值 |
48+
| -------------------- | ------------ | ------- | ------- |
49+
| `dataIndex` | 列数据在数据项中对应的路径,支持通过数组查询嵌套路径 | string | string[] | - |
50+
| `ellipsis` | 超过宽度将自动省略 | - |
51+
| `title` | 列头显示文字 | string | - |
52+
| `render` | 生成复杂数据的渲染函数,参数为当前行数据 | function(record) {} | - |

components/Table/index.tsx

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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;

src/routes.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,12 @@ export const stackPageData: Routes[] = [
249249
description: '模糊搜索select。',
250250
},
251251
},
252+
{
253+
name: 'Table',
254+
component: require('./routes/Table').default,
255+
params: {
256+
title: 'Table 表格',
257+
description: '表格Table',
258+
},
259+
},
252260
];

src/routes/Table/index.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React, { useState } from 'react';
2+
import { SafeAreaView,TouchableHighlight,Text } from 'react-native';
3+
import Table from '../../../components/Table/index'
4+
import Layout from '../../Layout';
5+
const SearchBarDemo = (props:any) => {
6+
7+
const { Header } = Layout;
8+
const {route} = props;
9+
const description = route.params.description;
10+
const title = route.params.title;
11+
12+
const [data, setData] = useState([
13+
{ label: '上海', value: 1 },
14+
{ label: '南京', value: 2 }
15+
])
16+
return (
17+
<SafeAreaView style={{ flex: 1 }}>
18+
<Header title={title} description={description} />
19+
<Table
20+
columns={[
21+
{
22+
title: '类型',
23+
dataIndex: 'reportType',
24+
ellipsis: true,
25+
},
26+
{
27+
title: '备注',
28+
dataIndex: 'remark',
29+
ellipsis: true,
30+
},
31+
{
32+
title: '操作',
33+
dataIndex: 'id',
34+
render: record => {
35+
return (
36+
<TouchableHighlight onPress={()=>{ }}>
37+
<Text style={{color: '#888'}}>查看</Text>
38+
</TouchableHighlight>
39+
);
40+
},
41+
},
42+
]}
43+
data={[
44+
{id: '1', reportType: '❤️爱永恒', remark: 'ff爱zz,三生三世用相随'},
45+
{id: '2', reportType: '❤️爱永恒', remark: 'ff爱zz,三生三世用相随'},
46+
{id: '3', reportType: '❤️爱永恒', remark: 'ff爱zz,三生三世用相随'},
47+
{id: '4', reportType: '❤️爱永恒', remark: 'ff爱zz,三生三世用相随'},
48+
]}
49+
rowKey="id"
50+
/>
51+
</SafeAreaView>
52+
);
53+
};
54+
export default SearchBarDemo;

0 commit comments

Comments
 (0)