Skip to content
5 changes: 5 additions & 0 deletions .changeset/spotty-beers-cheer.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Add DataTable, Table to drafts entrypoint

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am wondering when will be the best time to start exporting new components in experimental bundle instead of drafts? According to the ADR

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Great point, reached out about this over in Slack to see where that work is. For this PR I'll add it to drafts and will add it to experimental once that gets created 👀

68 changes: 23 additions & 45 deletions src/DataTable/DataTable.features.stories.tsx
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
import {Meta} from '@storybook/react'
import React from 'react'
import {
DataTable,
Table,
TableHead,
TableBody,
TableRow,
TableHeader,
TableCell,
TableContainer,
TableTitle,
TableSubtitle,
} from '../DataTable'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

import {DataTable, Table} from '../DataTable'
import Label from '../Label'
import LabelGroup from '../LabelGroup'
import RelativeTime from '../RelativeTime'

export default {
title: 'Drafts/Components/DataTable/Features',
component: DataTable,
subcomponents: {
Table,
TableHead,
TableBody,
TableRow,
TableHeader,
TableCell,
TableContainer,
TableTitle,
TableSubtitle,
},
} as Meta<typeof DataTable>

const now = Date.now()
Expand DownExpand Up@@ -139,13 +117,13 @@ function uppercase(input: string): string {
}

export const Default = () => (
<TableContainer>
<TableTitle as="h2" id="repositories">
<Table.Container>
<Table.Title as="h2" id="repositories">
Repositories
</TableTitle>
<TableSubtitle as="p" id="repositories-subtitle">
</Table.Title>
<Table.Subtitle as="p" id="repositories-subtitle">
A subtitle could appear here to give extra context to the data.
</TableSubtitle>
</Table.Subtitle>
<DataTable
aria-labelledby="repositories"
aria-describedby="repositories-subtitle"
Expand DownExpand Up@@ -198,14 +176,14 @@ export const Default = () => (
},
]}
/>
</TableContainer>
</Table.Container>
)

export const WithTitle = () => (
<TableContainer>
<TableTitle as="h2" id="repositories">
<Table.Container>
<Table.Title as="h2" id="repositories">
Repositories
</TableTitle>
</Table.Title>
<DataTable
aria-labelledby="repositories"
aria-describedby="repositories-subtitle"
Expand DownExpand Up@@ -258,17 +236,17 @@ export const WithTitle = () => (
},
]}
/>
</TableContainer>
</Table.Container>
)

export const WithTitleAndSubtitle = () => (
<TableContainer>
<TableTitle as="h2" id="repositories">
<Table.Container>
<Table.Title as="h2" id="repositories">
Repositories
</TableTitle>
<TableSubtitle as="p" id="repositories-subtitle">
</Table.Title>
<Table.Subtitle as="p" id="repositories-subtitle">
A subtitle could appear here to give extra context to the data.
</TableSubtitle>
</Table.Subtitle>
<DataTable
aria-labelledby="repositories"
aria-describedby="repositories-subtitle"
Expand DownExpand Up@@ -321,21 +299,21 @@ export const WithTitleAndSubtitle = () => (
},
]}
/>
</TableContainer>
</Table.Container>
)

