Skip to content

Commit 0a8f332

Browse files
committed
feat: update type
1 parent 6a3da2d commit 0a8f332

File tree

3 files changed

+77
-60
lines changed

3 files changed

+77
-60
lines changed

src/components/Table/Table.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ const Table = forwardRef(
2828
useImperativeHandle(ref, () => {
2929
return {};
3030
});
31+
3132
if (groupBy) {
3233
const groupedData = lodashGroupBy(dataSource, groupBy);
3334
return (
3435
<View style={{ width: '100%', height: '100%' }}>
3536
{Object.keys(groupedData).map((key) => (
36-
<Table key={key} dataSource={groupedData[key]} columns={columns} />
37+
<Table
38+
key={key}
39+
dataSource={groupedData[key] as any}
40+
columns={columns}
41+
/>
3742
))}
3843
</View>
3944
);

src/components/Table/Table.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type Column<T = any> = {
4141
* @param item The data item for the row.
4242
* @returns The rendered React node.
4343
*/
44-
render?: (item: T) => React.ReactNode;
44+
render?: (item: T, index?: number) => React.ReactNode;
4545
/**
4646
* A function that renders the title of the column.
4747
* @param title The title of the column.

src/components/Table/TableFlatlist.tsx

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import React, { forwardRef, Fragment, memo, useCallback, useMemo } from 'react';
1+
import React, {
2+
forwardRef,
3+
Fragment,
4+
memo,
5+
useCallback,
6+
useImperativeHandle,
7+
useMemo,
8+
} from 'react';
29
import { FlatList, RefreshControl, Text, View } from 'react-native';
310

411
import type { Column, TableFlatListProps, TableRef } from './Table.types';
@@ -19,42 +26,40 @@ const NoData = ({ message }: NoDataProps) => (
1926
export const isNullOrUndefined = (data?: any): data is null | undefined =>
2027
data === null || data === undefined;
2128

22-
const RowItem = memo(
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-
))
51-
);
29+
const RowItem = memo(({ item, index, columns, rowStyle, layout }: any) => (
30+
<View
31+
style={[
32+
{
33+
flex: layout === 'vertical' ? 1 : undefined,
34+
flexDirection: layout === 'horizontal' ? 'column' : 'row',
35+
},
36+
rowStyle
37+
? typeof rowStyle === 'function'
38+
? rowStyle(item, index)
39+
: rowStyle
40+
: {},
41+
]}
42+
>
43+
{columns.map((column: Column, cidx: React.Key | null | undefined) => (
44+
<View
45+
key={cidx}
46+
style={[{ flex: layout == 'horizontal' ? 0 : 1 }, column.style]}
47+
>
48+
{column.render ? (
49+
column.render(item[column.dataIndex ?? column.key], index)
50+
) : (
51+
<Text>{item[column.key]}</Text>
52+
)}
53+
</View>
54+
))}
55+
</View>
56+
));
57+
5258
const TableFlatlist = forwardRef(
5359
(
5460
{
5561
dataSource: originalDataSource,
5662
columns,
57-
groupBy,
5863
layout,
5964
renderHeader,
6065
headerStyle,
@@ -91,6 +96,10 @@ const TableFlatlist = forwardRef(
9196
delete flatListPropsInner.isRefreshing;
9297
}
9398

99+
useImperativeHandle(ref, () => {
100+
return {};
101+
});
102+
94103
const renderNoDataItem = useCallback(() => {
95104
if (
96105
typeof noDataContent === 'string' ||
@@ -102,33 +111,36 @@ const TableFlatlist = forwardRef(
102111
return noDataContent!;
103112
}, [noDataContent]);
104113

105-
const renderItem = useCallback(({ item, index }: any) => {
106-
if (renderRow) {
107-
return renderRow({
108-
record: item,
109-
index,
110-
children: (
111-
<RowItem
112-
index={index}
113-
item={item}
114-
columns={columns}
115-
rowStyle={rowStyle}
116-
layout={layout}
117-
/>
118-
),
119-
});
120-
}
114+
const renderItem = useCallback(
115+
({ item, index }: any) => {
116+
if (renderRow) {
117+
return renderRow({
118+
record: item,
119+
index,
120+
children: (
121+
<RowItem
122+
index={index}
123+
item={item}
124+
columns={columns}
125+
rowStyle={rowStyle}
126+
layout={layout}
127+
/>
128+
),
129+
});
130+
}
121131

122-
return (
123-
<RowItem
124-
index={index}
125-
item={item}
126-
columns={columns}
127-
rowStyle={rowStyle}
128-
layout={layout}
129-
/>
130-
);
131-
}, []);
132+
return (
133+
<RowItem
134+
index={index}
135+
item={item}
136+
columns={columns}
137+
rowStyle={rowStyle}
138+
layout={layout}
139+
/>
140+
);
141+
},
142+
[columns, layout, renderRow, rowStyle]
143+
);
132144

133145
const keyExtractor = useCallback((item: any, index: any) => {
134146
return rowKey

0 commit comments

Comments
 (0)