Skip to content

Commit ff3a710

Browse files
committed
refactor: table flatlist
1 parent 369db76 commit ff3a710

File tree

1 file changed

+44
-50
lines changed

1 file changed

+44
-50
lines changed

src/components/Table/TableFlatlist.tsx

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, Fragment, memo, useCallback } from 'react';
1+
import React, { forwardRef, Fragment, memo, useCallback, useMemo } from 'react';
22
import { FlatList, RefreshControl, Text, View } from 'react-native';
33

44
import type { Column, TableFlatListProps, TableRef } from './Table.types';
@@ -8,57 +8,51 @@ export type NoDataProps = ViewProps & {
88
message?: string | null | undefined;
99
};
1010

11-
const NoData = ({ message }: NoDataProps) => {
12-
return (
13-
<View>
14-
<Text>{message}</Text>
15-
</View>
16-
);
17-
};
11+
const noDataSource = [{ id: 'no_data', type: 'no_data' }];
12+
13+
const NoData = ({ message }: NoDataProps) => (
14+
<View>
15+
<Text>{message}</Text>
16+
</View>
17+
);
1818

1919
export const isNullOrUndefined = (data?: any): data is null | undefined =>
2020
data === null || data === undefined;
2121

2222
const RowItem = memo(
23-
forwardRef(({ item, index, columns, rowStyle, layout }: any, ref) => {
24-
return (
25-
<View
26-
style={[
27-
{
28-
flex: layout === 'vertical' ? 1 : undefined,
29-
flexDirection: layout === 'horizontal' ? 'column' : 'row',
30-
},
31-
rowStyle
32-
? typeof rowStyle === 'function'
33-
? rowStyle(item, index)
34-
: rowStyle
35-
: {},
36-
]}
37-
>
38-
{columns.map((column: Column, cidx: React.Key | null | undefined) => (
39-
<View
40-
key={cidx}
41-
style={[{ flex: layout == 'horizontal' ? 0 : 1 }, column.style]}
42-
>
43-
{column.render ? (
44-
column.render(
45-
column.dataIndex ? item[column.dataIndex] : item,
46-
index
47-
)
48-
) : (
49-
<Text>{item[column.key]}</Text>
50-
)}
51-
</View>
52-
))}
53-
</View>
54-
);
55-
})
23+
forwardRef(({ item, index, columns, rowStyle, layout }: any, ref) => (
24+
<View
25+
style={[
26+
{
27+
flex: layout === 'vertical' ? 1 : undefined,
28+
flexDirection: layout === 'horizontal' ? 'column' : 'row',
29+
},
30+
rowStyle
31+
? typeof rowStyle === 'function'
32+
? rowStyle(item, index)
33+
: rowStyle
34+
: {},
35+
]}
36+
>
37+
{columns.map((column: Column, cidx: React.Key | null | undefined) => (
38+
<View
39+
key={cidx}
40+
style={[{ flex: layout == 'horizontal' ? 0 : 1 }, column.style]}
41+
>
42+
{column.render ? (
43+
column.render(item[column.dataIndex ?? column.key], index)
44+
) : (
45+
<Text>{item[column.key]}</Text>
46+
)}
47+
</View>
48+
))}
49+
</View>
50+
))
5651
);
57-
5852
const TableFlatlist = forwardRef(
5953
(
6054
{
61-
dataSource,
55+
dataSource: originalDataSource,
6256
columns,
6357
groupBy,
6458
layout,
@@ -76,13 +70,15 @@ const TableFlatlist = forwardRef(
7670
}: TableFlatListProps,
7771
ref?: React.Ref<TableRef | undefined> | undefined
7872
) => {
79-
const isNoDataSource =
80-
(isNullOrUndefined(isNoData) && dataSource.length === 0) ||
81-
(!isNullOrUndefined(isNoData) && isNoData);
73+
const isNoDataSource = useMemo(
74+
() =>
75+
(isNullOrUndefined(isNoData) && originalDataSource.length === 0) ||
76+
(!isNullOrUndefined(isNoData) && isNoData),
77+
[isNoData, originalDataSource]
78+
);
79+
const dataSource = isNoDataSource ? noDataSource : originalDataSource;
8280

83-
dataSource = isNoDataSource ? noDataSource : dataSource;
8481
const flatListPropsInner = { ...flatListProps };
85-
8682
if (!flatListPropsInner.refreshControl) {
8783
if (flatListPropsInner.onRefresh) {
8884
const onRefresh = flatListPropsInner.onRefresh;
@@ -200,5 +196,3 @@ const TableFlatlist = forwardRef(
200196
);
201197

202198
export default TableFlatlist;
203-
204-
const noDataSource = [{ id: 'no_data', type: 'no_data' }];

0 commit comments

Comments
 (0)