|
| 1 | +import React, { forwardRef, Fragment, memo, useCallback } from 'react'; |
| 2 | +import { FlatList, RefreshControl, Text, View } from 'react-native'; |
| 3 | + |
| 4 | +import type { Column, TableFlatListProps, TableRef } from './Table.types'; |
| 5 | +import type { ViewProps } from 'react-native'; |
| 6 | + |
| 7 | +export type NoDataProps = ViewProps & { |
| 8 | + message?: string | null | undefined; |
| 9 | +}; |
| 10 | + |
| 11 | +const NoData = ({ message }: NoDataProps) => { |
| 12 | + return ( |
| 13 | + <View> |
| 14 | + <Text>{message}</Text> |
| 15 | + </View> |
| 16 | + ); |
| 17 | +}; |
| 18 | + |
| 19 | +export const isNullOrUndefined = (data?: any): data is null | undefined => |
| 20 | + data === null || data === undefined; |
| 21 | + |
| 22 | +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 | + }) |
| 56 | +); |
| 57 | + |
| 58 | +const TableFlatlist = forwardRef( |
| 59 | + ( |
| 60 | + { |
| 61 | + dataSource, |
| 62 | + columns, |
| 63 | + groupBy, |
| 64 | + layout, |
| 65 | + renderHeader, |
| 66 | + headerStyle, |
| 67 | + rowStyle, |
| 68 | + rowKey, |
| 69 | + style, |
| 70 | + renderRow, |
| 71 | + scrollEnabled = false, |
| 72 | + isStickyHeader, |
| 73 | + isNoData, |
| 74 | + noDataContent, |
| 75 | + ...flatListProps |
| 76 | + }: TableFlatListProps, |
| 77 | + ref?: React.Ref<TableRef | undefined> | undefined |
| 78 | + ) => { |
| 79 | + const isNoDataSource = |
| 80 | + (isNullOrUndefined(isNoData) && dataSource.length === 0) || |
| 81 | + (!isNullOrUndefined(isNoData) && isNoData); |
| 82 | + |
| 83 | + dataSource = isNoDataSource ? noDataSource : dataSource; |
| 84 | + const flatListPropsInner = { ...flatListProps }; |
| 85 | + |
| 86 | + if (!flatListPropsInner.refreshControl) { |
| 87 | + if (flatListPropsInner.onRefresh) { |
| 88 | + const onRefresh = flatListPropsInner.onRefresh; |
| 89 | + const isRefreshing = flatListPropsInner.isRefreshing; |
| 90 | + flatListPropsInner.refreshControl = ( |
| 91 | + <RefreshControl refreshing={!!isRefreshing} onRefresh={onRefresh} /> |
| 92 | + ); |
| 93 | + } |
| 94 | + delete flatListPropsInner.onRefresh; |
| 95 | + delete flatListPropsInner.isRefreshing; |
| 96 | + } |
| 97 | + |
| 98 | + const renderNoDataItem = useCallback(() => { |
| 99 | + if ( |
| 100 | + typeof noDataContent === 'string' || |
| 101 | + isNullOrUndefined(noDataContent) |
| 102 | + ) { |
| 103 | + return <NoData message={noDataContent} />; |
| 104 | + } |
| 105 | + |
| 106 | + return noDataContent!; |
| 107 | + }, [noDataContent]); |
| 108 | + |
| 109 | + const renderItem = useCallback(({ item, index }: any) => { |
| 110 | + if (renderRow) { |
| 111 | + return renderRow({ |
| 112 | + record: item, |
| 113 | + index, |
| 114 | + children: ( |
| 115 | + <RowItem |
| 116 | + index={index} |
| 117 | + item={item} |
| 118 | + columns={columns} |
| 119 | + rowStyle={rowStyle} |
| 120 | + layout={layout} |
| 121 | + /> |
| 122 | + ), |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + return ( |
| 127 | + <RowItem |
| 128 | + index={index} |
| 129 | + item={item} |
| 130 | + columns={columns} |
| 131 | + rowStyle={rowStyle} |
| 132 | + layout={layout} |
| 133 | + /> |
| 134 | + ); |
| 135 | + }, []); |
| 136 | + |
| 137 | + const keyExtractor = useCallback((item: any, index: any) => { |
| 138 | + return rowKey |
| 139 | + ? typeof rowKey === 'function' |
| 140 | + ? rowKey(item) |
| 141 | + : item[rowKey] |
| 142 | + : `row-${index}`; |
| 143 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 144 | + }, []); |
| 145 | + |
| 146 | + const listHeaderComponent = useCallback((): React.ReactElement => { |
| 147 | + if (renderHeader) { |
| 148 | + return renderHeader(columns); |
| 149 | + } |
| 150 | + return ( |
| 151 | + <View |
| 152 | + style={[ |
| 153 | + { |
| 154 | + flexDirection: layout === 'horizontal' ? 'column' : 'row', |
| 155 | + flex: layout === 'vertical' ? 1 : undefined, |
| 156 | + }, |
| 157 | + headerStyle, |
| 158 | + ]} |
| 159 | + > |
| 160 | + {columns.map((column, cidx) => ( |
| 161 | + <View |
| 162 | + key={cidx} |
| 163 | + style={[ |
| 164 | + { flex: layout == 'horizontal' ? 0 : 1 }, |
| 165 | + column.style, |
| 166 | + column.headerStyle, |
| 167 | + ]} |
| 168 | + > |
| 169 | + {column.renderTitle ? ( |
| 170 | + <Fragment>{column.renderTitle(column.title)}</Fragment> |
| 171 | + ) : ( |
| 172 | + <> |
| 173 | + <Text style={[column.titleStyle]}>{column.title}</Text> |
| 174 | + </> |
| 175 | + )} |
| 176 | + </View> |
| 177 | + ))} |
| 178 | + </View> |
| 179 | + ); |
| 180 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 181 | + }, []); |
| 182 | + |
| 183 | + return ( |
| 184 | + <FlatList |
| 185 | + style={[{ display: 'flex' }, style]} |
| 186 | + scrollEnabled={scrollEnabled} |
| 187 | + data={dataSource} |
| 188 | + windowSize={30} |
| 189 | + initialNumToRender={20} |
| 190 | + renderItem={isNoDataSource ? renderNoDataItem : renderItem} |
| 191 | + maxToRenderPerBatch={6} |
| 192 | + keyExtractor={keyExtractor} |
| 193 | + ListHeaderComponent={listHeaderComponent} |
| 194 | + stickyHeaderIndices={isStickyHeader ? [0] : undefined} |
| 195 | + horizontal={layout === 'horizontal'} |
| 196 | + {...flatListPropsInner} |
| 197 | + /> |
| 198 | + ); |
| 199 | + } |
| 200 | +); |
| 201 | + |
| 202 | +export default TableFlatlist; |
| 203 | + |
| 204 | +const noDataSource = [{ id: 'no_data', type: 'no_data' }]; |
0 commit comments