-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTable.jsx
More file actions
95 lines (90 loc) · 2.59 KB
/
Copy pathTable.jsx
File metadata and controls
95 lines (90 loc) · 2.59 KB
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
import PropTypes from 'prop-types';
import React from 'react';
import { withGlobalProps } from '../../providers/globalProps';
import { transferProps } from '../../helpers/transferProps';
import { TableHeaderCell } from './_components/TableHeaderCell';
import { TableBodyCell } from './_components/TableBodyCell';
import styles from './Table.module.scss';
export const Table = ({
columns,
id,
rows,
sort,
...restProps
}) => (
<table
{...transferProps(restProps)}
className={styles.table}
id={id}
>
<thead>
<tr className={styles.tableHeadRow}>
{columns.map((column) => (
<TableHeaderCell
column={column}
key={column.name}
sort={sort}
{...(id && { id: `${id}__headerCell__${column.name}` })}
/>
))}
</tr>
</thead>
<tbody>
{rows.map((row) => (
<tr
className={styles.tableRow}
key={row.id}
>
{columns.map((column) => (
<TableBodyCell
format={column.format}
isSortingActive={sort && column.name === sort.column}
key={`${row.id}-${column.name}`}
value={row[column.name]}
{...(id && { id: `${id}__bodyCell__${column.name}__${row.id}` })}
/>
))}
</tr>
))}
</tbody>
</table>
);
Table.defaultProps = {
id: undefined,
sort: undefined,
};
Table.propTypes = {
/**
* Table data columns, optionally sortable. The `format` function can be used to process the
* column data before displaying them.
*/
columns: PropTypes.arrayOf(PropTypes.shape({
format: PropTypes.func,
isSortable: PropTypes.bool,
label: PropTypes.string,
name: PropTypes.string.isRequired,
})).isRequired,
/**
* ID of the root HTML element. It also serves as base for nested elements:
* * `<ID>__headerCell__<COLUMN_NAME>`
* * `<ID>__headerCell__<COLUMN_NAME>__sortButton`
* * `<ID>__bodyCell__<COLUMN_NAME>__<ROW_ID>`
*/
id: PropTypes.string,
/**
* Table data rows, each object key must match a column `name`
*/
rows: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
/**
* Sorting configuration required to make columns sortable.
*/
sort: PropTypes.shape({
ascendingIcon: PropTypes.node.isRequired,
column: PropTypes.string.isRequired,
descendingIcon: PropTypes.node.isRequired,
direction: PropTypes.oneOf(['asc', 'desc']).isRequired,
onClick: PropTypes.func.isRequired,
}),
};
export const TableWithGlobalProps = withGlobalProps(Table, 'Table');
export default TableWithGlobalProps;