Claude Code के साथ Implementation
Claude Code का उपयोग करके implementation सीखें। Practical tips और code examples शामिल हैं।
tablecomponentの要件
datatableはmanagement画面やdashboardのमें心的なUI है।sort、filter、pageネーション、列のリsize आदि多くのfeaturesが求められ है।Claude Code का उपयोग करके、TanStack Tableをベースに高featuresなtableを素早くbuild किया जा सकता है。
TanStack Tableによる基本implementation
> TanStack Tableを使ったsort・filter付きtableを作って。
> 型safeで、column定義を宣言的に書ける तरहして。
import {
useReactTable, getCoreRowModel, getSortedRowModel,
getFilteredRowModel, getPaginationRowModel,
flexRender, createColumnHelper, SortingState,
} from '@tanstack/react-table';
import { useState } from 'react';
interface User {
id: string;
name: string;
email: string;
role: string;
createdAt: string;
}
const columnHelper = createColumnHelper<User>();
const columns = [
columnHelper.accessor('name', {
header: '名पहले',
cell: (info) => <span className="font-medium">{info.getValue()}</span>,
}),
columnHelper.accessor('email', {
header: 'メールアドレス',
}),
columnHelper.accessor('role', {
header: '権限',
cell: (info) => (
<span className={`px-2 py-1 rounded text-xs ${
info.getValue() === 'admin' ? 'bg-red-100 text-red-700' : 'bg-gray-100'
}`}>
{info.getValue()}
</span>
),
}),
columnHelper.accessor('createdAt', {
header: '登録日',
cell: (info) => new Date(info.getValue()).toLocaleDateString('en-US'),
}),
];
function DataTable({ data }: { data: User[] }) {
const [sorting, setSorting] = useState<SortingState>([]);
const [globalFilter, setGlobalFilter] = useState('');
const table = useReactTable({
data,
columns,
state: { sorting, globalFilter },
onSortingChange: setSorting,
onGlobalFilterChange: setGlobalFilter,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getPaginationRowModel: getPaginationRowModel(),
});
return (
<div>
<input
value={globalFilter}
onChange={(e) => setGlobalFilter(e.target.value)}
placeholder="search..."
className="mb-4 px-3 py-2 border rounded w-full max-w-sm"
/>
<table className="w-full border-collapse" role="grid">
<thead>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<th
key={header.id}
onClick={header.column.getToggleSortingHandler()}
className="px-4 py-3 text-left bg-gray-50 border-b cursor-pointer select-none"
aria-sort={header.column.getIsSorted() === 'asc' ? 'ascending' : header.column.getIsSorted() === 'desc' ? 'descending' : 'none'}
>
{flexRender(header.column.columnDef.header, header.getContext())}
{{ asc: ' ↑', desc: ' ↓' }[header.column.getIsSorted() as string] ?? ''}
</th>
))}
</tr>
))}
</thead>
<tbody>
{table.getRowModel().rows.map((row) => (
<tr key={row.id} className="hover:bg-gray-50">
{row.getVisibleCells().map((cell) => (
<td key={cell.id} className="px-4 py-3 border-b">
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
<Pagination table={table} />
</div>
);
}
pageネーション
function Pagination({ table }: { table: any }) {
return (
<div className="flex items-center justify-between mt-4">
<span className="text-sm text-gray-500">
全{table.getFilteredRowModel().rows.length}件में{' '}
{table.getState().pagination.pageIndex * table.getState().pagination.pageSize + 1}〜
{Math.min(
(table.getState().pagination.pageIndex + 1) * table.getState().pagination.pageSize,
table.getFilteredRowModel().rows.length
)}件
</span>
<div className="flex gap-2">
<button onClick={() => table.previousPage()} disabled={!table.getCanPreviousPage()}
className="px-3 py-1 border rounded disabled:opacity-50">Previous</button>
<button onClick={() => table.nextPage()} disabled={!table.getCanNextPage()}
className="px-3 py-1 border rounded disabled:opacity-50">Next</button>
</div>
</div>
);
}
行選択と一括操作
const [rowSelection, setRowSelection] = useState({});
// tablesettingsにadd
const table = useReactTable({
// ...既存のsettings
state: { sorting, globalFilter, rowSelection },
onRowSelectionChange: setRowSelection,
enableRowSelection: true,
});
// 一括操作button
const selectedCount = Object.keys(rowSelection).length;
{selectedCount > 0 && (
<div className="mb-4 p-3 bg-blue-50 rounded flex items-center gap-4">
<span>{selectedCount}件選択में</span>
<button onClick={() => handleBulkDelete(table.getSelectedRowModel().rows)}>
一括Delete
</button>
</div>
)}
Summary
Claude Code का उपयोग करके、TanStack Tableベースの高featurestableをsort・filter・pageネーション付きで素早くbuild किया जा सकता है。dataの可視化はdata可視化の記事を、カレンダーdisplayはカレンダーcomponentをदेखें。
TanStack Tableके details के लिएTanStack Tableofficial documentationदेखें。
Related Posts
Claude Code से Productivity 3 गुना बढ़ाने की 10 Tips
Claude Code से ज़्यादा पाने की 10 practical tips जानें। Prompt strategies से workflow shortcuts तक, ये techniques आज से ही आपकी efficiency boost करेंगी।
Claude Code के साथ Canvas/WebGL Optimization
Claude Code का उपयोग करके Canvas/WebGL optimization के बारे में जानें। Practical tips और code examples शामिल हैं।
Claude Code के साथ Markdown Implementation
Claude Code का उपयोग करके markdown implementation सीखें। Practical tips और code examples शामिल हैं।