@tanstack/react-table 0.0.1-alpha.0
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.
- package/dist/react-table.development.js +3406 -0
- package/dist/react-table.development.js.map +1 -0
- package/dist/react-table.production.min.js +2 -0
- package/dist/react-table.production.min.js.map +1 -0
- package/lib/index.js +65 -0
- package/package.json +122 -0
- package/src/.DS_Store +0 -0
- package/src/aggregationTypes.ts +115 -0
- package/src/core.tsx +1194 -0
- package/src/createTable.tsx +181 -0
- package/src/features/Expanding.ts +388 -0
- package/src/features/Filters.ts +707 -0
- package/src/features/Grouping.ts +451 -0
- package/src/features/Headers.ts +907 -0
- package/src/features/Ordering.ts +134 -0
- package/src/features/Pinning.ts +213 -0
- package/src/features/Sorting.ts +487 -0
- package/src/features/Visibility.ts +281 -0
- package/src/features/notest/useAbsoluteLayout.test.js +152 -0
- package/src/features/notest/useBlockLayout.test.js +158 -0
- package/src/features/notest/useColumnOrder.test.js +186 -0
- package/src/features/notest/useExpanded.test.js +125 -0
- package/src/features/notest/useFilters.test.js +393 -0
- package/src/features/notest/useFiltersAndRowSelect.test.js +256 -0
- package/src/features/notest/useFlexLayout.test.js +152 -0
- package/src/features/notest/useGroupBy.test.js +259 -0
- package/src/features/notest/usePagination.test.js +231 -0
- package/src/features/notest/useResizeColumns.test.js +229 -0
- package/src/features/notest/useRowSelect.test.js +250 -0
- package/src/features/notest/useRowState.test.js +178 -0
- package/src/features/tests/Visibility.test.tsx +225 -0
- package/src/features/tests/__snapshots__/Visibility.test.tsx.snap +390 -0
- package/src/features/tests/withSorting.notest.tsx +341 -0
- package/src/features/withColumnResizing.ts +281 -0
- package/src/features/withPagination.ts +208 -0
- package/src/features/withRowSelection.ts +467 -0
- package/src/filterTypes.ts +251 -0
- package/src/index.tsx +7 -0
- package/src/sortTypes.ts +159 -0
- package/src/test-utils/makeTestData.ts +41 -0
- package/src/tests/__snapshots__/core.test.tsx.snap +148 -0
- package/src/tests/core.test.tsx +241 -0
- package/src/types.ts +285 -0
- package/src/utils/columnFilterRowsFn.ts +162 -0
- package/src/utils/expandRowsFn.ts +53 -0
- package/src/utils/globalFilterRowsFn.ts +129 -0
- package/src/utils/groupRowsFn.ts +196 -0
- package/src/utils/sortRowsFn.ts +115 -0
- package/src/utils.tsx +243 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { renderHook, act } from '@testing-library/react-hooks'
|
|
3
|
+
import { render, fireEvent } from '../../../test-utils/react-testing'
|
|
4
|
+
import { useTable } from '../../hooks/useTable'
|
|
5
|
+
import { usePagination } from '../usePagination'
|
|
6
|
+
import { useFilters } from '../useFilters'
|
|
7
|
+
|
|
8
|
+
const data = [...new Array(1000)].fill(null).map((d, i) => ({
|
|
9
|
+
firstName: `tanner ${i + 1}`,
|
|
10
|
+
lastName: 'linsley',
|
|
11
|
+
age: 29,
|
|
12
|
+
visits: 100,
|
|
13
|
+
status: 'In Relationship',
|
|
14
|
+
progress: 50,
|
|
15
|
+
}))
|
|
16
|
+
|
|
17
|
+
const columns = [
|
|
18
|
+
{
|
|
19
|
+
Header: 'Name',
|
|
20
|
+
columns: [
|
|
21
|
+
{
|
|
22
|
+
Header: 'First Name',
|
|
23
|
+
accessor: 'firstName',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
Header: 'Last Name',
|
|
27
|
+
accessor: 'lastName',
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
Header: 'Info',
|
|
33
|
+
columns: [
|
|
34
|
+
{
|
|
35
|
+
Header: 'Age',
|
|
36
|
+
accessor: 'age',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
Header: 'Visits',
|
|
40
|
+
accessor: 'visits',
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
Header: 'Status',
|
|
44
|
+
accessor: 'status',
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
Header: 'Profile Progress',
|
|
48
|
+
accessor: 'progress',
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
function Table({ columns, data }) {
|
|
55
|
+
const {
|
|
56
|
+
getTableProps,
|
|
57
|
+
getTableBodyProps,
|
|
58
|
+
headerGroups,
|
|
59
|
+
prepareRow,
|
|
60
|
+
page,
|
|
61
|
+
canPreviousPage,
|
|
62
|
+
canNextPage,
|
|
63
|
+
pageOptions,
|
|
64
|
+
pageCount,
|
|
65
|
+
gotoPage,
|
|
66
|
+
nextPage,
|
|
67
|
+
previousPage,
|
|
68
|
+
setPageSize,
|
|
69
|
+
state: { pageIndex, pageSize },
|
|
70
|
+
} = useTable(
|
|
71
|
+
{
|
|
72
|
+
columns,
|
|
73
|
+
data,
|
|
74
|
+
initialState: { pageIndex: 2 },
|
|
75
|
+
},
|
|
76
|
+
usePagination
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<>
|
|
81
|
+
<table {...getTableProps()}>
|
|
82
|
+
<thead>
|
|
83
|
+
{headerGroups.map(headerGroup => (
|
|
84
|
+
<tr {...headerGroup.getHeaderGroupProps()}>
|
|
85
|
+
{headerGroup.headers.map(column => (
|
|
86
|
+
<th {...column.getHeaderProps()}>{column.render('Header')}</th>
|
|
87
|
+
))}
|
|
88
|
+
</tr>
|
|
89
|
+
))}
|
|
90
|
+
</thead>
|
|
91
|
+
<tbody {...getTableBodyProps()}>
|
|
92
|
+
{page.map(
|
|
93
|
+
(row, i) =>
|
|
94
|
+
prepareRow(row) || (
|
|
95
|
+
<tr {...row.getRowProps()}>
|
|
96
|
+
{row.cells.map(cell => {
|
|
97
|
+
return (
|
|
98
|
+
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
|
|
99
|
+
)
|
|
100
|
+
})}
|
|
101
|
+
</tr>
|
|
102
|
+
)
|
|
103
|
+
)}
|
|
104
|
+
</tbody>
|
|
105
|
+
</table>
|
|
106
|
+
<div className="pagination">
|
|
107
|
+
<button onClick={() => gotoPage(0)} disabled={!canPreviousPage}>
|
|
108
|
+
{'<<'}
|
|
109
|
+
</button>{' '}
|
|
110
|
+
<button onClick={() => previousPage()} disabled={!canPreviousPage}>
|
|
111
|
+
{'<'}
|
|
112
|
+
</button>{' '}
|
|
113
|
+
<button onClick={() => nextPage()} disabled={!canNextPage}>
|
|
114
|
+
{'>'}
|
|
115
|
+
</button>{' '}
|
|
116
|
+
<button onClick={() => gotoPage(pageCount - 1)} disabled={!canNextPage}>
|
|
117
|
+
{'>>'}
|
|
118
|
+
</button>{' '}
|
|
119
|
+
<span>
|
|
120
|
+
Page{' '}
|
|
121
|
+
<strong>
|
|
122
|
+
{pageIndex + 1} of {pageOptions.length}
|
|
123
|
+
</strong>{' '}
|
|
124
|
+
</span>
|
|
125
|
+
<span>
|
|
126
|
+
| Go to page:{' '}
|
|
127
|
+
<input
|
|
128
|
+
type="number"
|
|
129
|
+
defaultValue={pageIndex + 1}
|
|
130
|
+
onChange={e => {
|
|
131
|
+
const page = e.target.value ? Number(e.target.value) - 1 : 0
|
|
132
|
+
gotoPage(page)
|
|
133
|
+
}}
|
|
134
|
+
style={{ width: '100px' }}
|
|
135
|
+
/>
|
|
136
|
+
</span>{' '}
|
|
137
|
+
<select
|
|
138
|
+
value={pageSize}
|
|
139
|
+
onChange={e => {
|
|
140
|
+
setPageSize(Number(e.target.value))
|
|
141
|
+
}}
|
|
142
|
+
data-testid="page-size-select"
|
|
143
|
+
>
|
|
144
|
+
{[10, 20, 30, 40, 50].map(pageSize => (
|
|
145
|
+
<option key={pageSize} value={pageSize}>
|
|
146
|
+
Show {pageSize}
|
|
147
|
+
</option>
|
|
148
|
+
))}
|
|
149
|
+
</select>
|
|
150
|
+
</div>
|
|
151
|
+
</>
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function App() {
|
|
156
|
+
return <Table columns={columns} data={data} />
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
test('renders a paginated table', () => {
|
|
160
|
+
const rendered = render(<App />)
|
|
161
|
+
|
|
162
|
+
expect(rendered.queryAllByRole('cell')[0].textContent).toEqual('tanner 21')
|
|
163
|
+
|
|
164
|
+
fireEvent.click(rendered.getByText('>'))
|
|
165
|
+
expect(rendered.queryAllByRole('cell')[0].textContent).toEqual('tanner 31')
|
|
166
|
+
|
|
167
|
+
fireEvent.click(rendered.getByText('>'))
|
|
168
|
+
expect(rendered.queryAllByRole('cell')[0].textContent).toEqual('tanner 41')
|
|
169
|
+
|
|
170
|
+
fireEvent.click(rendered.getByText('>>'))
|
|
171
|
+
expect(rendered.queryAllByRole('cell')[0].textContent).toEqual('tanner 991')
|
|
172
|
+
|
|
173
|
+
fireEvent.click(rendered.getByText('<<'))
|
|
174
|
+
expect(rendered.queryAllByRole('cell')[0].textContent).toEqual('tanner 1')
|
|
175
|
+
|
|
176
|
+
fireEvent.change(rendered.getByTestId('page-size-select'), {
|
|
177
|
+
target: { value: 30 },
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
expect(
|
|
181
|
+
rendered.queryAllByRole('row').slice(2).reverse()[0].children[0].textContent
|
|
182
|
+
).toEqual('tanner 30')
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
describe('usePagination', () => {
|
|
186
|
+
test('renders a paginated table', () => {
|
|
187
|
+
const rendered = render(<App />)
|
|
188
|
+
|
|
189
|
+
expect(rendered.queryAllByRole('cell')[0].textContent).toEqual('tanner 21')
|
|
190
|
+
|
|
191
|
+
fireEvent.click(rendered.getByText('>'))
|
|
192
|
+
expect(rendered.queryAllByRole('cell')[0].textContent).toEqual('tanner 31')
|
|
193
|
+
|
|
194
|
+
fireEvent.click(rendered.getByText('>'))
|
|
195
|
+
expect(rendered.queryAllByRole('cell')[0].textContent).toEqual('tanner 41')
|
|
196
|
+
|
|
197
|
+
fireEvent.click(rendered.getByText('>>'))
|
|
198
|
+
expect(rendered.queryAllByRole('cell')[0].textContent).toEqual('tanner 991')
|
|
199
|
+
|
|
200
|
+
fireEvent.click(rendered.getByText('<<'))
|
|
201
|
+
expect(rendered.queryAllByRole('cell')[0].textContent).toEqual('tanner 1')
|
|
202
|
+
|
|
203
|
+
fireEvent.change(rendered.getByTestId('page-size-select'), {
|
|
204
|
+
target: { value: 30 },
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
expect(
|
|
208
|
+
rendered.queryAllByRole('row').slice(2).reverse()[0].children[0]
|
|
209
|
+
.textContent
|
|
210
|
+
).toEqual('tanner 30')
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
test('changing columnFilters resets pagination', async () => {
|
|
214
|
+
const { result } = renderHook(() =>
|
|
215
|
+
useTable(
|
|
216
|
+
{
|
|
217
|
+
columns,
|
|
218
|
+
data,
|
|
219
|
+
},
|
|
220
|
+
useFilters,
|
|
221
|
+
usePagination
|
|
222
|
+
)
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
act(() => result.current.nextPage())
|
|
226
|
+
act(() => result.current.nextPage())
|
|
227
|
+
expect(result.current.state.pageIndex).toEqual(2)
|
|
228
|
+
act(() => result.current.leafColumns[0].setFilter('tanner'))
|
|
229
|
+
expect(result.current.state.pageIndex).toEqual(0)
|
|
230
|
+
})
|
|
231
|
+
})
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { render, fireEvent } from '@testing-library/react'
|
|
3
|
+
import { useTable } from '../../hooks/useTable'
|
|
4
|
+
import { useBlockLayout } from '../useBlockLayout'
|
|
5
|
+
import { useResizeColumns } from '../useResizeColumns'
|
|
6
|
+
|
|
7
|
+
const data = [
|
|
8
|
+
{
|
|
9
|
+
firstName: 'tanner',
|
|
10
|
+
lastName: 'linsley',
|
|
11
|
+
age: 29,
|
|
12
|
+
visits: 100,
|
|
13
|
+
status: 'In Relationship',
|
|
14
|
+
progress: 50,
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
firstName: 'derek',
|
|
18
|
+
lastName: 'perkins',
|
|
19
|
+
age: 40,
|
|
20
|
+
visits: 40,
|
|
21
|
+
status: 'Single',
|
|
22
|
+
progress: 80,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
firstName: 'joe',
|
|
26
|
+
lastName: 'bergevin',
|
|
27
|
+
age: 45,
|
|
28
|
+
visits: 20,
|
|
29
|
+
status: 'Complicated',
|
|
30
|
+
progress: 10,
|
|
31
|
+
},
|
|
32
|
+
]
|
|
33
|
+
function Table({ columns, data }) {
|
|
34
|
+
const defaultColumn = React.useMemo(
|
|
35
|
+
() => ({
|
|
36
|
+
minWidth: 30,
|
|
37
|
+
width: 150,
|
|
38
|
+
maxWidth: 400,
|
|
39
|
+
}),
|
|
40
|
+
[]
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
|
|
44
|
+
useTable(
|
|
45
|
+
{
|
|
46
|
+
columns,
|
|
47
|
+
data,
|
|
48
|
+
defaultColumn,
|
|
49
|
+
},
|
|
50
|
+
useBlockLayout,
|
|
51
|
+
useResizeColumns
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<div {...getTableProps()} className="table">
|
|
56
|
+
<div>
|
|
57
|
+
{headerGroups.map(headerGroup => (
|
|
58
|
+
<div {...headerGroup.getHeaderGroupProps()} className="tr">
|
|
59
|
+
{headerGroup.headers.map(column => (
|
|
60
|
+
<div {...column.getHeaderProps()} className="th">
|
|
61
|
+
{column.render('Header')}
|
|
62
|
+
{/* Use column.getResizerProps to hook up the events correctly */}
|
|
63
|
+
<div
|
|
64
|
+
{...column.getResizerProps()}
|
|
65
|
+
className={`resizer${column.isResizing ? ' isResizing' : ''}`}
|
|
66
|
+
/>
|
|
67
|
+
</div>
|
|
68
|
+
))}
|
|
69
|
+
</div>
|
|
70
|
+
))}
|
|
71
|
+
</div>
|
|
72
|
+
|
|
73
|
+
<div {...getTableBodyProps()}>
|
|
74
|
+
{rows.map((row, i) => {
|
|
75
|
+
prepareRow(row)
|
|
76
|
+
return (
|
|
77
|
+
<div {...row.getRowProps()} className="tr">
|
|
78
|
+
{row.cells.map(cell => {
|
|
79
|
+
return (
|
|
80
|
+
<div {...cell.getCellProps()} className="td">
|
|
81
|
+
{cell.render('Cell')}
|
|
82
|
+
</div>
|
|
83
|
+
)
|
|
84
|
+
})}
|
|
85
|
+
</div>
|
|
86
|
+
)
|
|
87
|
+
})}
|
|
88
|
+
</div>
|
|
89
|
+
</div>
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function App() {
|
|
94
|
+
const columns = React.useMemo(
|
|
95
|
+
() => [
|
|
96
|
+
{
|
|
97
|
+
Header: 'Name',
|
|
98
|
+
columns: [
|
|
99
|
+
{
|
|
100
|
+
Header: 'First Name',
|
|
101
|
+
accessor: 'firstName',
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
Header: 'Last Name',
|
|
105
|
+
accessor: 'lastName',
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
Header: 'Info',
|
|
111
|
+
columns: [
|
|
112
|
+
{
|
|
113
|
+
Header: 'Age',
|
|
114
|
+
accessor: 'age',
|
|
115
|
+
width: 50,
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
Header: 'Visits',
|
|
119
|
+
accessor: 'visits',
|
|
120
|
+
width: 60,
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
Header: 'Status',
|
|
124
|
+
accessor: 'status',
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
Header: 'Profile Progress',
|
|
128
|
+
accessor: 'progress',
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
},
|
|
132
|
+
],
|
|
133
|
+
[]
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
return <Table columns={columns} data={data} />
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const start = 20
|
|
140
|
+
const move = 100
|
|
141
|
+
const end = 100
|
|
142
|
+
|
|
143
|
+
const sizesBefore = [
|
|
144
|
+
'300px',
|
|
145
|
+
'410px',
|
|
146
|
+
'150px',
|
|
147
|
+
'150px',
|
|
148
|
+
'50px',
|
|
149
|
+
'60px',
|
|
150
|
+
'150px',
|
|
151
|
+
'150px',
|
|
152
|
+
]
|
|
153
|
+
|
|
154
|
+
const sizesAfter = [
|
|
155
|
+
'300px',
|
|
156
|
+
'490px',
|
|
157
|
+
'150px',
|
|
158
|
+
'150px',
|
|
159
|
+
'59.75609756097561px',
|
|
160
|
+
'71.70731707317073px',
|
|
161
|
+
'179.26829268292684px',
|
|
162
|
+
'179.26829268292684px',
|
|
163
|
+
]
|
|
164
|
+
|
|
165
|
+
test('table can be resized by a mouse', () => {
|
|
166
|
+
const rtl = render(<App />)
|
|
167
|
+
|
|
168
|
+
const infoResizer = rtl
|
|
169
|
+
.getAllByRole('separator')
|
|
170
|
+
.find(d => d.previousSibling.textContent === 'Info')
|
|
171
|
+
|
|
172
|
+
expect(rtl.getAllByRole('columnheader').map(d => d.style.width)).toEqual(
|
|
173
|
+
sizesBefore
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
fireEvent.mouseDown(infoResizer, { clientX: start })
|
|
177
|
+
fireEvent.mouseMove(infoResizer, { clientX: move })
|
|
178
|
+
fireEvent.mouseUp(infoResizer, { clientX: end })
|
|
179
|
+
|
|
180
|
+
expect(rtl.getAllByRole('columnheader').map(d => d.style.width)).toEqual(
|
|
181
|
+
sizesAfter
|
|
182
|
+
)
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
test('table can be resized by a touch device', () => {
|
|
186
|
+
const rtl = render(<App />)
|
|
187
|
+
|
|
188
|
+
const infoResizer = rtl
|
|
189
|
+
.getAllByRole('separator')
|
|
190
|
+
.find(d => d.previousSibling.textContent === 'Info')
|
|
191
|
+
|
|
192
|
+
expect(rtl.getAllByRole('columnheader').map(d => d.style.width)).toEqual(
|
|
193
|
+
sizesBefore
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
fireEvent.touchStart(infoResizer, { touches: [{ clientX: start }] })
|
|
197
|
+
fireEvent.touchMove(infoResizer, { touches: [{ clientX: move }] })
|
|
198
|
+
fireEvent.touchEnd(infoResizer, { touches: [{ clientX: end }] })
|
|
199
|
+
|
|
200
|
+
expect(rtl.getAllByRole('columnheader').map(d => d.style.width)).toEqual(
|
|
201
|
+
sizesAfter
|
|
202
|
+
)
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
test('table can not be resized with multiple touches', () => {
|
|
206
|
+
const rtl = render(<App />)
|
|
207
|
+
|
|
208
|
+
const infoResizer = rtl
|
|
209
|
+
.getAllByRole('separator')
|
|
210
|
+
.find(d => d.previousSibling.textContent === 'Info')
|
|
211
|
+
|
|
212
|
+
expect(rtl.getAllByRole('columnheader').map(d => d.style.width)).toEqual(
|
|
213
|
+
sizesBefore
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
fireEvent.touchStart(infoResizer, {
|
|
217
|
+
touches: [{ clientX: start }, { clientX: start }],
|
|
218
|
+
})
|
|
219
|
+
fireEvent.touchMove(infoResizer, {
|
|
220
|
+
touches: [{ clientX: move }, { clientX: move }],
|
|
221
|
+
})
|
|
222
|
+
fireEvent.touchEnd(infoResizer, {
|
|
223
|
+
touches: [{ clientX: end }, { clientX: end }],
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
expect(rtl.getAllByRole('columnheader').map(d => d.style.width)).toEqual(
|
|
227
|
+
sizesBefore
|
|
228
|
+
)
|
|
229
|
+
})
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { render, fireEvent } from '@testing-library/react'
|
|
3
|
+
import { useTable } from '../../hooks/useTable'
|
|
4
|
+
import { useRowSelect } from '../useRowSelect'
|
|
5
|
+
import { useExpanded } from '../useExpanded'
|
|
6
|
+
|
|
7
|
+
const dataPiece = [
|
|
8
|
+
{
|
|
9
|
+
firstName: 'tanner',
|
|
10
|
+
lastName: 'linsley',
|
|
11
|
+
age: 29,
|
|
12
|
+
visits: 100,
|
|
13
|
+
status: 'In Relationship',
|
|
14
|
+
progress: 50,
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
firstName: 'derek',
|
|
18
|
+
lastName: 'perkins',
|
|
19
|
+
age: 40,
|
|
20
|
+
visits: 40,
|
|
21
|
+
status: 'Single',
|
|
22
|
+
progress: 80,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
firstName: 'joe',
|
|
26
|
+
lastName: 'bergevin',
|
|
27
|
+
age: 45,
|
|
28
|
+
visits: 20,
|
|
29
|
+
status: 'Complicated',
|
|
30
|
+
progress: 10,
|
|
31
|
+
expanded: true,
|
|
32
|
+
subRows: [
|
|
33
|
+
{
|
|
34
|
+
firstName: 'bob',
|
|
35
|
+
lastName: 'ross',
|
|
36
|
+
age: 52,
|
|
37
|
+
visits: 40,
|
|
38
|
+
status: 'In Relationship',
|
|
39
|
+
progress: 30,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
firstName: 'john',
|
|
43
|
+
lastName: 'smith',
|
|
44
|
+
age: 21,
|
|
45
|
+
visits: 30,
|
|
46
|
+
status: 'Single',
|
|
47
|
+
progress: 60,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
firstName: 'jaylen',
|
|
53
|
+
lastName: 'linsley',
|
|
54
|
+
age: 26,
|
|
55
|
+
visits: 99,
|
|
56
|
+
status: 'In Relationship',
|
|
57
|
+
progress: 70,
|
|
58
|
+
},
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
const data = [
|
|
62
|
+
...dataPiece,
|
|
63
|
+
...dataPiece,
|
|
64
|
+
...dataPiece,
|
|
65
|
+
...dataPiece,
|
|
66
|
+
...dataPiece,
|
|
67
|
+
...dataPiece,
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
function Table({ columns, data }) {
|
|
71
|
+
// Use the state and functions returned from useTable to build your UI
|
|
72
|
+
const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
|
|
73
|
+
useTable(
|
|
74
|
+
{
|
|
75
|
+
columns,
|
|
76
|
+
data,
|
|
77
|
+
},
|
|
78
|
+
useRowSelect,
|
|
79
|
+
useExpanded,
|
|
80
|
+
hooks => {
|
|
81
|
+
hooks.leafColumns.push(columns => [
|
|
82
|
+
// Let's make a column for selection
|
|
83
|
+
{
|
|
84
|
+
id: 'selection',
|
|
85
|
+
// The header can use the table's getToggleAllRowsSelectedProps method
|
|
86
|
+
// to render a checkbox
|
|
87
|
+
Header: ({ instance: { getToggleAllRowsSelectedProps } }) => (
|
|
88
|
+
<div>
|
|
89
|
+
<label>
|
|
90
|
+
<IndeterminateCheckbox {...getToggleAllRowsSelectedProps()} />{' '}
|
|
91
|
+
Select All
|
|
92
|
+
</label>
|
|
93
|
+
</div>
|
|
94
|
+
),
|
|
95
|
+
// The cell can use the individual row's getToggleRowSelectedProps method
|
|
96
|
+
// to the render a checkbox
|
|
97
|
+
Cell: ({ row }) => (
|
|
98
|
+
<div>
|
|
99
|
+
<label>
|
|
100
|
+
<IndeterminateCheckbox {...row.getToggleRowSelectedProps()} />{' '}
|
|
101
|
+
Select Row
|
|
102
|
+
</label>
|
|
103
|
+
</div>
|
|
104
|
+
),
|
|
105
|
+
},
|
|
106
|
+
...columns,
|
|
107
|
+
])
|
|
108
|
+
}
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
// Render the UI for your table
|
|
112
|
+
return (
|
|
113
|
+
<>
|
|
114
|
+
<table {...getTableProps()}>
|
|
115
|
+
<thead>
|
|
116
|
+
{headerGroups.map(headerGroup => (
|
|
117
|
+
<tr {...headerGroup.getHeaderGroupProps()}>
|
|
118
|
+
{headerGroup.headers.map(column => (
|
|
119
|
+
<th {...column.getHeaderProps()}>{column.render('Header')}</th>
|
|
120
|
+
))}
|
|
121
|
+
</tr>
|
|
122
|
+
))}
|
|
123
|
+
</thead>
|
|
124
|
+
<tbody {...getTableBodyProps()}>
|
|
125
|
+
{rows.map(
|
|
126
|
+
(row, i) =>
|
|
127
|
+
prepareRow(row) || (
|
|
128
|
+
<tr {...row.getRowProps()}>
|
|
129
|
+
{row.cells.map(cell => {
|
|
130
|
+
return (
|
|
131
|
+
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
|
|
132
|
+
)
|
|
133
|
+
})}
|
|
134
|
+
</tr>
|
|
135
|
+
)
|
|
136
|
+
)}
|
|
137
|
+
</tbody>
|
|
138
|
+
</table>
|
|
139
|
+
</>
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const IndeterminateCheckbox = React.forwardRef(
|
|
144
|
+
({ indeterminate, ...rest }, ref) => {
|
|
145
|
+
const defaultRef = React.useRef()
|
|
146
|
+
const resolvedRef = ref || defaultRef
|
|
147
|
+
|
|
148
|
+
React.useEffect(() => {
|
|
149
|
+
resolvedRef.current.indeterminate = indeterminate
|
|
150
|
+
}, [resolvedRef, indeterminate])
|
|
151
|
+
|
|
152
|
+
return <input type="checkbox" ref={resolvedRef} {...rest} />
|
|
153
|
+
}
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
function App() {
|
|
157
|
+
const columns = React.useMemo(
|
|
158
|
+
() => [
|
|
159
|
+
{
|
|
160
|
+
id: 'selectedStatus',
|
|
161
|
+
Cell: ({ row }) =>
|
|
162
|
+
row.isSelected ? (
|
|
163
|
+
<div>
|
|
164
|
+
<div>Selected</div>
|
|
165
|
+
<div>Row {row.id}</div>
|
|
166
|
+
</div>
|
|
167
|
+
) : null,
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
Header: 'Name',
|
|
171
|
+
columns: [
|
|
172
|
+
{
|
|
173
|
+
Header: 'First Name',
|
|
174
|
+
accessor: 'firstName',
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
Header: 'Last Name',
|
|
178
|
+
accessor: 'lastName',
|
|
179
|
+
},
|
|
180
|
+
],
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
Header: 'Info',
|
|
184
|
+
columns: [
|
|
185
|
+
{
|
|
186
|
+
Header: 'Age',
|
|
187
|
+
accessor: 'age',
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
Header: 'Visits',
|
|
191
|
+
accessor: 'visits',
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
Header: 'Status',
|
|
195
|
+
accessor: 'status',
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
Header: 'Profile Progress',
|
|
199
|
+
accessor: 'progress',
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
},
|
|
203
|
+
],
|
|
204
|
+
[]
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
return <Table columns={columns} data={data} />
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
test('renders a table with selectable rows', () => {
|
|
211
|
+
const rtl = render(<App />)
|
|
212
|
+
|
|
213
|
+
fireEvent.click(rtl.getByLabelText('Select All'))
|
|
214
|
+
|
|
215
|
+
expect(rtl.getAllByText('Selected').length).toBe(24)
|
|
216
|
+
|
|
217
|
+
fireEvent.click(rtl.getAllByLabelText('Select Row')[2])
|
|
218
|
+
|
|
219
|
+
expect(rtl.queryAllByText('Selected').length).toBe(23)
|
|
220
|
+
|
|
221
|
+
fireEvent.click(rtl.getByLabelText('Select All'))
|
|
222
|
+
|
|
223
|
+
expect(rtl.queryAllByText('Selected').length).toBe(24)
|
|
224
|
+
|
|
225
|
+
fireEvent.click(rtl.getByLabelText('Select All'))
|
|
226
|
+
|
|
227
|
+
expect(rtl.queryAllByText('Selected').length).toBe(0)
|
|
228
|
+
|
|
229
|
+
fireEvent.click(rtl.getAllByLabelText('Select Row')[0])
|
|
230
|
+
fireEvent.click(rtl.getAllByLabelText('Select Row')[2])
|
|
231
|
+
|
|
232
|
+
rtl.getByText('Row 0')
|
|
233
|
+
rtl.getByText('Row 2')
|
|
234
|
+
|
|
235
|
+
fireEvent.click(rtl.getAllByLabelText('Select Row')[2])
|
|
236
|
+
|
|
237
|
+
expect(rtl.queryByText('Row 2')).toBeNull()
|
|
238
|
+
|
|
239
|
+
fireEvent.click(rtl.getAllByLabelText('Select Row')[3])
|
|
240
|
+
|
|
241
|
+
rtl.queryByText('Row 3')
|
|
242
|
+
|
|
243
|
+
fireEvent.click(rtl.getAllByLabelText('Select Row')[4])
|
|
244
|
+
|
|
245
|
+
rtl.queryByText('Row 4')
|
|
246
|
+
|
|
247
|
+
fireEvent.click(rtl.getAllByLabelText('Select Row')[4])
|
|
248
|
+
|
|
249
|
+
expect(rtl.queryByText('Row 4')).toBeNull()
|
|
250
|
+
})
|