Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/datatable-initial-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

DataTable: Sort the rows on the first render when `initialSortColumn` or `initialSortDirection` is set. Before, only the header showed the sort direction and the rows stayed in the order of `data` until it changed or a header was clicked.
2 changes: 1 addition & 1 deletion packages/react/src/DataTable/DataTable.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
"name": "initialSortColumn",
"type": "string | number",
"required": false,
"description": "Provide the id or field of the column by which the table is sorted. When\nusing this `prop`, the input data must be sorted by this column in\nascending order",
"description": "Provide the id or field of the column by which the table is sorted. The\nrows are sorted by this column on the first render, so the input data\ndoes not need to be sorted beforehand. With `externalSorting`, the table\ndoes not sort the rows, and the input data must arrive in the desired order",
"defaultValue": ""
},
{
Expand Down
7 changes: 4 additions & 3 deletions packages/react/src/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ type DataTableBaseProps<Data extends UniqueRow> = {
columns: Array<Column<Data>>

/**
* Provide the id or field of the column by which the table is sorted. When
* using this `prop`, the input data must be sorted by this column in
* ascending order
* Provide the id or field of the column by which the table is sorted. The
* rows are sorted by this column on the first render, so the input data
* does not need to be sorted beforehand. With `externalSorting`, the table
* does not sort the rows, and the input data must arrive in the desired order
*/
initialSortColumn?: ObjectPaths<Data> | string | number

Expand Down
157 changes: 155 additions & 2 deletions packages/react/src/DataTable/__tests__/DataTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,165 @@ describe('DataTable', () => {
return cells
})
expect(rows).toEqual([
['a', 'c'],
['b', 'b'],
['c', 'a'],
['b', 'b'],
['a', 'c'],
])
})

it('should sort the rows on the first render using `initialSortColumn`', async () => {
render(
<DataTable
data={[
{
id: 1,
value: 3,
},
{
id: 2,
value: 1,
},
{
id: 3,
value: 2,
},
]}
columns={[
{
header: 'Value',
field: 'value',
sortBy: true,
},
]}
initialSortColumn="value"
/>,
)

const rows = page
.getByRole('cell')
.elements()
.map(cell => cell.textContent)
expect(rows).toEqual(['1', '2', '3'])
})

it('should sort the rows on the first render when `initialSortColumn` is the field of a column with an id', async () => {
render(
<DataTable
data={[
{
id: 1,
value: 3,
},
{
id: 2,
value: 1,
},
{
id: 3,
value: 2,
},
]}
columns={[
{
id: 'val',
header: 'Value',
field: 'value',
sortBy: true,
},
]}
initialSortColumn="value"
/>,
)

const header = page.getByRole('columnheader', {
name: 'Value',
})
await expect.element(header).toHaveAttribute('aria-sort', 'ascending')

const rows = page
.getByRole('cell')
.elements()
.map(cell => cell.textContent)
expect(rows).toEqual(['1', '2', '3'])
})

it('should sort the rows on the first render using `initialSortDirection` and the column strategy', async () => {
render(
<DataTable
data={[
{
id: 1,
value: 'z2',
},
{
id: 2,
value: 'z11',
},
{
id: 3,
value: 'z3',
},
]}
columns={[
{
header: 'Value',
field: 'value',
sortBy: 'alphanumeric',
},
]}
initialSortColumn="value"
initialSortDirection="DESC"
/>,
)

const header = page.getByRole('columnheader', {
name: 'Value',
})
await expect.element(header).toHaveAttribute('aria-sort', 'descending')

const rows = page
.getByRole('cell')
.elements()
.map(cell => cell.textContent)
expect(rows).toEqual(['z11', 'z3', 'z2'])
})

it('should keep the given row order when `externalSorting` is set', async () => {
render(
<DataTable
data={[
{
id: 1,
value: 3,
},
{
id: 2,
value: 1,
},
{
id: 3,
value: 2,
},
]}
columns={[
{
header: 'Value',
field: 'value',
sortBy: true,
},
]}
initialSortColumn="value"
externalSorting
/>,
)

const rows = page
.getByRole('cell')
.elements()
.map(cell => cell.textContent)
expect(rows).toEqual(['3', '1', '2'])
})

it('should not set a default sort state if `initialSortDirection` is provided but no columns are sortable', async () => {
const spy = vi.spyOn(console, 'warn').mockImplementationOnce(() => {})
render(
Expand Down
Loading
Loading