@tanstack/react-table 0.0.1-alpha.9 → 8.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +112 -0
  2. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
  3. package/build/cjs/aggregationTypes.js +130 -0
  4. package/build/cjs/aggregationTypes.js.map +1 -0
  5. package/build/cjs/core.js +552 -0
  6. package/build/cjs/core.js.map +1 -0
  7. package/build/cjs/createTable.js +108 -0
  8. package/build/cjs/createTable.js.map +1 -0
  9. package/build/cjs/features/ColumnSizing.js +317 -0
  10. package/build/cjs/features/ColumnSizing.js.map +1 -0
  11. package/build/cjs/features/Expanding.js +255 -0
  12. package/build/cjs/features/Expanding.js.map +1 -0
  13. package/build/cjs/features/Filters.js +445 -0
  14. package/build/cjs/features/Filters.js.map +1 -0
  15. package/build/cjs/features/Grouping.js +249 -0
  16. package/build/cjs/features/Grouping.js.map +1 -0
  17. package/build/cjs/features/Headers.js +549 -0
  18. package/build/cjs/features/Headers.js.map +1 -0
  19. package/build/cjs/features/Ordering.js +86 -0
  20. package/build/cjs/features/Ordering.js.map +1 -0
  21. package/build/cjs/features/Pagination.js +194 -0
  22. package/build/cjs/features/Pagination.js.map +1 -0
  23. package/build/cjs/features/Pinning.js +149 -0
  24. package/build/cjs/features/Pinning.js.map +1 -0
  25. package/build/cjs/features/RowSelection.js +541 -0
  26. package/build/cjs/features/RowSelection.js.map +1 -0
  27. package/build/cjs/features/Sorting.js +327 -0
  28. package/build/cjs/features/Sorting.js.map +1 -0
  29. package/build/cjs/features/Visibility.js +166 -0
  30. package/build/cjs/features/Visibility.js.map +1 -0
  31. package/build/cjs/filterTypes.js +172 -0
  32. package/build/cjs/filterTypes.js.map +1 -0
  33. package/build/cjs/index.js +30 -0
  34. package/build/cjs/index.js.map +1 -0
  35. package/build/cjs/sortTypes.js +122 -0
  36. package/build/cjs/sortTypes.js.map +1 -0
  37. package/build/cjs/utils/columnFilterRowsFn.js +131 -0
  38. package/build/cjs/utils/columnFilterRowsFn.js.map +1 -0
  39. package/build/cjs/utils/expandRowsFn.js +38 -0
  40. package/build/cjs/utils/expandRowsFn.js.map +1 -0
  41. package/build/cjs/utils/globalFilterRowsFn.js +101 -0
  42. package/build/cjs/utils/globalFilterRowsFn.js.map +1 -0
  43. package/build/cjs/utils/groupRowsFn.js +155 -0
  44. package/build/cjs/utils/groupRowsFn.js.map +1 -0
  45. package/build/cjs/utils/sortRowsFn.js +94 -0
  46. package/build/cjs/utils/sortRowsFn.js.map +1 -0
  47. package/build/cjs/utils.js +146 -0
  48. package/build/cjs/utils.js.map +1 -0
  49. package/build/esm/index.js +4747 -0
  50. package/build/esm/index.js.map +1 -0
  51. package/build/stats-html.html +2689 -0
  52. package/build/stats-react.json +817 -0
  53. package/build/types/aggregationTypes.d.ts +22 -0
  54. package/build/types/core.d.ts +111 -0
  55. package/build/types/createTable.d.ts +53 -0
  56. package/build/types/features/ColumnSizing.d.ts +67 -0
  57. package/build/types/features/Expanding.d.ts +53 -0
  58. package/build/types/features/Filters.d.ts +98 -0
  59. package/build/types/features/Grouping.d.ts +82 -0
  60. package/build/types/features/Headers.d.ts +41 -0
  61. package/build/types/features/Ordering.d.ts +19 -0
  62. package/build/types/features/Pagination.d.ts +43 -0
  63. package/build/types/features/Pinning.d.ts +39 -0
  64. package/build/types/features/RowSelection.d.ts +66 -0
  65. package/build/types/features/Sorting.d.ts +78 -0
  66. package/build/types/features/Visibility.d.ts +47 -0
  67. package/build/types/filterTypes.d.ts +50 -0
  68. package/build/types/index.d.ts +7 -0
  69. package/build/types/sortTypes.d.ts +18 -0
  70. package/build/types/types.d.ts +127 -0
  71. package/build/types/utils/columnFilterRowsFn.d.ts +2 -0
  72. package/build/types/utils/expandRowsFn.d.ts +2 -0
  73. package/build/types/utils/globalFilterRowsFn.d.ts +2 -0
  74. package/build/types/utils/groupRowsFn.d.ts +2 -0
  75. package/build/types/utils/paginateRowsFn.d.ts +2 -0
  76. package/build/types/utils/sortRowsFn.d.ts +2 -0
  77. package/build/types/utils.d.ts +28 -0
  78. package/{dist/react-table.development.js → build/umd/index.development.js} +1617 -249
  79. package/build/umd/index.development.js.map +1 -0
  80. package/build/umd/index.production.js +12 -0
  81. package/build/umd/index.production.js.map +1 -0
  82. package/package.json +9 -94
  83. package/src/core.tsx +252 -264
  84. package/src/createTable.tsx +69 -9
  85. package/src/features/ColumnSizing.ts +453 -0
  86. package/src/features/Expanding.ts +27 -11
  87. package/src/features/Filters.ts +75 -20
  88. package/src/features/Grouping.ts +27 -12
  89. package/src/features/Headers.ts +55 -50
  90. package/src/features/Ordering.ts +2 -3
  91. package/src/features/Pagination.ts +314 -0
  92. package/src/features/Pinning.ts +3 -16
  93. package/src/features/RowSelection.ts +831 -0
  94. package/src/features/Sorting.ts +82 -22
  95. package/src/features/Visibility.ts +2 -4
  96. package/src/sortTypes.ts +1 -1
  97. package/src/types.ts +55 -9
  98. package/src/utils/columnFilterRowsFn.ts +5 -12
  99. package/src/utils/expandRowsFn.ts +2 -5
  100. package/src/utils/globalFilterRowsFn.ts +3 -10
  101. package/src/utils/groupRowsFn.ts +3 -5
  102. package/src/utils/paginateRowsFn.ts +34 -0
  103. package/src/utils/sortRowsFn.ts +5 -5
  104. package/src/utils.tsx +20 -6
  105. package/dist/react-table.development.js.map +0 -1
  106. package/dist/react-table.production.min.js +0 -2
  107. package/dist/react-table.production.min.js.map +0 -1
  108. package/lib/index.js +0 -65
  109. package/src/features/notest/useAbsoluteLayout.test.js +0 -152
  110. package/src/features/notest/useBlockLayout.test.js +0 -158
  111. package/src/features/notest/useColumnOrder.test.js +0 -186
  112. package/src/features/notest/useExpanded.test.js +0 -125
  113. package/src/features/notest/useFilters.test.js +0 -393
  114. package/src/features/notest/useFiltersAndRowSelect.test.js +0 -256
  115. package/src/features/notest/useFlexLayout.test.js +0 -152
  116. package/src/features/notest/useGroupBy.test.js +0 -259
  117. package/src/features/notest/usePagination.test.js +0 -231
  118. package/src/features/notest/useResizeColumns.test.js +0 -229
  119. package/src/features/notest/useRowSelect.test.js +0 -250
  120. package/src/features/notest/useRowState.test.js +0 -178
  121. package/src/features/tests/Visibility.test.tsx +0 -225
  122. package/src/features/tests/__snapshots__/Visibility.test.tsx.snap +0 -390
  123. package/src/features/tests/withSorting.notest.tsx +0 -341
  124. package/src/features/withColumnResizing.ts +0 -281
  125. package/src/features/withPagination.ts +0 -208
  126. package/src/features/withRowSelection.ts +0 -467
  127. package/src/test-utils/makeTestData.ts +0 -41
  128. package/src/tests/__snapshots__/core.test.tsx.snap +0 -148
  129. package/src/tests/core.test.tsx +0 -241
