-
Notifications
You must be signed in to change notification settings - Fork 620
feat: add DataTable and Table to drafts #2951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7602c17
feat: add DataTable and Table to drafts
joshblack 1c8b3fa
refactor(DataTable): change interface to type alias
joshblack 4c41cca
test: update exports and DataTable test
joshblack ce8e6df
Merge branch 'main' into feat/add-table-datatable-to-drafts
joshblack 7c68ba5
chore: add changeset
joshblack 18ca912
Merge branch 'main' into feat/add-table-datatable-to-drafts
joshblack ad784cf
feat(drafts): add table types to exports
joshblack 0930cd4
refactor(types): update how types are re-exported
joshblack 1ec5001
Merge branch 'main' into feat/add-table-datatable-to-drafts
joshblack 6a6d850
chore: update snapshots
joshblack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/react': minor | ||
--- | ||
|
||
Add DataTable, Table to drafts entrypoint | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -392,6 +370,6 @@ export const WithSorting = () => { | |
initialSortColumn="updatedAt" | ||
initialSortDirection="DESC" | ||
/> | ||
</TableContainer> | ||
</Table.Container> | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofdrafts
? According to the ADRThere was a problem hiding this comment.
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 toexperimental
once that gets created 👀