Skip to content

Commit b2d0aa6

Browse files
fix(useSortBy): fix the bug that causes flatRows to be incorrect when sorting and using subRows (#2443)
Co-authored-by: Aaron Corley <aaron.corley@sirsidynix.com> Co-authored-by: Tanner Linsley <tannerlinsley@gmail.com>
1 parent a4225c3 commit b2d0aa6

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

src/plugin-hooks/tests/useSortBy.test.js

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,32 @@ const data = [
2828
status: 'Complicated',
2929
progress: 10,
3030
},
31+
{
32+
firstName: 'john',
33+
lastName: 'buggyman',
34+
age: 52,
35+
visits: 24,
36+
status: 'Married',
37+
progress: 17,
38+
subRows: [
39+
{
40+
firstName: 'winston',
41+
lastName: 'buggyman',
42+
age: 18,
43+
visits: 200,
44+
status: 'Single',
45+
progress: 10,
46+
},
47+
],
48+
},
3149
]
3250

3351
const defaultColumn = {
3452
Cell: ({ value, column: { id } }) => `${id}: ${value}`,
3553
}
3654

37-
function Table({ columns, data }) {
38-
const {
39-
getTableProps,
40-
getTableBodyProps,
41-
headerGroups,
42-
rows,
43-
prepareRow,
44-
} = useTable(
55+
function Table({ columns, data, useTableRef }) {
56+
const instance = useTable(
4557
{
4658
columns,
4759
data,
@@ -50,6 +62,18 @@ function Table({ columns, data }) {
5062
useSortBy
5163
)
5264

65+
const {
66+
getTableProps,
67+
getTableBodyProps,
68+
headerGroups,
69+
rows,
70+
prepareRow,
71+
} = instance
72+
73+
if (useTableRef) {
74+
useTableRef.current = instance
75+
}
76+
5377
return (
5478
<table {...getTableProps()}>
5579
<thead>
@@ -85,7 +109,7 @@ function Table({ columns, data }) {
85109
)
86110
}
87111

88-
function App() {
112+
function App({ useTableRef }) {
89113
const columns = React.useMemo(
90114
() => [
91115
{
@@ -126,7 +150,7 @@ function App() {
126150
[]
127151
)
128152

129-
return <Table columns={columns} data={data} />
153+
return <Table columns={columns} data={data} useTableRef={useTableRef} />
130154
}
131155

132156
test('renders a sortable table', () => {
@@ -139,7 +163,12 @@ test('renders a sortable table', () => {
139163
.queryAllByRole('row')
140164
.slice(2)
141165
.map(d => d.children[0].textContent)
142-
).toEqual(['firstName: derek', 'firstName: joe', 'firstName: tanner'])
166+
).toEqual([
167+
'firstName: derek',
168+
'firstName: joe',
169+
'firstName: john',
170+
'firstName: tanner',
171+
])
143172

144173
fireEvent.click(rendered.getByText('First Name 🔼0'))
145174
rendered.getByText('First Name 🔽0')
@@ -148,7 +177,12 @@ test('renders a sortable table', () => {
148177
.queryAllByRole('row')
149178
.slice(2)
150179
.map(d => d.children[0].textContent)
151-
).toEqual(['firstName: tanner', 'firstName: joe', 'firstName: derek'])
180+
).toEqual([
181+
'firstName: tanner',
182+
'firstName: john',
183+
'firstName: joe',
184+
'firstName: derek',
185+
])
152186

153187
fireEvent.click(rendered.getByText('Profile Progress'))
154188
rendered.getByText('Profile Progress 🔼0')
@@ -157,7 +191,12 @@ test('renders a sortable table', () => {
157191
.queryAllByRole('row')
158192
.slice(2)
159193
.map(d => d.children[0].textContent)
160-
).toEqual(['firstName: joe', 'firstName: tanner', 'firstName: derek'])
194+
).toEqual([
195+
'firstName: joe',
196+
'firstName: john',
197+
'firstName: tanner',
198+
'firstName: derek',
199+
])
161200

162201
fireEvent.click(rendered.getByText('First Name'), { shiftKey: true })
163202
rendered.getByText('Profile Progress 🔼0')
@@ -167,5 +206,22 @@ test('renders a sortable table', () => {
167206
.queryAllByRole('row')
168207
.slice(2)
169208
.map(d => d.children[0].textContent)
170-
).toEqual(['firstName: joe', 'firstName: derek', 'firstName: tanner'])
209+
).toEqual([
210+
'firstName: joe',
211+
'firstName: john',
212+
'firstName: derek',
213+
'firstName: tanner',
214+
])
215+
})
216+
217+
test('maintains the integrity of instance.flatRows', () => {
218+
const useTableRef = { current: null }
219+
const rendered = render(<App useTableRef={useTableRef} />)
220+
221+
fireEvent.click(rendered.getByText('First Name'))
222+
const flatRows = useTableRef.current.flatRows
223+
expect(flatRows.length).toBe(5)
224+
expect(
225+
flatRows.find(r => r.values.firstName === 'winston')
226+
).not.toBeUndefined()
171227
})

src/plugin-hooks/useSortBy.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@ function useInstance(instance) {
321321
// If there are sub-rows, sort them
322322
sortedData.forEach(row => {
323323
sortedFlatRows.push(row)
324-
if (!row.subRows || row.subRows.length < 1) {
324+
if (!row.subRows) {
325+
return
326+
} else if (row.subRows.length === 1) {
327+
sortedFlatRows.push(row.subRows[0])
325328
return
326329
}
327330
row.subRows = sortData(row.subRows)

0 commit comments

Comments
 (0)