Skip to content

Commit

Permalink
feat(tables): add isTableHeadHidden prop to DataTable
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Oct 24, 2024
1 parent e75b525 commit fb723b0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/tables/DataTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useReactTable
} from '@tanstack/react-table'
import { useState } from 'react'
import styled from 'styled-components'

import { Td } from './Td'
import { Th } from './Th'
Expand All @@ -18,9 +19,16 @@ export type DataTableProps<T extends AnyObject> = {
columns: Array<ColumnDef<T>>
data: T[] | undefined
initialSorting: SortingState
isTableHeadHidden?: boolean | undefined
tableOptions?: TableOptions<T> | undefined
}
export function DataTable<T extends AnyObject>({ columns, data, initialSorting, tableOptions }: DataTableProps<T>) {
export function DataTable<T extends AnyObject>({
columns,
data,
initialSorting,
isTableHeadHidden = false,
tableOptions
}: DataTableProps<T>) {
const [sorting, setSorting] = useState<SortingState>(initialSorting)

const table = useReactTable({
Expand Down Expand Up @@ -49,29 +57,41 @@ export function DataTable<T extends AnyObject>({ columns, data, initialSorting,

{data.length > 0 && (
<SimpleTable.Table>
<SimpleTable.Head>
{table.getHeaderGroups().map(headerGroup => (
<tr key={headerGroup.id}>
{headerGroup.headers.map(header => (
<Th key={header.id} header={header} />
))}
</tr>
))}
</SimpleTable.Head>
{!isTableHeadHidden && (
<SimpleTable.Head>
{table.getHeaderGroups().map(headerGroup => (
<tr key={headerGroup.id}>
{headerGroup.headers.map(header => (
<Th key={header.id} header={header} />
))}
</tr>
))}
</SimpleTable.Head>
)}

<tbody>
<TBody $withTopBorder={isTableHeadHidden!}>
{rows.map(row => (
<SimpleTable.BodyTr key={row.id} data-id={'id' in row.original ? row.original.id : row.id}>
{row.getVisibleCells().map(cell => (
<Td key={cell.id} cell={cell} />
))}
</SimpleTable.BodyTr>
))}
</tbody>
</TBody>
</SimpleTable.Table>
)}
</>
)}
</>
)
}

const TBody = styled.tbody<{
$withTopBorder: boolean
}>`
> tr {
> td {
border-top: ${p => (p.$withTopBorder ? `1px solid ${p.theme.color.lightGray}` : 0)};
}
}
`
2 changes: 2 additions & 0 deletions stories/tables/DataTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const args: DataTableProps<FakeBasicTableDataItem> = {
id: 'lastName'
}
],
isTableHeadHidden: false,
tableOptions: undefined
}

Expand All @@ -28,6 +29,7 @@ const meta: Meta<DataTableProps<FakeBasicTableDataItem>> = {
component: DataTable,

argTypes: {
isTableHeadHidden: ARG_TYPE.OPTIONAL_BOOLEAN,
tableOptions: {
...ARG_TYPE.NO_CONTROL_INPUT,
table: {
Expand Down

0 comments on commit fb723b0

Please sign in to comment.