-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from internxt/feat/table-component
[PB-3284]: feat/table component
- Loading branch information
Showing
6 changed files
with
393 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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,62 @@ | ||
import React from 'react'; | ||
|
||
interface TableProps extends React.HTMLAttributes<HTMLTableElement> { | ||
children: React.ReactNode; | ||
className?: string; | ||
} | ||
|
||
interface TableHeaderProps extends React.HTMLAttributes<HTMLTableSectionElement> { | ||
children: React.ReactNode; | ||
className?: string; | ||
} | ||
|
||
interface TableBodyProps extends React.HTMLAttributes<HTMLTableSectionElement> { | ||
children: React.ReactNode; | ||
className?: string; | ||
} | ||
|
||
interface TableRowProps extends React.HTMLAttributes<HTMLTableRowElement> { | ||
children: React.ReactNode; | ||
className?: string; | ||
} | ||
|
||
interface TableCellProps extends React.HTMLAttributes<HTMLTableCellElement> { | ||
children: React.ReactNode; | ||
className?: string; | ||
isHeader?: boolean; | ||
} | ||
|
||
export const Table: React.FC<TableProps> = ({ children, className, ...props }) => ( | ||
<div className={className}> | ||
<table className="w-full" {...props}> | ||
{children} | ||
</table> | ||
</div> | ||
); | ||
|
||
export const TableHeader: React.FC<TableHeaderProps> = ({ children, className, ...props }) => ( | ||
<thead className={className} {...props}> | ||
{children} | ||
</thead> | ||
); | ||
|
||
export const TableBody: React.FC<TableBodyProps> = ({ children, className, ...props }) => ( | ||
<tbody className={className} {...props}> | ||
{children} | ||
</tbody> | ||
); | ||
|
||
export const TableRow: React.FC<TableRowProps> = ({ children, className, onClick, ...props }) => ( | ||
<tr onClick={onClick} className={className} {...props}> | ||
{children} | ||
</tr> | ||
); | ||
|
||
export const TableCell: React.FC<TableCellProps> = ({ children, className, isHeader = false, onClick, ...props }) => { | ||
const Component = isHeader ? 'th' : 'td'; | ||
return ( | ||
<Component onClick={onClick} className={className} {...props}> | ||
{children} | ||
</Component> | ||
); | ||
}; |
This file contains 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,97 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { describe, it, expect } from 'vitest'; | ||
import { Table, TableHeader, TableBody, TableRow, TableCell } from '../Table'; | ||
|
||
describe('Table Component Snapshots', () => { | ||
it('renders the complete table structure', () => { | ||
const { container } = render( | ||
<Table className="custom-table-class"> | ||
<TableHeader className="custom-header-class"> | ||
<TableRow className="custom-row-class"> | ||
<TableCell isHeader className="custom-cell-class"> | ||
Header 1 | ||
</TableCell> | ||
<TableCell isHeader>Header 2</TableCell> | ||
</TableRow> | ||
</TableHeader> | ||
<TableBody className="custom-body-class"> | ||
<TableRow> | ||
<TableCell>Cell 1</TableCell> | ||
<TableCell>Cell 2</TableCell> | ||
</TableRow> | ||
<TableRow className="another-row-class"> | ||
<TableCell className="another-cell-class">Cell 3</TableCell> | ||
<TableCell>Cell 4</TableCell> | ||
</TableRow> | ||
</TableBody> | ||
</Table>, | ||
); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders a table with minimal structure', () => { | ||
const { container } = render( | ||
<Table> | ||
<TableHeader> | ||
<TableRow> | ||
<TableCell isHeader>Header</TableCell> | ||
</TableRow> | ||
</TableHeader> | ||
<TableBody> | ||
<TableRow> | ||
<TableCell>Cell</TableCell> | ||
</TableRow> | ||
</TableBody> | ||
</Table>, | ||
); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders a row with an onClick handler', () => { | ||
const handleClick = () => {}; | ||
const { container } = render( | ||
<Table> | ||
<TableBody> | ||
<TableRow onClick={handleClick}> | ||
<TableCell>Clickable Row Cell</TableCell> | ||
</TableRow> | ||
</TableBody> | ||
</Table>, | ||
); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders a cell with custom props', () => { | ||
const { container } = render( | ||
<Table> | ||
<TableBody> | ||
<TableRow> | ||
<TableCell className="custom-class" data-testid="test-cell"> | ||
Custom Cell | ||
</TableCell> | ||
</TableRow> | ||
</TableBody> | ||
</Table>, | ||
); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders a header cell with isHeader set to true', () => { | ||
const { container } = render( | ||
<Table> | ||
<TableHeader> | ||
<TableRow> | ||
<TableCell isHeader>Header Cell</TableCell> | ||
</TableRow> | ||
</TableHeader> | ||
</Table>, | ||
); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
}); |
136 changes: 136 additions & 0 deletions
136
src/components/table/__test__/__snapshots__/Table.test.tsx.snap
This file contains 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,136 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`Table Component Snapshots > renders a cell with custom props 1`] = ` | ||
<div> | ||
<div> | ||
<table | ||
class="w-full" | ||
> | ||
<tbody> | ||
<tr> | ||
<td | ||
class="custom-class" | ||
data-testid="test-cell" | ||
> | ||
Custom Cell | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`Table Component Snapshots > renders a header cell with isHeader set to true 1`] = ` | ||
<div> | ||
<div> | ||
<table | ||
class="w-full" | ||
> | ||
<thead> | ||
<tr> | ||
<th> | ||
Header Cell | ||
</th> | ||
</tr> | ||
</thead> | ||
</table> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`Table Component Snapshots > renders a row with an onClick handler 1`] = ` | ||
<div> | ||
<div> | ||
<table | ||
class="w-full" | ||
> | ||
<tbody> | ||
<tr> | ||
<td> | ||
Clickable Row Cell | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`Table Component Snapshots > renders a table with minimal structure 1`] = ` | ||
<div> | ||
<div> | ||
<table | ||
class="w-full" | ||
> | ||
<thead> | ||
<tr> | ||
<th> | ||
Header | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td> | ||
Cell | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`Table Component Snapshots > renders the complete table structure 1`] = ` | ||
<div> | ||
<div | ||
class="custom-table-class" | ||
> | ||
<table | ||
class="w-full" | ||
> | ||
<thead | ||
class="custom-header-class" | ||
> | ||
<tr | ||
class="custom-row-class" | ||
> | ||
<th | ||
class="custom-cell-class" | ||
> | ||
Header 1 | ||
</th> | ||
<th> | ||
Header 2 | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody | ||
class="custom-body-class" | ||
> | ||
<tr> | ||
<td> | ||
Cell 1 | ||
</td> | ||
<td> | ||
Cell 2 | ||
</td> | ||
</tr> | ||
<tr | ||
class="another-row-class" | ||
> | ||
<td | ||
class="another-cell-class" | ||
> | ||
Cell 3 | ||
</td> | ||
<td> | ||
Cell 4 | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
`; |
Oops, something went wrong.