Skip to content

Commit 941af96

Browse files
joshblackradglob
authored andcommitted
refactor(DataTable): update optional type signatures (#2987)
* refactor(DataTable): update optional type signatures * chore: update docs and add stories to json for DataTable * chore: remove default story * Update generated/components.json --------- Co-authored-by: Josh Black <[email protected]>
1 parent 9c8af24 commit 941af96

File tree

8 files changed

+54
-77
lines changed

8 files changed

+54
-77
lines changed

docs/content/drafts/DataTable.mdx

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,6 @@ status: Draft
66

77
import data from '../../../src/DataTable/DataTable.docs.json'
88

9-
## Examples
10-
11-
```jsx
12-
<DataTable
13-
title="Repositories"
14-
data={[
15-
{
16-
id: 1,
17-
name: 'codeql-dca-worker',
18-
type: 'internal',
19-
securityFeatures: {
20-
dependabot: ['alerts', 'security updates'],
21-
},
22-
},
23-
{
24-
id: 2,
25-
name: 'aegir',
26-
type: 'public',
27-
securityFeatures: {
28-
dependabot: ['alerts'],
29-
},
30-
},
31-
{
32-
id: 3,
33-
name: 'strapi',
34-
type: 'public',
35-
securityFeatures: {
36-
dependabot: [],
37-
},
38-
},
39-
]}
40-
columns={[
41-
{
42-
header: 'Repository',
43-
field: 'name',
44-
rowHeader: true,
45-
},
46-
{
47-
header: 'Type',
48-
field: 'type',
49-
renderCell: row => {
50-
return <Label>{uppercase(row.type)}</Label>
51-
},
52-
},
53-
{
54-
header: 'Dependabot',
55-
renderCell: row => {
56-
return row.securityFeatures.dependabot.length > 0 ? (
57-
<LabelGroup>
58-
{row.securityFeatures.dependabot.map(feature => {
59-
return <Label key={feature}>{uppercase(feature)}</Label>
60-
})}
61-
</LabelGroup>
62-
) : null
63-
},
64-
},
65-
]}
66-
/>
67-
```
68-
699
## Props
7010

7111
<ComponentProps data={data} />

generated/components.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,26 @@
18981898
{
18991899
"id": "components-datatable--default",
19001900
"code": "() => (\n <Table.Container>\n <Table.Title as=\"h2\" id=\"repositories\">\n Repositories\n </Table.Title>\n <Table.Subtitle as=\"p\" id=\"repositories-subtitle\">\n A subtitle could appear here to give extra context to the data.\n </Table.Subtitle>\n <DataTable\n aria-labelledby=\"repositories\"\n aria-describedby=\"repositories-subtitle\"\n data={data}\n columns={[\n {\n header: 'Repository',\n field: 'name',\n rowHeader: true,\n },\n {\n header: 'Type',\n field: 'type',\n renderCell: (row) => {\n return <Label>{uppercase(row.type)}</Label>\n },\n },\n {\n header: 'Updated',\n field: 'updatedAt',\n renderCell: (row) => {\n return <RelativeTime date={new Date(row.updatedAt)} />\n },\n },\n {\n header: 'Dependabot',\n field: 'securityFeatures.dependabot',\n renderCell: (row) => {\n return row.securityFeatures.dependabot.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.dependabot.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n {\n header: 'Code scanning',\n field: 'securityFeatures.codeScanning',\n renderCell: (row) => {\n return row.securityFeatures.codeScanning.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.codeScanning.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n ]}\n />\n </Table.Container>\n)"
1901+
},
1902+
{
1903+
"id": "drafts-components-datatable--with-title",
1904+
"code": "() => (\n <Table.Container>\n <Table.Title as=\"h2\" id=\"repositories\">\n Repositories\n </Table.Title>\n <DataTable\n aria-labelledby=\"repositories\"\n aria-describedby=\"repositories-subtitle\"\n data={data}\n columns={[\n {\n header: 'Repository',\n field: 'name',\n rowHeader: true,\n },\n {\n header: 'Type',\n field: 'type',\n renderCell: (row) => {\n return <Label>{uppercase(row.type)}</Label>\n },\n },\n {\n header: 'Updated',\n field: 'updatedAt',\n renderCell: (row) => {\n return <RelativeTime date={new Date(row.updatedAt)} />\n },\n },\n {\n header: 'Dependabot',\n field: 'securityFeatures.dependabot',\n renderCell: (row) => {\n return row.securityFeatures.dependabot.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.dependabot.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n {\n header: 'Code scanning',\n field: 'securityFeatures.codeScanning',\n renderCell: (row) => {\n return row.securityFeatures.codeScanning.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.codeScanning.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n ]}\n />\n </Table.Container>\n)"
1905+
},
1906+
{
1907+
"id": "drafts-components-datatable--with-title-and-subtitle",
1908+
"code": "() => (\n <Table.Container>\n <Table.Title as=\"h2\" id=\"repositories\">\n Repositories\n </Table.Title>\n <Table.Subtitle as=\"p\" id=\"repositories-subtitle\">\n A subtitle could appear here to give extra context to the data.\n </Table.Subtitle>\n <DataTable\n aria-labelledby=\"repositories\"\n aria-describedby=\"repositories-subtitle\"\n data={data}\n columns={[\n {\n header: 'Repository',\n field: 'name',\n rowHeader: true,\n },\n {\n header: 'Type',\n field: 'type',\n renderCell: (row) => {\n return <Label>{uppercase(row.type)}</Label>\n },\n },\n {\n header: 'Updated',\n field: 'updatedAt',\n renderCell: (row) => {\n return <RelativeTime date={new Date(row.updatedAt)} />\n },\n },\n {\n header: 'Dependabot',\n field: 'securityFeatures.dependabot',\n renderCell: (row) => {\n return row.securityFeatures.dependabot.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.dependabot.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n {\n header: 'Code scanning',\n field: 'securityFeatures.codeScanning',\n renderCell: (row) => {\n return row.securityFeatures.codeScanning.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.codeScanning.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n ]}\n />\n </Table.Container>\n)"
1909+
},
1910+
{
1911+
"id": "drafts-components-datatable--with-sorting",
1912+
"code": "() => {\n const rows = Array.from(data).sort((a, b) => {\n return b.updatedAt - a.updatedAt\n })\n return (\n <Table.Container>\n <Table.Title as=\"h2\" id=\"repositories\">\n Repositories\n </Table.Title>\n <Table.Subtitle as=\"p\" id=\"repositories-subtitle\">\n A subtitle could appear here to give extra context to the data.\n </Table.Subtitle>\n <DataTable\n aria-labelledby=\"repositories\"\n aria-describedby=\"repositories-subtitle\"\n data={rows}\n columns={[\n {\n header: 'Repository',\n field: 'name',\n rowHeader: true,\n sortBy: true,\n },\n {\n header: 'Type',\n field: 'type',\n renderCell: (row) => {\n return <Label>{uppercase(row.type)}</Label>\n },\n },\n {\n header: 'Updated',\n field: 'updatedAt',\n sortBy: true,\n renderCell: (row) => {\n return <RelativeTime date={new Date(row.updatedAt)} />\n },\n },\n {\n header: 'Dependabot',\n field: 'securityFeatures.dependabot',\n renderCell: (row) => {\n return row.securityFeatures.dependabot.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.dependabot.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n {\n header: 'Code scanning',\n field: 'securityFeatures.codeScanning',\n renderCell: (row) => {\n return row.securityFeatures.codeScanning.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.codeScanning.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n ]}\n initialSortColumn=\"updatedAt\"\n initialSortDirection=\"DESC\"\n />\n </Table.Container>\n )\n}"
1913+
},
1914+
{
1915+
"id": "drafts-components-datatable--with-actions",
1916+
"code": "() => (\n <Table.Container>\n <Table.Title as=\"h2\" id=\"repositories\">\n Repositories\n </Table.Title>\n <Table.Actions>\n <IconButton\n aria-label=\"Download\"\n icon={DownloadIcon}\n variant=\"invisible\"\n />\n <IconButton aria-label=\"Add row\" icon={PlusIcon} variant=\"invisible\" />\n </Table.Actions>\n <Table.Divider />\n <Table.Subtitle as=\"p\" id=\"repositories-subtitle\">\n A subtitle could appear here to give extra context to the data.\n </Table.Subtitle>\n <DataTable\n aria-labelledby=\"repositories\"\n aria-describedby=\"repositories-subtitle\"\n data={data}\n columns={[\n {\n header: 'Repository',\n field: 'name',\n rowHeader: true,\n },\n {\n header: 'Type',\n field: 'type',\n renderCell: (row) => {\n return <Label>{uppercase(row.type)}</Label>\n },\n },\n {\n header: 'Updated',\n field: 'updatedAt',\n renderCell: (row) => {\n return <RelativeTime date={new Date(row.updatedAt)} />\n },\n },\n {\n header: 'Dependabot',\n field: 'securityFeatures.dependabot',\n renderCell: (row) => {\n return row.securityFeatures.dependabot.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.dependabot.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n {\n header: 'Code scanning',\n field: 'securityFeatures.codeScanning',\n renderCell: (row) => {\n return row.securityFeatures.codeScanning.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.codeScanning.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n ]}\n />\n </Table.Container>\n)"
1917+
},
1918+
{
1919+
"id": "drafts-components-datatable--with-action",
1920+
"code": "() => (\n <Table.Container>\n <Table.Title as=\"h2\" id=\"repositories\">\n Repositories\n </Table.Title>\n <Table.Actions>\n <Button>Action</Button>\n </Table.Actions>\n <Table.Divider />\n <Table.Subtitle as=\"p\" id=\"repositories-subtitle\">\n A subtitle could appear here to give extra context to the data.\n </Table.Subtitle>\n <DataTable\n aria-labelledby=\"repositories\"\n aria-describedby=\"repositories-subtitle\"\n data={data}\n columns={[\n {\n header: 'Repository',\n field: 'name',\n rowHeader: true,\n },\n {\n header: 'Type',\n field: 'type',\n renderCell: (row) => {\n return <Label>{uppercase(row.type)}</Label>\n },\n },\n {\n header: 'Updated',\n field: 'updatedAt',\n renderCell: (row) => {\n return <RelativeTime date={new Date(row.updatedAt)} />\n },\n },\n {\n header: 'Dependabot',\n field: 'securityFeatures.dependabot',\n renderCell: (row) => {\n return row.securityFeatures.dependabot.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.dependabot.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n {\n header: 'Code scanning',\n field: 'securityFeatures.codeScanning',\n renderCell: (row) => {\n return row.securityFeatures.codeScanning.length > 0 ? (\n <LabelGroup>\n {row.securityFeatures.codeScanning.map((feature) => {\n return <Label key={feature}>{uppercase(feature)}</Label>\n })}\n </LabelGroup>\n ) : null\n },\n },\n ]}\n />\n </Table.Container>\n)"
19011921
}
19021922
],
19031923
"props": [
@@ -1997,7 +2017,7 @@
19972017
},
19982018
{
19992019
"name": "scope",
2000-
"type": "string",
2020+
"type": "'row'",
20012021
"description": "Provide the scope for a table cell, useful for defining a row header using `scope=\"row\"`"
20022022
}
20032023
]

