Skip to content

Commit

Permalink
feat: add DataTable and Table to drafts (#2951)
Browse files Browse the repository at this point in the history
* feat: add DataTable and Table to drafts

* refactor(DataTable): change interface to type alias

* test: update exports and DataTable test

* chore: add changeset

* feat(drafts): add table types to exports

* refactor(types): update how types are re-exported

* chore: update snapshots

---------

Co-authored-by: Josh Black <joshblack@users.noreply.github.com>
  • Loading branch information
joshblack and joshblack authored Mar 1, 2023
1 parent 87c999f commit eff43b0
Show file tree
Hide file tree
Showing 10 changed files with 473 additions and 213 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-beers-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Add DataTable, Table to drafts entrypoint
68 changes: 23 additions & 45 deletions src/DataTable/DataTable.features.stories.tsx
Original file line number Diff line number Diff 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'
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 Down Expand 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 Down Expand 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 Down Expand 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 Down Expand 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 Down Expand 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 number Diff line number Diff 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 Down Expand 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 Down Expand 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 number Diff line number Diff 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

0 comments on commit eff43b0

Please sign in to comment.