package/lib/index.js DELETED
@@ -1,65 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- var _exportNames = {
5
- createTable: true
6
- };
7
- exports.createTable = void 0;
8
-
9
- var _types = require("./types");
10
-
11
- Object.keys(_types).forEach(function (key) {
12
- if (key === "default" || key === "__esModule") return;
13
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
14
- if (key in exports && exports[key] === _types[key]) return;
15
- exports[key] = _types[key];
16
- });
17
-
18
- var _columnFilterRowsFn = require("./utils/columnFilterRowsFn");
19
-
20
- Object.keys(_columnFilterRowsFn).forEach(function (key) {
21
- if (key === "default" || key === "__esModule") return;
22
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
23
- if (key in exports && exports[key] === _columnFilterRowsFn[key]) return;
24
- exports[key] = _columnFilterRowsFn[key];
25
- });
26
-
27
- var _globalFilterRowsFn = require("./utils/globalFilterRowsFn");
28
-
29
- Object.keys(_globalFilterRowsFn).forEach(function (key) {
30
- if (key === "default" || key === "__esModule") return;
31
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
32
- if (key in exports && exports[key] === _globalFilterRowsFn[key]) return;
33
- exports[key] = _globalFilterRowsFn[key];
34
- });
35
-
36
- var _sortRowsFn = require("./utils/sortRowsFn");
37
-
38
- Object.keys(_sortRowsFn).forEach(function (key) {
39
- if (key === "default" || key === "__esModule") return;
40
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
41
- if (key in exports && exports[key] === _sortRowsFn[key]) return;
42
- exports[key] = _sortRowsFn[key];
43
- });
44
-
45
- var _groupRowsFn = require("./utils/groupRowsFn");
46
-
47
- Object.keys(_groupRowsFn).forEach(function (key) {
48
- if (key === "default" || key === "__esModule") return;
49
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
50
- if (key in exports && exports[key] === _groupRowsFn[key]) return;
51
- exports[key] = _groupRowsFn[key];
52
- });
53
-
54
- var _expandRowsFn = require("./utils/expandRowsFn");
55
-
56
- Object.keys(_expandRowsFn).forEach(function (key) {
57
- if (key === "default" || key === "__esModule") return;
58
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
59
- if (key in exports && exports[key] === _expandRowsFn[key]) return;
60
- exports[key] = _expandRowsFn[key];
61
- });
62
-
63
- var _createTable = require("./createTable");
64
-
65
- exports.createTable = _createTable.createTable;
@@ -1,152 +0,0 @@
1
- import React from 'react'
2
- import { render } from '@testing-library/react'
3
- import { useTable } from '../../hooks/useTable'
4
- import { useAbsoluteLayout } from '../useAbsoluteLayout'
5
-
6
- const data = [
7
- {
8
- firstName: 'tanner',
9
- lastName: 'linsley',
10
- age: 29,
11
- visits: 100,
12
- status: 'In Relationship',
13
- progress: 50,
14
- },
15
- {
16
- firstName: 'derek',
17
- lastName: 'perkins',
18
- age: 30,
19
- visits: 40,
20
- status: 'Single',
21
- progress: 80,
22
- },
23
- {
24
- firstName: 'joe',
25
- lastName: 'bergevin',
26
- age: 45,
27
- visits: 20,
28
- status: 'Complicated',
29
- progress: 10,
30
- },
31
- ]
32
-
33
- const defaultColumn = {
34
- Cell: ({ value, column: { id } }) => `${id}: ${value}`,
35
- width: 200,
36
- minWidth: 100,
37
- maxWidth: 300,
38
- }
39
-
40
- function Table({ columns, data }) {
41
- const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
42
- useTable(
43
- {
44
- columns,
45
- data,
46
- defaultColumn,
47
- },
48
- useAbsoluteLayout
49
- )
50
-
51
- return (
52
- <div {...getTableProps()} className="table">
53
- <div>
54
- {headerGroups.map(headerGroup => (
55
- <div {...headerGroup.getHeaderGroupProps()} className="row">
56
- {headerGroup.headers.map(column => (
57
- <div {...column.getHeaderProps()} className="cell header">
58
- {column.render('Header')}
59
- </div>
60
- ))}
61
- </div>
62
- ))}
63
- </div>
64
-
65
- <div {...getTableBodyProps()}>
66
- {rows.map(
67
- (row, i) =>
68
- prepareRow(row) || (
69
- <div {...row.getRowProps()} className="row">
70
- {row.cells.map(cell => {
71
- return (
72
- <div {...cell.getCellProps()} className="cell">
73
- {cell.render('Cell')}
74
- </div>
75
- )
76
- })}
77
- </div>
78
- )
79
- )}
80
- </div>
81
- </div>
82
- )
83
- }
84
-
85
- function App() {
86
- const columns = React.useMemo(
87
- () => [
88
- {
89
- Header: 'Name',
90
- columns: [
91
- {
92
- Header: 'First Name',
93
- accessor: 'firstName',
94
- width: 250,
95
- },
96
- {
97
- Header: 'Last Name',
98
- accessor: 'lastName',
99
- width: 350,
100
- },
101
- ],
102
- },
103
- {
104
- Header: 'Info',
105
- columns: [
106
- {
107
- Header: 'Age',
108
- accessor: 'age',
109
- minWidth: 300,
110
- },
111
- {
112
- Header: 'Visits',
113
- accessor: 'visits',
114
- maxWidth: 150,
115
- },
116
- {
117
- Header: 'Status',
118
- accessor: 'status',
119
- },
120
- {
121
- Header: 'Profile Progress',
122
- accessor: 'progress',
123
- },
124
- ],
125
- },
126
- ],
127
- []
128
- )
129
-
130
- return <Table columns={columns} data={data} />
131
- }
132
-
133
- test('renders a table', () => {
134
- const rtl = render(<App />)
135
-
136
- expect(
137
- rtl.getAllByRole('columnheader').every(d => d.style.position === 'absolute')
138
- ).toBe(true)
139
-
140
- expect(
141
- rtl.getAllByRole('columnheader').map(d => [d.style.left, d.style.width])
142
- ).toStrictEqual([
143
- ['0px', '550px'],
144
- ['550px', '850px'],
145
- ['0px', '250px'],
146
- ['250px', '300px'],
147
- ['550px', '300px'],
148
- ['850px', '150px'],
149
- ['1000px', '200px'],
150
- ['1200px', '200px'],
151
- ])
152
- })
@@ -1,158 +0,0 @@
1
- import React from 'react'
2
- import { render } from '@testing-library/react'
3
- import { useTable } from '../../hooks/useTable'
4
- import { useBlockLayout } from '../useBlockLayout'
5
-
6
- const data = [
7
- {
8
- firstName: 'tanner',
9
- lastName: 'linsley',
10
- age: 29,
11
- visits: 100,
12
- status: 'In Relationship',
13
- progress: 50,
14
- },
15
- {
16
- firstName: 'derek',
17
- lastName: 'perkins',
18
- age: 30,
19
- visits: 40,
20
- status: 'Single',
21
- progress: 80,
22
- },
23
- {
24
- firstName: 'joe',
25
- lastName: 'bergevin',
26
- age: 45,
27
- visits: 20,
28
- status: 'Complicated',
29
- progress: 10,
30
- },
31
- ]
32
-
33
- const defaultColumn = {
34
- Cell: ({ value, column: { id } }) => `${id}: ${value}`,
35
- width: 200,
36
- minWidth: 100,
37
- maxWidth: 300,
38
- }
39
-
40
- function Table({ columns, data }) {
41
- const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
42
- useTable(
43
- {
44
- columns,
45
- data,
46
- defaultColumn,
47
- },
48
- useBlockLayout
49
- )
50
-
51
- return (
52
- <div {...getTableProps()} className="table">
53
- <div>
54
- {headerGroups.map(headerGroup => (
55
- <div {...headerGroup.getHeaderGroupProps()} className="row">
56
- {headerGroup.headers.map(column => (
57
- <div {...column.getHeaderProps()} className="cell header">
58
- {column.render('Header')}
59
- </div>
60
- ))}
61
- </div>
62
- ))}
63
- </div>
64
-
65
- <div {...getTableBodyProps()}>
66
- {rows.map(
67
- (row, i) =>
68
- prepareRow(row) || (
69
- <div {...row.getRowProps()} className="row">
70
- {row.cells.map(cell => {
71
- return (
72
- <div {...cell.getCellProps()} className="cell">
73
- {cell.render('Cell')}
74
- </div>
75
- )
76
- })}
77
- </div>
78
- )
79
- )}
80
- </div>
81
- </div>
82
- )
83
- }
84
-
85
- function App() {
86
- const columns = React.useMemo(
87
- () => [
88
- {
89
- Header: 'Name',
90
- columns: [
91
- {
92
- Header: 'First Name',
93
- accessor: 'firstName',
94
- width: 250,
95
- },
96
- {
97
- Header: 'Last Name',
98
- accessor: 'lastName',
99
- width: 350,
100
- },
101
- ],
102
- },
103
- {
104
- Header: 'Info',
105
- columns: [
106
- {
107
- Header: 'Age',
108
- accessor: 'age',
109
- minWidth: 300,
110
- },
111
- {
112
- Header: 'Visits',
113
- accessor: 'visits',
114
- maxWidth: 150,
115
- },
116
- {
117
- Header: 'Status',
118
- accessor: 'status',
119
- },
120
- {
121
- Header: 'Profile Progress',
122
- accessor: 'progress',
123
- },
124
- ],
125
- },
126
- ],
127
- []
128
- )
129
-
130
- return <Table columns={columns} data={data} />
131
- }
132
-
133
- test('renders a table', () => {
134
- const rtl = render(<App />)
135
-
136
- expect(
137
- rtl
138
- .getAllByRole('columnheader')
139
- .every(d => d.style.display === 'inline-block')
140
- ).toBe(true)
141
-
142
- expect(rtl.getAllByRole('row').every(d => d.style.display === 'flex')).toBe(
143
- true
144
- )
145
-
146
- expect(
147
- rtl.getAllByRole('columnheader').map(d => d.style.width)
148
- ).toStrictEqual([
149
- '550px',
150
- '850px',
151
- '250px',
152
- '300px',
153
- '300px',
154
- '150px',
155
- '200px',
156
- '200px',
157
- ])
158
- })
@@ -1,186 +0,0 @@
1
- import React from 'react'
2
- import { render, fireEvent } from '@testing-library/react'
3
- import { useTable } from '../../hooks/useTable'
4
- import { useColumnOrder } from '../useColumnOrder'
5
-
6
- const data = [
7
- {
8
- firstName: 'tanner',
9
- lastName: 'linsley',
10
- age: 29,
11
- visits: 100,
12
- status: 'In Relationship',
13
- progress: 50,
14
- },
15
- {
16
- firstName: 'derek',
17
- lastName: 'perkins',
18
- age: 40,
19
- visits: 40,
20
- status: 'Single',
21
- progress: 80,
22
- },
23
- {
24
- firstName: 'joe',
25
- lastName: 'bergevin',
26
- age: 45,
27
- visits: 20,
28
- status: 'Complicated',
29
- progress: 10,
30
- },
31
- {
32
- firstName: 'jaylen',
33
- lastName: 'linsley',
34
- age: 26,
35
- visits: 99,
36
- status: 'In Relationship',
37
- progress: 70,
38
- },
39
- ]
40
-
41
- function shuffle(arr, mapping) {
42
- if (arr.length !== mapping.length) {
43
- throw new Error()
44
- }
45
- arr = [...arr]
46
- mapping = [...mapping]
47
- const shuffled = []
48
- while (arr.length) {
49
- shuffled.push(arr.splice([mapping.shift()], 1)[0])
50
- }
51
- return shuffled
52
- }
53
-
54
- function Table({ columns, data }) {
55
- const {
56
- getTableProps,
57
- getTableBodyProps,
58
- headerGroups,
59
- rows,
60
- leafColumns,
61
- prepareRow,
62
- setColumnOrder,
63
- state,
64
- } = useTable(
65
- {
66
- columns,
67
- data,
68
- },
69
- useColumnOrder
70
- )
71
-
72
- const testColumnOrder = () => {
73
- setColumnOrder(
74
- shuffle(
75
- leafColumns.map(d => d.id),
76
- [1, 4, 2, 0, 3, 5]
77
- )
78
- )
79
- }
80
-
81
- return (
82
- <>
83
- <button onClick={() => testColumnOrder({})}>Randomize Columns</button>
84
- <table {...getTableProps()}>
85
- <thead>
86
- {headerGroups.map((headerGroup, i) => (
87
- <tr {...headerGroup.getHeaderGroupProps()}>
88
- {headerGroup.headers.map(column => (
89
- <th {...column.getHeaderProps()}>{column.render('Header')}</th>
90
- ))}
91
- </tr>
92
- ))}
93
- </thead>
94
- <tbody {...getTableBodyProps()}>
95
- {rows.slice(0, 10).map((row, i) => {
96
- prepareRow(row)
97
- return (
98
- <tr {...row.getRowProps()}>
99
- {row.cells.map((cell, i) => {
100
- return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
101
- })}
102
- </tr>
103
- )
104
- })}
105
- </tbody>
106
- </table>
107
- <pre>
108
- <code>{JSON.stringify(state, null, 2)}</code>
109
- </pre>
110
- </>
111
- )
112
- }
113
-
114
- function App() {
115
- const columns = React.useMemo(
116
- () => [
117
- {
118
- Header: 'Name',
119
- columns: [
120
- {
121
- Header: 'First Name',
122
- accessor: 'firstName',
123
- },
124
- {
125
- Header: 'Last Name',
126
- accessor: 'lastName',
127
- },
128
- ],
129
- },
130
- {
131
- Header: 'Info',
132
- columns: [
133
- {
134
- Header: 'Age',
135
- accessor: 'age',
136
- },
137
- {
138
- Header: 'Visits',
139
- accessor: 'visits',
140
- },
141
- {
142
- Header: 'Status',
143
- accessor: 'status',
144
- },
145
- {
146
- Header: 'Profile Progress',
147
- accessor: 'progress',
148
- },
149
- ],
150
- },
151
- ],
152
- []
153
- )
154
-
155
- return <Table columns={columns} data={data} />
156
- }
157
-
158
- test('renders a column-orderable table', () => {
159
- const rtl = render(<App />)
160
-
161
- expect(rtl.getAllByRole('columnheader').map(d => d.textContent)).toEqual([
162
- 'Name',
163
- 'Info',
164
- 'First Name',
165
- 'Last Name',
166
- 'Age',
167
- 'Visits',
168
- 'Status',
169
- 'Profile Progress',
170
- ])
171
-
172
- fireEvent.click(rtl.getByText('Randomize Columns'))
173
-
174
- expect(rtl.getAllByRole('columnheader').map(d => d.textContent)).toEqual([
175
- 'Name',
176
- 'Info',
177
- 'Name',
178
- 'Info',
179
- 'Last Name',
180
- 'Profile Progress',
181
- 'Visits',
182
- 'First Name',
183
- 'Age',
184
- 'Status',
185
- ])
186
- })
@@ -1,125 +0,0 @@
1
- import React from 'react'
2
- import { render, fireEvent } from '@testing-library/react'
3
- import { useTable } from '../../hooks/useTable'
4
- import { useExpanded } from '../useExpanded'
5
- import makeTestData from '../../../test-utils/makeTestData'
6
-
7
- const data = makeTestData(3, 3, 3)
8
-
9
- function Table({ columns: userColumns, data, SubComponent }) {
10
- const {
11
- getTableProps,
12
- getTableBodyProps,
13
- headerGroups,
14
- rows,
15
- prepareRow,
16
- leafColumns,
17
- } = useTable(
18
- {
19
- columns: userColumns,
20
- data,
21
- },
22
- useExpanded
23
- )
24
-
25
- return (
26
- <>
27
- <table {...getTableProps()}>
28
- <thead>
29
- {headerGroups.map(headerGroup => (
30
- <tr {...headerGroup.getHeaderGroupProps()}>
31
- {headerGroup.headers.map(column => (
32
- <th {...column.getHeaderProps()}>{column.render('Header')}</th>
33
- ))}
34
- </tr>
35
- ))}
36
- </thead>
37
- <tbody {...getTableBodyProps()}>
38
- {rows.map((row, i) => {
39
- prepareRow(row)
40
- const { key, ...rowProps } = row.getRowProps()
41
- return (
42
- <React.Fragment key={key}>
43
- <tr {...rowProps}>
44
- {row.cells.map(cell => {
45
- return (
46
- <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
47
- )
48
- })}
49
- </tr>
50
- {!row.subRows.length && row.isExpanded ? (
51
- <tr>
52
- <td colSpan={leafColumns.length}>
53
- {SubComponent({ row })}
54
- </td>
55
- </tr>
56
- ) : null}
57
- </React.Fragment>
58
- )
59
- })}
60
- </tbody>
61
- </table>
62
- </>
63
- )
64
- }
65
-
66
- function App() {
67
- const columns = React.useMemo(
68
- () => [
69
- {
70
- Header: () => null,
71
- id: 'expander',
72
- Cell: ({ row }) => (
73
- <span
74
- style={{
75
- cursor: 'pointer',
76
- paddingLeft: `${row.depth * 2}rem`,
77
- }}
78
- onClick={() => row.toggleRowExpanded()}
79
- >
80
- {row.isExpanded ? 'Collapse' : 'Expand'} Row {row.id}
81
- </span>
82
- ),
83
- },
84
- {
85
- Header: 'First Name',
86
- accessor: 'name',
87
- Cell: ({ row: { id } }) => `Row ${id}`,
88
- },
89
- ],
90
- []
91
- )
92
-
93
- return (
94
- <Table
95
- columns={columns}
96
- data={data}
97
- SubComponent={({ row }) => <span>SubComponent: {row.id}</span>}
98
- />
99
- )
100
- }
101
-
102
- test('renders an expandable table', () => {
103
- const rtl = render(<App />)
104
-
105
- rtl.getByText('Row 0')
106
-
107
- fireEvent.click(rtl.getByText('Expand Row 0'))
108
-
109
- rtl.getByText('Row 0.0')
110
- rtl.getByText('Row 0.1')
111
- rtl.getByText('Row 0.2')
112
-
113
- fireEvent.click(rtl.getByText('Expand Row 0.1'))
114
-
115
- rtl.getByText('Row 0.1.2')
116
-
117
- fireEvent.click(rtl.getByText('Expand Row 0.1.2'))
118
-
119
- rtl.getByText('SubComponent: 0.1.2')
120
-
121
- fireEvent.click(rtl.getByText('Collapse Row 0'))
122
-
123
- expect(rtl.queryByText('SubComponent: 0.1.2')).toBe(null)
124
- rtl.getByText('Expand Row 0')
125
- })