From e6727abf35ca3f23b72fba28c2bbd490a6fae57b Mon Sep 17 00:00:00 2001 From: Jack Meyer Date: Fri, 23 Oct 2020 20:50:57 -0500 Subject: [PATCH] fix(table): fix table types --- src/components/Table/Table.tsx | 14 ++++------ stories/table.stories.tsx | 48 ++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/components/Table/Table.tsx b/src/components/Table/Table.tsx index 3c2d351b..3572ca07 100644 --- a/src/components/Table/Table.tsx +++ b/src/components/Table/Table.tsx @@ -1,13 +1,9 @@ -import React, { ReactNode } from 'react' +import React from 'react' import { ButtonVariant } from 'src/interfaces' import { Button } from '../Button' -interface Row { - [key: string]: ReactNode -} - -interface Props { +interface Props { tableClassName: string headerClassName: string columns: { key: string; label: string; formatter?: (row: T) => React.ReactNode }[] @@ -18,7 +14,7 @@ interface Props { onRowClick?: (row: T) => void } -function Table(props: Props) { +function Table(props: Props) { const { tableClassName, headerClassName, @@ -35,7 +31,7 @@ function Table(props: Props) { {columns.map((column) => ( - {column.label} + {column.label} ))} {actions ? {actionsHeaderText} : null} @@ -52,7 +48,7 @@ function Table(props: Props) { }} > {columns.map((column) => { - const content = !column.formatter ? row[column.key] : column.formatter(row) + const content = !column.formatter ? row[column.key as keyof T] : column.formatter(row) return {content} })} diff --git a/stories/table.stories.tsx b/stories/table.stories.tsx index 00d2c177..5e1149c6 100644 --- a/stories/table.stories.tsx +++ b/stories/table.stories.tsx @@ -4,6 +4,13 @@ import React from 'react' import { Table, Toast, Toaster } from '../src' import { ButtonVariant } from '../src/interfaces' +interface Data { + id: number + name: string + phone: string + drinks: string[] +} + storiesOf('Table', module) .addParameters({ info: { @@ -14,38 +21,33 @@ storiesOf('Table', module)
{storyFn()}
)) .add('Demo Table', () => { - const ID = 'id' - const NAME = 'name' - const PHONE = 'phone' - const DRINKS = 'drinks' - const getDrinksList = (row: any) => (
    - {row[DRINKS].map((d: string) => ( + {row.drinks.map((d: string) => (
  • {d}
  • ))}
) const columns = [ - { key: ID, label: 'ID' }, - { key: NAME, label: 'Name' }, - { key: PHONE, label: 'Phone #' }, - { key: DRINKS, label: 'Drinks?', formatter: getDrinksList }, + { key: 'id', label: 'ID' }, + { key: 'name', label: 'Name' }, + { key: 'phone', label: 'Phone #' }, + { key: 'drinks', label: 'Drinks?', formatter: getDrinksList }, ] - const data = [ + const data: Data[] = [ { - [ID]: 333, - [NAME]: 'Mickey', - [PHONE]: '789-777', - [DRINKS]: ['milk', 'tea'], + id: 333, + name: 'Mickey', + phone: '789-777', + drinks: ['milk', 'tea'], }, { - [ID]: 777, - [NAME]: 'Minnie', - [PHONE]: '333-123', - [DRINKS]: ['water', 'coffee'], + id: 777, + name: 'Minnie', + phone: '333-123', + drinks: ['water', 'coffee'], }, ] @@ -53,23 +55,23 @@ storiesOf('Table', module) { label: 'Edit', action: (row: any) => { - Toast('info', 'an edit clicked', `will edit ID=${row[ID]}`) + Toast('info', 'an edit clicked', `will edit ID=${row.id}`) }, buttonColor: 'info' as ButtonVariant, }, { label: 'Delete', action: (row: any) => { - Toast('error', 'a delete clicked', `will delete ID=${row[ID]}`) + Toast('error', 'a delete clicked', `will delete ID=${row.id}`) }, buttonColor: 'danger' as ButtonVariant, }, ] - const getID = (row: any) => row[ID] + const getID = (row: any): string => row.id const onRowClick = (row: any) => { - Toast('success', 'a row clicked', `${row[NAME]} @ ${row[PHONE]}`) + Toast('success', 'a row clicked', `${row.name} @ ${row.phone}`) } return (