Skip to content

Commit ad01733

Browse files
committed
Add custom width to Table column's gridWidth
1 parent f33a222 commit ad01733

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

packages/bento-design-system/src/Table/createTable.tsx

+9-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
sectionHeaderContainer,
3434
table,
3535
} from "./Table.css";
36-
import { Column as ColumnType, Row as RowType } from "./types";
36+
import { Column as ColumnType, GridWidth, Row as RowType } from "./types";
3737
import {
3838
useLayoutEffect,
3939
useMemo,
@@ -60,6 +60,7 @@ import { ButtonLinkProps } from "../Button/ButtonLink";
6060
import { IconButtonProps } from "../IconButton/createIconButton";
6161
import { TableConfig } from "./Config";
6262
import { IconHelp, IconInfo } from "../Icons";
63+
import { match, P } from "ts-pattern";
6364

6465
type SortFn<C extends ReadonlyArray<ColumnType<string, {}, any>>> = (
6566
a: Row<RowType<C>>,
@@ -245,13 +246,13 @@ export function createTable(
245246
);
246247
}
247248

248-
function gridWidthStyle(gridWidth: "fit-content" | "fill-available"): string {
249-
switch (gridWidth) {
250-
case "fit-content":
251-
return "max-content";
252-
case "fill-available":
253-
return "minmax(max-content, auto)";
254-
}
249+
function gridWidthStyle(gridWidth: GridWidth): string {
250+
return match(gridWidth)
251+
.with("fit-content", () => "max-content")
252+
.with("fill-available", () => "minmax(max-content, auto")
253+
.with({ custom: P.select(P.string) }, (width) => width)
254+
.with({ custom: P.select(P.number) }, (width) => `${width}px`)
255+
.exhaustive();
255256
}
256257

257258
const gridTemplateColumns = columns

packages/bento-design-system/src/Table/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ declare module "react-table" {
1212
interface ColumnInterface<D extends object> extends UseSortByColumnOptions<D> {
1313
align?: "left" | "right" | "center";
1414
sticky?: "left";
15-
gridWidth?: "fit-content" | "fill-available";
15+
gridWidth?: GridWidth;
1616
hint?: LocalizedString | { onPress: () => void };
1717
}
1818

@@ -35,3 +35,5 @@ type ColumnValueByAccessor<C, K extends string> = C extends Column<K, infer _D,
3535
export type Row<Columns extends ReadonlyArray<Column<string, {}, any>>> = {
3636
[k in Columns[number]["accessor"]]?: ColumnValueByAccessor<Columns[number], k>;
3737
};
38+
39+
export type GridWidth = "fit-content" | "fill-available" | { custom: string | number };

packages/storybook/stories/Components/Table.stories.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const exampleColumns = [
3434
headerLabel: formatMessage("Name"),
3535
accessor: "name",
3636
}),
37+
tableColumn.text({
38+
headerLabel: formatMessage("Extended address"),
39+
accessor: "address",
40+
width: { custom: 200 },
41+
}),
3742
tableColumn.textWithIcon({
3843
headerLabel: formatMessage("Country"),
3944
accessor: "country",
@@ -87,6 +92,7 @@ const deleteAction = {
8792
const exampleData = [
8893
{
8994
name: formatMessage("Amazon"),
95+
address: "Theodore Lowe Ap #867-859 Sit Rd. Azusa New York 39531",
9096
country: {
9197
icon: IconInformative,
9298
text: formatMessage("US"),
@@ -110,6 +116,7 @@ const exampleData = [
110116
},
111117
{
112118
name: formatMessage("Google"),
119+
address: "Cecilia Chapman 711-2880 Nulla St. Mankato Mississippi 96522",
113120
country: {
114121
icon: IconInformative,
115122
text: formatMessage("US"),
@@ -133,6 +140,7 @@ const exampleData = [
133140
},
134141
{
135142
name: formatMessage("Microsoft"),
143+
address: "Iris Watson P.O. Box 283 8562 Fusce Rd. Frederick Nebraska 20620",
136144
country: {
137145
icon: IconInformative,
138146
text: formatMessage("US"),
@@ -157,6 +165,7 @@ const exampleData = [
157165
},
158166
{
159167
name: formatMessage("buildo"),
168+
address: "Celeste Slater 606-3727 Ullamcorper. Street Roseville NH 11523",
160169
country: {
161170
icon: null,
162171
text: formatMessage("IT"),
@@ -176,6 +185,7 @@ const exampleData = [
176185
},
177186
{
178187
name: formatMessage("Twitter"),
188+
address: "Theodore Lowe Ap #867-859 Sit Rd. Azusa New York 39531",
179189
country: {
180190
icon: IconInformative,
181191
text: formatMessage("US"),

0 commit comments

Comments
 (0)