Skip to content

Commit d8faa31

Browse files
committed
implement customized DataTable story
1 parent 8756863 commit d8faa31

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/components/Table/DataTable.stories.tsx

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Meta } from "@storybook/react";
22
import { StoryObj } from "@storybook/react";
3-
import React from "react";
3+
import React, { useState } from "react";
44
import { DataTable } from "./index";
5-
import { ColumnDef } from "@tanstack/react-table";
5+
import { ColumnDef, RowSelectionState } from "@tanstack/react-table";
6+
import { Checkbox } from "../Checkbox";
67

78
type DummyData = {
89
id: number;
@@ -80,3 +81,76 @@ export const DefaultVariant: Story = {
8081
data: data
8182
}
8283
};
84+
85+
export const CustomizedVariant: Story = {
86+
name: "Customized",
87+
render: () => <CustomizedDataTable />,
88+
args: {
89+
// provide in component below
90+
columns: [],
91+
data: []
92+
}
93+
};
94+
95+
const CustomizedDataTable = () => {
96+
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
97+
98+
return (
99+
<div>
100+
<DataTable
101+
columns={colsCustomized}
102+
data={data}
103+
getRowId={(x) => x.id.toString()}
104+
rowSelection={rowSelection}
105+
onRowSelectionChange={setRowSelection}
106+
/>
107+
108+
<div className="mt-4">
109+
<p>Selected rows:</p>
110+
<div className="flex gap-2">
111+
{Object.keys(rowSelection).map((key) => (
112+
<span key={key}>{key}</span>
113+
))}
114+
</div>
115+
</div>
116+
</div>
117+
);
118+
};
119+
120+
const colsCustomized: ColumnDef<DummyData, unknown>[] = [
121+
{
122+
id: "select",
123+
accessorKey: "select",
124+
header: ({ table }) => (
125+
<Checkbox
126+
id="select-all"
127+
checked={table.getIsAllRowsSelected()}
128+
onCheckedChange={(state) =>
129+
table.toggleAllRowsSelected(state === "indeterminate" ? true : state)
130+
}
131+
/>
132+
),
133+
cell: ({ row }) => (
134+
<Checkbox
135+
id={`select-${row.id}`}
136+
checked={row.getIsSelected()}
137+
onCheckedChange={row.getToggleSelectedHandler()}
138+
/>
139+
)
140+
},
141+
{
142+
id: "id",
143+
header: "ID",
144+
accessorKey: "id"
145+
},
146+
{
147+
id: "name",
148+
header: "Name",
149+
accessorKey: "name"
150+
},
151+
{
152+
id: "age",
153+
header: "Age",
154+
accessorKey: "age"
155+
}
156+
];

src/components/Table/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from "./Table";
22
export * from "./DataTable";
33
export * from "./DataTableConsumerContext";
44
export * from "./Sorting";
5+
export * from "./Icons";

0 commit comments

Comments
 (0)