Skip to content

Commit b153a08

Browse files
committed
fix!: update headers props
BREAKING CHANGE: headers props is updated
1 parent 071082d commit b153a08

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

src/components/table/table.cy.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('Table', () => {
3333
)
3434
})
3535

36-
it(`renders headers based on data keys even though headers are not provided on ${viewport} screen`, () => {
36+
it(`renders headers based on data keys if headers props are not provided on ${viewport} screen`, () => {
3737
cy.viewport(viewport)
3838
cy.mount(<Table data={testData} />)
3939
Object.keys(testData[firstRowIndex]).forEach((item) => {
@@ -43,10 +43,25 @@ describe('Table', () => {
4343

4444
it(`renders headers if provided on ${viewport} screen`, () => {
4545
cy.viewport(viewport)
46-
const headers = ['column 1', 'column 2']
46+
const headers = [
47+
{ dataKey: 'col2', label: 'column 2' },
48+
{ dataKey: 'col1', label: 'column 1' },
49+
]
4750
cy.mount(<Table headers={headers} data={testData} />)
4851
headers.forEach((header) => {
49-
cy.get('th').contains(header, { matchCase: false })
52+
cy.get('th').contains(header.label, { matchCase: false })
53+
})
54+
55+
cy.get('.rustic-table tbody tr').each((row) => {
56+
cy.wrap(row)
57+
.find('td')
58+
.each((cell, index) => {
59+
const header = headers[index]
60+
const value = (testData as Record<string, any>)[row.index()][
61+
header.dataKey
62+
]
63+
expect(cell.text().trim()).to.equal(value.toString())
64+
})
5065
})
5166
})
5267

@@ -59,26 +74,28 @@ describe('Table', () => {
5974

6075
it(`does not show data if its header is not in the headers props on ${viewport} screen`, () => {
6176
cy.viewport(viewport)
62-
const headers = ['col1', 'col2']
77+
const headers = [{ dataKey: 'col1' }, { dataKey: 'col2' }]
6378
const testDataWithExtra = [...testData, { col1: 'ghl', col3: 'extra' }]
6479
cy.mount(<Table headers={headers} data={testDataWithExtra} />)
6580
cy.contains('extra').should('not.exist')
6681
})
6782

6883
it(`can render rows based on headers on ${viewport} screen`, () => {
6984
cy.viewport(viewport)
70-
const headers = ['col2', 'col1']
85+
const headers = [{ dataKey: 'col2' }, { dataKey: 'col1' }]
7186
cy.mount(<Table headers={headers} data={testData} />)
7287
cy.get('th').each((th, index) => {
73-
expect(th.text().trim().toLowerCase()).contains(headers[index])
88+
expect(th.text().trim().toLowerCase()).contains(headers[index].dataKey)
7489
})
7590

7691
cy.get('.rustic-table tbody tr').each((row) => {
7792
cy.wrap(row)
7893
.find('td')
7994
.each((cell, index) => {
8095
const header = headers[index]
81-
const value = (testData as Record<string, any>)[row.index()][header]
96+
const value = (testData as Record<string, any>)[row.index()][
97+
header.dataKey
98+
]
8299
expect(cell.text().trim()).to.equal(value.toString())
83100
})
84101
})

src/components/table/table.stories.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export default {
1313
},
1414
},
1515
},
16+
argTypes: {
17+
headers: {
18+
control: 'array',
19+
description:
20+
'Use this prop to set the order of columns and assign headers. If none is provided, headers will be taken from the keys of the data objects from the `data` prop array and the first letter will be capitalized. \n<pre>```interface TableHeader:{\n dataKey: string\n label?: string\n}```</pre>',
21+
},
22+
},
1623
}
1724

1825
const sampleData = [
@@ -62,7 +69,13 @@ export const Default = {
6269
export const ChangeOrderUsingHeader = {
6370
args: {
6471
data: sampleData,
65-
headers: ['food', 'carbs', 'protein', 'fat', 'calories'],
72+
headers: [
73+
{ dataKey: 'food' },
74+
{ dataKey: 'carbs' },
75+
{ dataKey: 'protein' },
76+
{ dataKey: 'fat' },
77+
{ dataKey: 'calories' },
78+
],
6679
},
6780
}
6881

@@ -75,15 +88,15 @@ export const HasTitleAndDescription = {
7588
},
7689
}
7790

78-
export const HasHeadersProps = {
91+
export const CustomizeHeaders = {
7992
args: {
8093
title: 'Nutrient Data Comparison Across Various Types of Milk',
8194
headers: [
82-
'type (per 250ml)',
83-
'calories (kCal)',
84-
'carbs (g)',
85-
'protein (g)',
86-
'fat (g)',
95+
{ dataKey: 'food', label: 'type (per 250ml)' },
96+
{ dataKey: 'calories', label: 'calories (kCal)' },
97+
{ dataKey: 'carbs', label: 'carbs (g)' },
98+
{ dataKey: 'protein', label: 'protein (g)' },
99+
{ dataKey: 'fat', label: 'fat (g)' },
87100
],
88101
description:
89102
'This table illustrates the variations in calories and nutrients for different types of milk, with measurements based on a serving size of 250 ml. Caloric values are expressed in kCal, and nutrient quantities are measured in grams. The data is sourced from the Canadian Nutrient File.',

src/components/table/table.tsx

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ import React from 'react'
1212

1313
import { capitalizeFirstLetter } from '../helper'
1414

15+
export interface TableHeader {
16+
dataKey: string
17+
label?: string
18+
}
19+
1520
export type TableProps = {
1621
/** Data to be displayed in the table. */
1722
data: Array<Record<string, string | number>>
1823
/** Title of the table. If no title is provided, `aria-label` is defaults to "a table of data". */
1924
title?: string
2025
/** Description of the table. */
2126
description?: string
22-
/** Use this prop to set customized headers. If none is provided, headers will be taken from the keys of the data objects from the `data` prop array. */
23-
headers?: string[]
27+
headers?: TableHeader[]
2428
}
2529

2630
export default function Table(props: TableProps) {
@@ -31,7 +35,8 @@ export default function Table(props: TableProps) {
3135
const dataKeys = Array.from(
3236
new Set(props.data.flatMap((rowData) => Object.keys(rowData)))
3337
)
34-
const headers = props.headers || dataKeys
38+
const headers: TableHeader[] =
39+
props.headers || dataKeys.map((key) => ({ dataKey: key }))
3540

3641
return (
3742
<Box className="rustic-table">
@@ -54,31 +59,25 @@ export default function Table(props: TableProps) {
5459
{headers.length > 0 && (
5560
<TableHead>
5661
<TableRow>
57-
{headers.map((header, index) => {
58-
return (
59-
<TableCell key={`header-${index}`}>
60-
{capitalizeFirstLetter(header)}
61-
</TableCell>
62-
)
63-
})}
62+
{headers.map((header, index) => (
63+
<TableCell key={`header-${index}`}>
64+
{header.label || capitalizeFirstLetter(header.dataKey)}
65+
</TableCell>
66+
))}
6467
</TableRow>
6568
</TableHead>
6669
)}
6770

6871
<TableBody>
69-
{props.data.map((rowData, index) => {
70-
return (
71-
<TableRow key={`row-${index}`}>
72-
{headers.map((header, dataIndex) => {
73-
return (
74-
<TableCell key={`cell-${index}x${dataIndex}`}>
75-
{rowData[header]}
76-
</TableCell>
77-
)
78-
})}
79-
</TableRow>
80-
)
81-
})}
72+
{props.data.map((rowData, rowIndex) => (
73+
<TableRow key={`row-${rowIndex}`}>
74+
{headers.map((header, colIndex) => (
75+
<TableCell key={`cell-${rowIndex}-${colIndex}`}>
76+
{rowData[header.dataKey]}
77+
</TableCell>
78+
))}
79+
</TableRow>
80+
))}
8281
</TableBody>
8382
</MuiTable>
8483
</TableContainer>

0 commit comments

Comments
 (0)