diff --git a/src/components/Table/Table.tsx b/src/components/Table/Table.tsx index 832fe35..b4cea06 100644 --- a/src/components/Table/Table.tsx +++ b/src/components/Table/Table.tsx @@ -28,12 +28,17 @@ const Table = forwardRef( useImperativeHandle(ref, () => { return {}; }); + if (groupBy) { const groupedData = lodashGroupBy(dataSource, groupBy); return ( {Object.keys(groupedData).map((key) => ( - +
))} ); diff --git a/src/components/Table/Table.types.ts b/src/components/Table/Table.types.ts index fc10a85..bcef2a7 100644 --- a/src/components/Table/Table.types.ts +++ b/src/components/Table/Table.types.ts @@ -41,7 +41,7 @@ export type Column = { * @param item The data item for the row. * @returns The rendered React node. */ - render?: (item: T) => React.ReactNode; + render?: (item: T, index?: number) => React.ReactNode; /** * A function that renders the title of the column. * @param title The title of the column. diff --git a/src/components/Table/TableFlatlist.tsx b/src/components/Table/TableFlatlist.tsx index c4d8a77..c0fc90b 100644 --- a/src/components/Table/TableFlatlist.tsx +++ b/src/components/Table/TableFlatlist.tsx @@ -1,4 +1,11 @@ -import React, { forwardRef, Fragment, memo, useCallback, useMemo } from 'react'; +import React, { + forwardRef, + Fragment, + memo, + useCallback, + useImperativeHandle, + useMemo, +} from 'react'; import { FlatList, RefreshControl, Text, View } from 'react-native'; import type { Column, TableFlatListProps, TableRef } from './Table.types'; @@ -19,42 +26,40 @@ const NoData = ({ message }: NoDataProps) => ( export const isNullOrUndefined = (data?: any): data is null | undefined => data === null || data === undefined; -const RowItem = memo( - forwardRef(({ item, index, columns, rowStyle, layout }: any, ref) => ( - - {columns.map((column: Column, cidx: React.Key | null | undefined) => ( - - {column.render ? ( - column.render(item[column.dataIndex ?? column.key], index) - ) : ( - {item[column.key]} - )} - - ))} - - )) -); +const RowItem = memo(({ item, index, columns, rowStyle, layout }: any) => ( + + {columns.map((column: Column, cidx: React.Key | null | undefined) => ( + + {column.render ? ( + column.render(item[column.dataIndex ?? column.key], index) + ) : ( + {item[column.key]} + )} + + ))} + +)); + const TableFlatlist = forwardRef( ( { dataSource: originalDataSource, columns, - groupBy, layout, renderHeader, headerStyle, @@ -91,6 +96,10 @@ const TableFlatlist = forwardRef( delete flatListPropsInner.isRefreshing; } + useImperativeHandle(ref, () => { + return {}; + }); + const renderNoDataItem = useCallback(() => { if ( typeof noDataContent === 'string' || @@ -102,33 +111,36 @@ const TableFlatlist = forwardRef( return noDataContent!; }, [noDataContent]); - const renderItem = useCallback(({ item, index }: any) => { - if (renderRow) { - return renderRow({ - record: item, - index, - children: ( - - ), - }); - } + const renderItem = useCallback( + ({ item, index }: any) => { + if (renderRow) { + return renderRow({ + record: item, + index, + children: ( + + ), + }); + } - return ( - - ); - }, []); + return ( + + ); + }, + [columns, layout, renderRow, rowStyle] + ); const keyExtractor = useCallback((item: any, index: any) => { return rowKey