1- import React , { forwardRef , Fragment , memo , useCallback } from 'react' ;
1+ import React , { forwardRef , Fragment , memo , useCallback , useMemo } from 'react' ;
22import { FlatList , RefreshControl , Text , View } from 'react-native' ;
33
44import 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
1919export const isNullOrUndefined = ( data ?: any ) : data is null | undefined =>
2020 data === null || data === undefined ;
2121
2222const 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-
5852const 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
202198export default TableFlatlist ;
203-
204- const noDataSource = [ { id : 'no_data' , type : 'no_data' } ] ;
0 commit comments