|
1 | 1 | import { Meta } from "@storybook/react";
|
2 | 2 | import { StoryObj } from "@storybook/react";
|
3 |
| -import React from "react"; |
| 3 | +import React, { useState } from "react"; |
4 | 4 | import { DataTable } from "./index";
|
5 |
| -import { ColumnDef } from "@tanstack/react-table"; |
| 5 | +import { ColumnDef, RowSelectionState } from "@tanstack/react-table"; |
| 6 | +import { Checkbox } from "../Checkbox"; |
6 | 7 |
|
7 | 8 | type DummyData = {
|
8 | 9 | id: number;
|
@@ -80,3 +81,76 @@ export const DefaultVariant: Story = {
|
80 | 81 | data: data
|
81 | 82 | }
|
82 | 83 | };
|
| 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 | +]; |
0 commit comments