Skip to content

Commit 54f7bd7

Browse files
committed
Add support for size in tailwind-based Table component
1 parent 8e52ad7 commit 54f7bd7

File tree

5 files changed

+319
-43
lines changed

5 files changed

+319
-43
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org).
66

7+
## [0.9.6] - 2025-05-31
8+
### Added
9+
* Add `size` property to tailwind-based `Table` component.
10+
11+
### Changed
12+
* *Nothing*
13+
14+
### Deprecated
15+
* *Nothing*
16+
17+
### Removed
18+
* *Nothing*
19+
20+
### Fixed
21+
* *Nothing*
22+
23+
724
## [0.9.5] - 2025-05-30
825
### Added
926
* *Nothing*

dev/tailwind/content/TablePage.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export const TablePage: FC = () => {
9696
))}
9797
</Table>
9898
</div>
99+
99100
<div className="tw:flex tw:flex-col tw:gap-y-2">
100101
<h2>Non-responsive table inside Card</h2>
101102
<SimpleCard>
@@ -133,6 +134,7 @@ export const TablePage: FC = () => {
133134
</Table>
134135
</SimpleCard>
135136
</div>
137+
136138
<div className="tw:flex tw:flex-col tw:gap-y-2">
137139
<h2>Table with footer</h2>
138140
<Table
@@ -158,6 +160,64 @@ export const TablePage: FC = () => {
158160
))}
159161
</Table>
160162
</div>
163+
164+
<div className="tw:flex tw:flex-col tw:gap-y-2">
165+
<h2>Table sizes</h2>
166+
<Table
167+
size="sm"
168+
header={
169+
<Table.Row>
170+
<Table.Cell>Small</Table.Cell>
171+
<Table.Cell>Small</Table.Cell>
172+
<Table.Cell>Small</Table.Cell>
173+
</Table.Row>
174+
}
175+
>
176+
{users.slice(0, 2).map((u) => (
177+
<Table.Row key={u.id}>
178+
<Table.Cell columnName="Name:">{u.name}</Table.Cell>
179+
<Table.Cell columnName="Surname:">{u.surname}</Table.Cell>
180+
<Table.Cell columnName="Role:">{u.role}</Table.Cell>
181+
</Table.Row>
182+
))}
183+
</Table>
184+
<Table
185+
size="md"
186+
header={
187+
<Table.Row>
188+
<Table.Cell>Medium</Table.Cell>
189+
<Table.Cell>Medium</Table.Cell>
190+
<Table.Cell>Medium</Table.Cell>
191+
</Table.Row>
192+
}
193+
>
194+
{users.slice(0, 2).map((u) => (
195+
<Table.Row key={u.id}>
196+
<Table.Cell columnName="Name:">{u.name}</Table.Cell>
197+
<Table.Cell columnName="Surname:">{u.surname}</Table.Cell>
198+
<Table.Cell columnName="Role:">{u.role}</Table.Cell>
199+
</Table.Row>
200+
))}
201+
</Table>
202+
<Table
203+
size="lg"
204+
header={
205+
<Table.Row>
206+
<Table.Cell>Large</Table.Cell>
207+
<Table.Cell>Large</Table.Cell>
208+
<Table.Cell>Large</Table.Cell>
209+
</Table.Row>
210+
}
211+
>
212+
{users.slice(0, 2).map((u) => (
213+
<Table.Row key={u.id}>
214+
<Table.Cell columnName="Name:">{u.name}</Table.Cell>
215+
<Table.Cell columnName="Surname:">{u.surname}</Table.Cell>
216+
<Table.Cell columnName="Role:">{u.role}</Table.Cell>
217+
</Table.Row>
218+
))}
219+
</Table>
220+
</div>
161221
</div>
162222
);
163223
};

src/tailwind/content/Table.tsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import clsx from 'clsx';
22
import type { FC, HTMLProps, PropsWithChildren, ReactNode } from 'react';
33
import { createContext, useContext } from 'react';
4+
import type { Size } from '../types';
45

56
export type SectionType = 'head' | 'body' | 'footer';
67

78
const TableSectionContext = createContext<{ section: SectionType } | undefined>(undefined);
89

9-
const TableContext = createContext<{ responsive: boolean }>({ responsive: true });
10+
const TableContext = createContext<{ responsive: boolean; size: Size }>({ responsive: true, size: 'md' });
1011

1112
export type TableElementProps = PropsWithChildren & {
1213
className?: string;
@@ -106,17 +107,22 @@ export type CellProps = HTMLProps<HTMLTableCellElement> & {
106107
const Cell: FC<CellProps> = ({ children, className, columnName, type, ...rest }) => {
107108
const sectionContext = useContext(TableSectionContext);
108109
const Tag = type ?? (sectionContext?.section !== 'body' ? 'th' : 'td');
109-
const { responsive } = useContext(TableContext);
110+
const { responsive, size } = useContext(TableContext);
110111

111112
return (
112113
<Tag
113114
data-column={responsive ? columnName : undefined}
114115
className={clsx(
115-
'tw:p-2 tw:border-lm-border tw:dark:border-dm-border',
116+
'tw:border-lm-border tw:dark:border-dm-border',
116117
{
118+
'tw:p-1': size === 'sm',
119+
'tw:p-2': size === 'md',
120+
'tw:p-3': size === 'lg',
121+
117122
'tw:border-b-1': !responsive,
118123
'tw:block tw:lg:table-cell tw:not-last:border-b-1 tw:lg:border-b-1': responsive,
119-
// For md and lower, display the content in data-column attribute as before
124+
125+
// For responsive tables, display the content in data-column attribute for md sizes and lower
120126
'tw:before:lg:hidden tw:before:content-[attr(data-column)] tw:before:font-bold tw:before:mr-1': responsive && Tag === 'td',
121127
},
122128
className,
@@ -128,7 +134,7 @@ const Cell: FC<CellProps> = ({ children, className, columnName, type, ...rest })
128134
);
129135
};
130136

131-
export type TableProps = HTMLProps<HTMLTableElement> & {
137+
export type TableProps = Omit<HTMLProps<HTMLTableElement>, 'size'> & {
132138
header: ReactNode;
133139
footer?: ReactNode;
134140

@@ -138,20 +144,13 @@ export type TableProps = HTMLProps<HTMLTableElement> & {
138144
*/
139145
responsive?: boolean;
140146

141-
/**
142-
* @todo
143-
* `default`: Only horizontal borders are rendered. No background color on any row
144-
* `stripped`: Every other row has a different background color
145-
* `grid`: Both horizontal and vertical borders are rendered. No background color on any row
146-
*/
147-
// variant?: 'default' | 'stripped' | 'grid';
148-
149-
// size?: Size;
147+
/** Determines the padding in every cell. Defaults to md */
148+
size?: Size;
150149
};
151150

152-
const BaseTable: FC<TableProps> = ({ header, footer, children, responsive = true, ...rest }) => {
151+
const BaseTable: FC<TableProps> = ({ header, footer, children, responsive = true, size = 'md', ...rest }) => {
153152
return (
154-
<TableContext.Provider value={{ responsive }}>
153+
<TableContext.Provider value={{ responsive, size }}>
155154
<table className="tw:w-full" {...rest}>
156155
<TableHead>
157156
{header}

test/tailwind/content/Table.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ describe('<Table />', () => {
4646
</Table.Row>
4747
),
4848
},
49+
{ size: 'sm' as const },
50+
{ size: 'lg' as const },
4951
])('renders as expected based on provided props', (props) => {
5052
const { container } = setUp(props);
5153
expect(container).toMatchSnapshot();

0 commit comments

Comments
 (0)