Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit e6727ab

Browse files
committed
fix(table): fix table types
1 parent f322e2f commit e6727ab

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

src/components/Table/Table.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import React, { ReactNode } from 'react'
1+
import React from 'react'
22
import { ButtonVariant } from 'src/interfaces'
33

44
import { Button } from '../Button'
55

6-
interface Row {
7-
[key: string]: ReactNode
8-
}
9-
10-
interface Props<T extends Row> {
6+
interface Props<T> {
117
tableClassName: string
128
headerClassName: string
139
columns: { key: string; label: string; formatter?: (row: T) => React.ReactNode }[]
@@ -18,7 +14,7 @@ interface Props<T extends Row> {
1814
onRowClick?: (row: T) => void
1915
}
2016

21-
function Table<T extends Row>(props: Props<T>) {
17+
function Table<T>(props: Props<T>) {
2218
const {
2319
tableClassName,
2420
headerClassName,
@@ -35,7 +31,7 @@ function Table<T extends Row>(props: Props<T>) {
3531
<thead className={headerClassName}>
3632
<tr>
3733
{columns.map((column) => (
38-
<th key={column.key}>{column.label}</th>
34+
<th key={column.key as string}>{column.label}</th>
3935
))}
4036
{actions ? <th>{actionsHeaderText}</th> : null}
4137
</tr>
@@ -52,7 +48,7 @@ function Table<T extends Row>(props: Props<T>) {
5248
}}
5349
>
5450
{columns.map((column) => {
55-
const content = !column.formatter ? row[column.key] : column.formatter(row)
51+
const content = !column.formatter ? row[column.key as keyof T] : column.formatter(row)
5652
return <td key={`${column.key}-${getID(row)}`}>{content}</td>
5753
})}
5854

stories/table.stories.tsx

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import React from 'react'
44
import { Table, Toast, Toaster } from '../src'
55
import { ButtonVariant } from '../src/interfaces'
66

7+
interface Data {
8+
id: number
9+
name: string
10+
phone: string
11+
drinks: string[]
12+
}
13+
714
storiesOf('Table', module)
815
.addParameters({
916
info: {
@@ -14,62 +21,57 @@ storiesOf('Table', module)
1421
<div style={{ marginLeft: '2em', marginRight: '2em' }}>{storyFn()}</div>
1522
))
1623
.add('Demo Table', () => {
17-
const ID = 'id'
18-
const NAME = 'name'
19-
const PHONE = 'phone'
20-
const DRINKS = 'drinks'
21-
2224
const getDrinksList = (row: any) => (
2325
<ul>
24-
{row[DRINKS].map((d: string) => (
26+
{row.drinks.map((d: string) => (
2527
<li>{d}</li>
2628
))}
2729
</ul>
2830
)
2931

3032
const columns = [
31-
{ key: ID, label: 'ID' },
32-
{ key: NAME, label: 'Name' },
33-
{ key: PHONE, label: 'Phone #' },
34-
{ key: DRINKS, label: 'Drinks?', formatter: getDrinksList },
33+
{ key: 'id', label: 'ID' },
34+
{ key: 'name', label: 'Name' },
35+
{ key: 'phone', label: 'Phone #' },
36+
{ key: 'drinks', label: 'Drinks?', formatter: getDrinksList },
3537
]
3638

37-
const data = [
39+
const data: Data[] = [
3840
{
39-
[ID]: 333,
40-
[NAME]: 'Mickey',
41-
[PHONE]: '789-777',
42-
[DRINKS]: ['milk', 'tea'],
41+
id: 333,
42+
name: 'Mickey',
43+
phone: '789-777',
44+
drinks: ['milk', 'tea'],
4345
},
4446
{
45-
[ID]: 777,
46-
[NAME]: 'Minnie',
47-
[PHONE]: '333-123',
48-
[DRINKS]: ['water', 'coffee'],
47+
id: 777,
48+
name: 'Minnie',
49+
phone: '333-123',
50+
drinks: ['water', 'coffee'],
4951
},
5052
]
5153

5254
const actions = [
5355
{
5456
label: 'Edit',
5557
action: (row: any) => {
56-
Toast('info', 'an edit clicked', `will edit ID=${row[ID]}`)
58+
Toast('info', 'an edit clicked', `will edit ID=${row.id}`)
5759
},
5860
buttonColor: 'info' as ButtonVariant,
5961
},
6062
{
6163
label: 'Delete',
6264
action: (row: any) => {
63-
Toast('error', 'a delete clicked', `will delete ID=${row[ID]}`)
65+
Toast('error', 'a delete clicked', `will delete ID=${row.id}`)
6466
},
6567
buttonColor: 'danger' as ButtonVariant,
6668
},
6769
]
6870

69-
const getID = (row: any) => row[ID]
71+
const getID = (row: any): string => row.id
7072

7173
const onRowClick = (row: any) => {
72-
Toast('success', 'a row clicked', `${row[NAME]} @ ${row[PHONE]}`)
74+
Toast('success', 'a row clicked', `${row.name} @ ${row.phone}`)
7375
}
7476

7577
return (

0 commit comments

Comments
 (0)