Skip to content

Commit

Permalink
feat: update type
Browse files Browse the repository at this point in the history
  • Loading branch information
npv2k1 committed Mar 10, 2024
1 parent 6a3da2d commit 0a8f332
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 60 deletions.
7 changes: 6 additions & 1 deletion src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ const Table = forwardRef(
useImperativeHandle(ref, () => {
return {};
});

if (groupBy) {
const groupedData = lodashGroupBy(dataSource, groupBy);
return (
<View style={{ width: '100%', height: '100%' }}>

Check warning on line 35 in src/components/Table/Table.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { width: '100%', height: '100%' }
{Object.keys(groupedData).map((key) => (
<Table key={key} dataSource={groupedData[key]} columns={columns} />
<Table
key={key}
dataSource={groupedData[key] as any}
columns={columns}
/>
))}
</View>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Table/Table.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type Column<T = any> = {
* @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.
Expand Down
128 changes: 70 additions & 58 deletions src/components/Table/TableFlatlist.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) => (
<View
style={[
{
flex: layout === 'vertical' ? 1 : undefined,
flexDirection: layout === 'horizontal' ? 'column' : 'row',
},
rowStyle
? typeof rowStyle === 'function'
? rowStyle(item, index)
: rowStyle
: {},
]}
>
{columns.map((column: Column, cidx: React.Key | null | undefined) => (
<View
key={cidx}
style={[{ flex: layout == 'horizontal' ? 0 : 1 }, column.style]}
>
{column.render ? (
column.render(item[column.dataIndex ?? column.key], index)
) : (
<Text>{item[column.key]}</Text>
)}
</View>
))}
</View>
))
);
const RowItem = memo(({ item, index, columns, rowStyle, layout }: any) => (
<View
style={[
{
flex: layout === 'vertical' ? 1 : undefined,
flexDirection: layout === 'horizontal' ? 'column' : 'row',
},
rowStyle
? typeof rowStyle === 'function'
? rowStyle(item, index)
: rowStyle
: {},
]}
>
{columns.map((column: Column, cidx: React.Key | null | undefined) => (
<View
key={cidx}
style={[{ flex: layout == 'horizontal' ? 0 : 1 }, column.style]}
>
{column.render ? (
column.render(item[column.dataIndex ?? column.key], index)
) : (
<Text>{item[column.key]}</Text>
)}
</View>
))}
</View>
));

const TableFlatlist = forwardRef(
(
{
dataSource: originalDataSource,
columns,
groupBy,
layout,
renderHeader,
headerStyle,
Expand Down Expand Up @@ -91,6 +96,10 @@ const TableFlatlist = forwardRef(
delete flatListPropsInner.isRefreshing;
}

useImperativeHandle(ref, () => {
return {};
});

const renderNoDataItem = useCallback(() => {
if (
typeof noDataContent === 'string' ||
Expand All @@ -102,33 +111,36 @@ const TableFlatlist = forwardRef(
return noDataContent!;
}, [noDataContent]);

const renderItem = useCallback(({ item, index }: any) => {
if (renderRow) {
return renderRow({
record: item,
index,
children: (
<RowItem
index={index}
item={item}
columns={columns}
rowStyle={rowStyle}
layout={layout}
/>
),
});
}
const renderItem = useCallback(
({ item, index }: any) => {
if (renderRow) {
return renderRow({
record: item,
index,
children: (
<RowItem
index={index}
item={item}
columns={columns}
rowStyle={rowStyle}
layout={layout}
/>
),
});
}

return (
<RowItem
index={index}
item={item}
columns={columns}
rowStyle={rowStyle}
layout={layout}
/>
);
}, []);
return (
<RowItem
index={index}
item={item}
columns={columns}
rowStyle={rowStyle}
layout={layout}
/>
);
},
[columns, layout, renderRow, rowStyle]
);

const keyExtractor = useCallback((item: any, index: any) => {
return rowKey
Expand Down

0 comments on commit 0a8f332

Please sign in to comment.