export const WithSorting = () => {
const rows = Array.from(data).sort((a, b) => {
return b.updatedAt - a.updatedAt
})
return (
<TableContainer>
<TableTitle as="h2" id="repositories">
<Table.Container>
<Table.Title as="h2" id="repositories">
Repositories
</TableTitle>
<TableSubtitle as="p" id="repositories-subtitle">
</Table.Title>
<Table.Subtitle as="p" id="repositories-subtitle">
A subtitle could appear here to give extra context to the data.
</TableSubtitle>
</Table.Subtitle>
<DataTable
aria-labelledby="repositories"
aria-describedby="repositories-subtitle"
Expand DownExpand Up@@ -392,6 +370,6 @@ export const WithSorting = () => {
initialSortColumn="updatedAt"
initialSortDirection="DESC"
/>
</TableContainer>
</Table.Container>
)
}
14 changes: 7 additions & 7 deletions src/DataTable/DataTable.stories.tsx
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
import {Meta, ComponentStory} from '@storybook/react'
import React from 'react'
import {DataTable, TableContainer, TableTitle, TableSubtitle} from '../DataTable'
import {DataTable, Table} from '../DataTable'
import Label from '../Label'
import LabelGroup from '../LabelGroup'
import RelativeTime from '../RelativeTime'
Expand DownExpand Up@@ -118,13 +118,13 @@ function uppercase(input: string): string {

export const Playground: ComponentStory<typeof DataTable> = args => {
return (
<TableContainer>
<TableTitle as="h2" id="repositories">
<Table.Container>
<Table.Title as="h2" id="repositories">
Repositories
</TableTitle>
<TableSubtitle as="p" id="repositories-subtitle">
</Table.Title>
<Table.Subtitle as="p" id="repositories-subtitle">
A subtitle could appear here to give extra context to the data.
</TableSubtitle>
</Table.Subtitle>
<DataTable
{...args}
aria-labelledby="repositories"
Expand DownExpand Up@@ -178,7 +178,7 @@ export const Playground: ComponentStory<typeof DataTable> = args => {
},
]}
/>
</TableContainer>
</Table.Container>
)
}

Expand Down
113 changes: 113 additions & 0 deletions src/DataTable/DataTable.tsx
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
import React from 'react'
import {Column} from './column'
import {useTable} from './useTable'
import {SortDirection} from './sorting'
import {UniqueRow} from './row'
import {ObjectPaths} from './utils'
import {Table, TableHead, TableBody, TableRow, TableHeader, TableSortHeader, TableCell} from './Table'

// ----------------------------------------------------------------------------
// DataTable
// ----------------------------------------------------------------------------

export type DataTableProps<Data extends UniqueRow> = {
/**
* Provide an id to an element which uniquely describes this table
*/
'aria-describedby'?: string | undefined

/**
* Provide an id to an element which uniquely labels this table
*/
'aria-labelledby'?: string | undefined

/**
* Specify the amount of space that should be available around the contents of
* a cell
*/
cellPadding?: 'condensed' | 'normal' | 'spacious' | undefined

/**
* Provide a collection of the rows which will be rendered inside of the table
*/
data: Array<Data>

/**
* Provide the columns for the table and the fields in `data` to which they
* correspond
*/
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
*/
initialSortColumn?: ObjectPaths<Data> | string | undefined

/**
* Provide the sort direction that the table should be sorted by on the
* currently sorted column
*/
initialSortDirection?: Exclude<SortDirection, 'NONE'> | undefined
}

function DataTable<Data extends UniqueRow>({
'aria-labelledby': labelledby,
'aria-describedby': describedby,
cellPadding,
columns,
data,
initialSortColumn,
initialSortDirection,
}: DataTableProps<Data>) {
const {headers, rows, actions} = useTable({
data,
columns,
initialSortColumn,
initialSortDirection,
})
return (
<Table aria-labelledby={labelledby} aria-describedby={describedby} cellPadding={cellPadding}>
<TableHead>
<TableRow>
{headers.map(header => {
if (header.isSortable()) {
return (
<TableSortHeader
key={header.id}
direction={header.getSortDirection()}
onToggleSort={() => {
actions.sortBy(header)
}}
>
{header.column.header}
</TableSortHeader>
)
}
return <TableHeader key={header.id}>{header.column.header}</TableHeader>
})}
</TableRow>
</TableHead>
<TableBody>
{rows.map(row => {
return (
<TableRow key={row.id}>
{row.getCells().map(cell => {
return (
<TableCell key={cell.id} scope={cell.rowHeader ? 'row' : undefined}>
{cell.column.renderCell
? cell.column.renderCell(row.getValue())
: (cell.getValue() as React.ReactNode)}
</TableCell>
)
})}
</TableRow>
)
})}
</TableBody>
</Table>
)
}

export {DataTable}
Loading