-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathDataTable.tsx
138 lines (128 loc) · 3.7 KB
/
DataTable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as React from 'react';
import { StyleSheet, StyleProp, View, ViewStyle } from 'react-native';
import DataTableCell from './DataTableCell';
import DataTableHeader, {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
DataTableHeader as _DataTableHeader,
} from './DataTableHeader';
import DataTablePagination, {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
DataTablePagination as _DataTablePagination,
} from './DataTablePagination';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import DataTableRow, { DataTableRow as _DataTableRow } from './DataTableRow';
import DataTableTitle, {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
DataTableTitle as _DataTableTitle,
} from './DataTableTitle';
export type Props = React.ComponentPropsWithRef<typeof View> & {
/**
* Content of the `DataTable`.
*/
children: React.ReactNode;
style?: StyleProp<ViewStyle>;
};
/**
* Data tables allow displaying sets of data.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { DataTable } from 'react-native-paper';
*
* const MyComponent = () => {
* const [page, setPage] = React.useState<number>(0);
* const [numberOfItemsPerPageList] = React.useState([2, 3, 4]);
* const [itemsPerPage, onItemsPerPageChange] = React.useState(
* numberOfItemsPerPageList[0]
* );
*
* const [items] = React.useState([
* {
* key: 1,
* name: 'Cupcake',
* calories: 356,
* fat: 16,
* },
* {
* key: 2,
* name: 'Eclair',
* calories: 262,
* fat: 16,
* },
* {
* key: 3,
* name: 'Frozen yogurt',
* calories: 159,
* fat: 6,
* },
* {
* key: 4,
* name: 'Gingerbread',
* calories: 305,
* fat: 3.7,
* },
* ]);
*
* const from = page * itemsPerPage;
* const to = Math.min((page + 1) * itemsPerPage, items.length);
*
* React.useEffect(() => {
* setPage(0);
* }, [itemsPerPage]);
*
* return (
* <DataTable>
* <DataTable.Header>
* <DataTable.Title>Dessert</DataTable.Title>
* <DataTable.Title numeric>Calories</DataTable.Title>
* <DataTable.Title numeric>Fat</DataTable.Title>
* </DataTable.Header>
*
* {items.slice(from, to).map((item) => (
* <DataTable.Row key={item.key}>
* <DataTable.Cell>{item.name}</DataTable.Cell>
* <DataTable.Cell numeric>{item.calories}</DataTable.Cell>
* <DataTable.Cell numeric>{item.fat}</DataTable.Cell>
* </DataTable.Row>
* ))}
*
* <DataTable.Pagination
* page={page}
* numberOfPages={Math.ceil(items.length / itemsPerPage)}
* onPageChange={(page) => setPage(page)}
* label={`${from + 1}-${to} of ${items.length}`}
* numberOfItemsPerPageList={numberOfItemsPerPageList}
* numberOfItemsPerPage={itemsPerPage}
* onItemsPerPageChange={onItemsPerPageChange}
* showFastPaginationControls
* selectPageDropdownLabel={'Rows per page'}
* />
* </DataTable>
* );
* };
*
* export default MyComponent;
* ```
*/
const DataTable = ({ children, style, ...rest }: Props) => (
<View {...rest} style={[styles.container, style]}>
{children}
</View>
);
// @component ./DataTableHeader.tsx
DataTable.Header = DataTableHeader;
// @component ./DataTableTitle.tsx
DataTable.Title = DataTableTitle;
// @component ./DataTableRow.tsx
DataTable.Row = DataTableRow;
// @component ./DataTableCell.tsx
DataTable.Cell = DataTableCell;
// @component ./DataTablePagination.tsx
DataTable.Pagination = DataTablePagination;
const styles = StyleSheet.create({
container: {
width: '100%',
},
});
export default DataTable;