src/DataTable/DataTable.docs.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,23 @@
33
"name": "DataTable",
44
"status": "draft",
55
"a11yReviewed": false,
6-
"stories": [],
6+
"stories": [
7+
{
8+
"id": "drafts-components-datatable--with-title"
9+
},
10+
{
11+
"id": "drafts-components-datatable--with-title-and-subtitle"
12+
},
13+
{
14+
"id": "drafts-components-datatable--with-sorting"
15+
},
16+
{
17+
"id": "drafts-components-datatable--with-actions"
18+
},
19+
{
20+
"id": "drafts-components-datatable--with-action"
21+
}
22+
],
723
"props": [
824
{
925
"name": "aria-describedby",
@@ -101,7 +117,7 @@
101117
},
102118
{
103119
"name": "scope",
104-
"type": "string",
120+
"type": "'row'",
105121
"description": "Provide the scope for a table cell, useful for defining a row header using `scope=\"row\"`"
106122
}
107123
]

src/DataTable/DataTable.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ export type DataTableProps<Data extends UniqueRow> = {
1414
/**
1515
* Provide an id to an element which uniquely describes this table
1616
*/
17-
'aria-describedby'?: string | undefined
17+
'aria-describedby'?: string
1818

1919
/**
2020
* Provide an id to an element which uniquely labels this table
2121
*/
22-
'aria-labelledby'?: string | undefined
22+
'aria-labelledby'?: string
2323

2424
/**
2525
* Specify the amount of space that should be available around the contents of
2626
* a cell
2727
*/
28-
cellPadding?: 'condensed' | 'normal' | 'spacious' | undefined
28+
cellPadding?: 'condensed' | 'normal' | 'spacious'
2929

3030
/**
3131
* Provide a collection of the rows which will be rendered inside of the table
@@ -43,13 +43,13 @@ export type DataTableProps<Data extends UniqueRow> = {
4343
* using this `prop`, the input data must be sorted by this column in
4444
* ascending order
4545
*/
46-
initialSortColumn?: ObjectPaths<Data> | string | undefined
46+
initialSortColumn?: ObjectPaths<Data> | string
4747

4848
/**
4949
* Provide the sort direction that the table should be sorted by on the
5050
* currently sorted column
5151
*/
52-
initialSortDirection?: Exclude<SortDirection, 'NONE'> | undefined
52+
initialSortDirection?: Exclude<SortDirection, 'NONE'>
5353
}
5454

