Skip to content

Commit

Permalink
feat(DataTable): add sorting behavior (#2902)
Browse files Browse the repository at this point in the history
* feat(DataTable): add sorting behavior

* chore: ignore eslint violations for generic types

* chore: remove custom sort strategy

* chore: remove unused imports

* test(DataTable): add tests for initial sort state

* chore: address eslint violations

---------

Co-authored-by: Josh Black <joshblack@users.noreply.github.com>
  • Loading branch information
joshblack and joshblack committed Feb 22, 2023
1 parent b3786fa commit 7c83497
Show file tree
Hide file tree
Showing 9 changed files with 963 additions and 85 deletions.
78 changes: 78 additions & 0 deletions src/DataTable/DataTable.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export const Default = () => (
},
{
header: 'Dependabot',
field: 'securityFeatures.dependabot',
renderCell: row => {
return row.securityFeatures.dependabot.length > 0 ? (
<LabelGroup>
Expand All @@ -184,6 +185,7 @@ export const Default = () => (
},
{
header: 'Code scanning',
field: 'securityFeatures.codeScanning',
renderCell: row => {
return row.securityFeatures.codeScanning.length > 0 ? (
<LabelGroup>
Expand Down Expand Up @@ -230,6 +232,7 @@ export const WithTitle = () => (
},
{
header: 'Dependabot',
field: 'securityFeatures.dependabot',
renderCell: row => {
return row.securityFeatures.dependabot.length > 0 ? (
<LabelGroup>
Expand All @@ -242,6 +245,7 @@ export const WithTitle = () => (
},
{
header: 'Code scanning',
field: 'securityFeatures.codeScanning',
renderCell: row => {
return row.securityFeatures.codeScanning.length > 0 ? (
<LabelGroup>
Expand Down Expand Up @@ -291,6 +295,7 @@ export const WithTitleAndSubtitle = () => (
},
{
header: 'Dependabot',
field: 'securityFeatures.dependabot',
renderCell: row => {
return row.securityFeatures.dependabot.length > 0 ? (
<LabelGroup>
Expand All @@ -303,6 +308,7 @@ export const WithTitleAndSubtitle = () => (
},
{
header: 'Code scanning',
field: 'securityFeatures.codeScanning',
renderCell: row => {
return row.securityFeatures.codeScanning.length > 0 ? (
<LabelGroup>
Expand All @@ -317,3 +323,75 @@ export const WithTitleAndSubtitle = () => (
/>
</TableContainer>
)

export const WithSorting = () => {
const rows = Array.from(data).sort((a, b) => {
return b.updatedAt - a.updatedAt
})
return (
<TableContainer>
<TableTitle as="h2" id="repositories">
Repositories
</TableTitle>
<TableSubtitle as="p" id="repositories-subtitle">
A subtitle could appear here to give extra context to the data.
</TableSubtitle>
<DataTable
aria-labelledby="repositories"
aria-describedby="repositories-subtitle"
data={rows}
columns={[
{
header: 'Repository',
field: 'name',
rowHeader: true,
sortBy: true,
},
{
header: 'Type',
field: 'type',
renderCell: row => {
return <Label>{uppercase(row.type)}</Label>
},
},
{
header: 'Updated',
field: 'updatedAt',
sortBy: true,
renderCell: row => {
return <RelativeTime date={new Date(row.updatedAt)} />
},
},
{
header: 'Dependabot',
field: 'securityFeatures.dependabot',
renderCell: row => {
return row.securityFeatures.dependabot.length > 0 ? (
<LabelGroup>
{row.securityFeatures.dependabot.map(feature => {
return <Label key={feature}>{uppercase(feature)}</Label>
})}
</LabelGroup>
) : null
},
},
{
header: 'Code scanning',
field: 'securityFeatures.codeScanning',
renderCell: row => {
return row.securityFeatures.codeScanning.length > 0 ? (
<LabelGroup>
{row.securityFeatures.codeScanning.map(feature => {
return <Label key={feature}>{uppercase(feature)}</Label>
})}
</LabelGroup>
) : null
},
},
]}
initialSortColumn="updatedAt"
initialSortDirection="DESC"
/>
</TableContainer>
)
}
2 changes: 2 additions & 0 deletions src/DataTable/DataTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const Playground: ComponentStory<typeof DataTable> = args => {
},
{
header: 'Dependabot',
field: 'securityFeatures.dependabot',
renderCell: row => {
return row.securityFeatures.dependabot.length > 0 ? (
<LabelGroup>
Expand All @@ -164,6 +165,7 @@ export const Playground: ComponentStory<typeof DataTable> = args => {
},
{
header: 'Code scanning',
field: 'securityFeatures.codeScanning',
renderCell: row => {
return row.securityFeatures.codeScanning.length > 0 ? (
<LabelGroup>
Expand Down
Loading

0 comments on commit 7c83497

Please sign in to comment.