Skip to content

Commit

Permalink
fix(useSortBy): fix the bug that causes flatRows to be incorrect when…
Browse files Browse the repository at this point in the history
… sorting and using subRows (#2443)

Co-authored-by: Aaron Corley <aaron.corley@sirsidynix.com>
Co-authored-by: Tanner Linsley <tannerlinsley@gmail.com>
  • Loading branch information
3 people committed Aug 3, 2020
1 parent a4225c3 commit b2d0aa6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
84 changes: 70 additions & 14 deletions src/plugin-hooks/tests/useSortBy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,32 @@ const data = [
status: 'Complicated',
progress: 10,
},
{
firstName: 'john',
lastName: 'buggyman',
age: 52,
visits: 24,
status: 'Married',
progress: 17,
subRows: [
{
firstName: 'winston',
lastName: 'buggyman',
age: 18,
visits: 200,
status: 'Single',
progress: 10,
},
],
},
]

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

function Table({ columns, data }) {
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable(
function Table({ columns, data, useTableRef }) {
const instance = useTable(
{
columns,
data,
Expand All @@ -50,6 +62,18 @@ function Table({ columns, data }) {
useSortBy
)

const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = instance

if (useTableRef) {
useTableRef.current = instance
}

return (
<table {...getTableProps()}>
<thead>
Expand Down Expand Up @@ -85,7 +109,7 @@ function Table({ columns, data }) {
)
}

function App() {
function App({ useTableRef }) {
const columns = React.useMemo(
() => [
{
Expand Down Expand Up @@ -126,7 +150,7 @@ function App() {
[]
)

return <Table columns={columns} data={data} />
return <Table columns={columns} data={data} useTableRef={useTableRef} />
}

test('renders a sortable table', () => {
Expand All @@ -139,7 +163,12 @@ test('renders a sortable table', () => {
.queryAllByRole('row')
.slice(2)
.map(d => d.children[0].textContent)
).toEqual(['firstName: derek', 'firstName: joe', 'firstName: tanner'])
).toEqual([
'firstName: derek',
'firstName: joe',
'firstName: john',
'firstName: tanner',
])

fireEvent.click(rendered.getByText('First Name 🔼0'))
rendered.getByText('First Name 🔽0')
Expand All @@ -148,7 +177,12 @@ test('renders a sortable table', () => {
.queryAllByRole('row')
.slice(2)
.map(d => d.children[0].textContent)
).toEqual(['firstName: tanner', 'firstName: joe', 'firstName: derek'])
).toEqual([
'firstName: tanner',
'firstName: john',
'firstName: joe',
'firstName: derek',
])

fireEvent.click(rendered.getByText('Profile Progress'))
rendered.getByText('Profile Progress 🔼0')
Expand All @@ -157,7 +191,12 @@ test('renders a sortable table', () => {
.queryAllByRole('row')
.slice(2)
.map(d => d.children[0].textContent)
).toEqual(['firstName: joe', 'firstName: tanner', 'firstName: derek'])
).toEqual([
'firstName: joe',
'firstName: john',
'firstName: tanner',
'firstName: derek',
])

fireEvent.click(rendered.getByText('First Name'), { shiftKey: true })
rendered.getByText('Profile Progress 🔼0')
Expand All @@ -167,5 +206,22 @@ test('renders a sortable table', () => {
.queryAllByRole('row')
.slice(2)
.map(d => d.children[0].textContent)
).toEqual(['firstName: joe', 'firstName: derek', 'firstName: tanner'])
).toEqual([
'firstName: joe',
'firstName: john',
'firstName: derek',
'firstName: tanner',
])
})

test('maintains the integrity of instance.flatRows', () => {
const useTableRef = { current: null }
const rendered = render(<App useTableRef={useTableRef} />)

fireEvent.click(rendered.getByText('First Name'))
const flatRows = useTableRef.current.flatRows
expect(flatRows.length).toBe(5)
expect(
flatRows.find(r => r.values.firstName === 'winston')
).not.toBeUndefined()
})
5 changes: 4 additions & 1 deletion src/plugin-hooks/useSortBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ function useInstance(instance) {
// If there are sub-rows, sort them
sortedData.forEach(row => {
sortedFlatRows.push(row)
if (!row.subRows || row.subRows.length < 1) {
if (!row.subRows) {
return
} else if (row.subRows.length === 1) {
sortedFlatRows.push(row.subRows[0])
return
}
row.subRows = sortData(row.subRows)
Expand Down

1 comment on commit b2d0aa6

@vercel
Copy link

@vercel vercel bot commented on b2d0aa6 Aug 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.