5555
function DataTable<Data extends UniqueRow>({

src/DataTable/Table.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,18 @@ export type TableProps = React.ComponentPropsWithoutRef<'table'> & {
144144
/**
145145
* Provide an id to an element which uniquely describes this table
146146
*/
147-
'aria-describedby'?: string | undefined
147+
'aria-describedby'?: string
148148

149149
/**
150150
* Provide an id to an element which uniquely labels this table
151151
*/
152-
'aria-labelledby'?: string | undefined
152+
'aria-labelledby'?: string
153153

154154
/**
155155
* Specify the amount of space that should be available around the contents of
156156
* a cell
157157
*/
158-
cellPadding?: 'condensed' | 'normal' | 'spacious' | undefined
158+
cellPadding?: 'condensed' | 'normal' | 'spacious'
159159
}
160160

161161
const Table = React.forwardRef<HTMLTableElement, TableProps>(function Table({cellPadding = 'normal', ...rest}, ref) {
@@ -252,7 +252,7 @@ export type TableCellProps = React.ComponentPropsWithoutRef<'td'> & {
252252
* Provide the scope for a table cell, useful for defining a row header using
253253
* `scope="row"`
254254
*/
255-
scope?: 'row' | undefined
255+
scope?: 'row'
256256
}
257257

258258
function TableCell({children, scope, ...rest}: TableCellProps) {

src/DataTable/column.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {UniqueRow} from './row'
33
import {SortStrategies} from './sorting'
44

55
export interface Column<Data extends UniqueRow> {
6-
id?: string | undefined
6+
id?: string
77

88
/**
99
* Provide the name of the column. This will be rendered as a table header
@@ -25,13 +25,13 @@ export interface Column<Data extends UniqueRow> {
2525
* Provide a custom component or render prop to render the data for this
2626
* column in a row
2727
*/
28-
renderCell?: ((data: Data) => React.ReactNode) | undefined
28+
renderCell?: (data: Data) => React.ReactNode
2929

3030
/**
3131
* Specify if the value of this column for a row should be treated as a row
3232
* header
3333
*/
34-
rowHeader?: boolean | undefined
34+
rowHeader?: boolean
3535

3636
/**
3737
* Specify if the table should sort by this column and, if applicable, a

src/DataTable/useTable.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {ObjectPathValue} from './utils'
77
interface TableConfig<Data extends UniqueRow> {
88
columns: Array<Column<Data>>
99
data: Array<Data>
10-
initialSortColumn?: string | undefined
11-
initialSortDirection?: Exclude<SortDirection, 'NONE'> | undefined
10+
initialSortColumn?: string
11+
initialSortDirection?: Exclude<SortDirection, 'NONE'>
1212
}
1313

1414
interface Table<Data extends UniqueRow> {

src/__tests__/storybook.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const allowlist = [
1818
'Checkbox',
1919
'CheckboxGroup',
2020
'CounterLabel',
21+
'DataTable',
2122
'Details',
2223
'Flash',
2324
'Heading',

0 commit comments

Comments
 (0)