-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor ColumnPinningTable to use createColumnHelper for improved ty…
…pe safety. Enhance TableGrid with stricter typing for columns and update fuzzy search implementation for better key handling.
- Loading branch information
1 parent
7ca0b77
commit 2668553
Showing
10 changed files
with
268 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
"use client"; | ||
import { useMemo } from "react"; | ||
import TableGrid from "@/components/ui/table-grid/table-grid"; | ||
import dummyData from "@/data/dummy.json"; | ||
import { useTableGrid } from "@/hooks/use-table-grid"; | ||
import { createColumnHelper } from "@/components/ui/table-grid/column-helper"; | ||
|
||
interface DataItem { | ||
id: number; | ||
name: string; | ||
age: number; | ||
email: string; | ||
department: string; | ||
role: string; | ||
salary: number; | ||
status: string; | ||
location: string; | ||
joinDate: string; | ||
phone: string; | ||
[key: string]: unknown; | ||
} | ||
|
||
const columnHelper = createColumnHelper<DataItem>(); | ||
|
||
const ColumnResizingTable = () => { | ||
const columns = useMemo( | ||
() => [ | ||
columnHelper.accessor("id", { | ||
header: "ID", | ||
sortable: true, | ||
}), | ||
columnHelper.accessor("name", { | ||
header: "Name", | ||
sortable: true, | ||
}), | ||
columnHelper.accessor("email", { | ||
header: "Email", | ||
sortable: true, | ||
}), | ||
columnHelper.accessor("department", { | ||
header: "Department", | ||
sortable: true, | ||
}), | ||
columnHelper.accessor("role", { | ||
header: "Role", | ||
sortable: true, | ||
}), | ||
columnHelper.accessor("salary", { | ||
header: "Salary", | ||
sortable: true, | ||
}), | ||
], | ||
[] | ||
); | ||
|
||
const { filteredData, handleSort, sortColumn, sortDirection } = | ||
useTableGrid<DataItem>({ | ||
data: dummyData, | ||
columns, | ||
initialState: { | ||
sortColumn: "name", | ||
sortDirection: "asc", | ||
}, | ||
onStateChange: (state) => { | ||
console.log("Table state changed:", state); | ||
}, | ||
}); | ||
|
||
return ( | ||
<div className="p-4"> | ||
<div className="flex justify-between items-center mb-4"> | ||
<h2 className="text-2xl font-bold">Column Resizing Table</h2> | ||
</div> | ||
|
||
<TableGrid<DataItem> | ||
columns={columns} | ||
data={filteredData} | ||
gridTemplateColumns="1fr 1fr 1fr 1fr 1fr 1fr" | ||
maxHeight="400px" | ||
variant="classic" | ||
onSort={handleSort} | ||
sortColumn={sortColumn} | ||
sortDirection={sortDirection} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ColumnResizingTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { Column } from './types' | ||
|
||
export type DisplayColumnDef<TData, TValue = unknown> = Omit<Column<TData>, 'id' | 'accessorKey'> & { | ||
id?: keyof TData | ||
cell?: (props: { value: TValue; row: TData }) => React.ReactNode | ||
} | ||
|
||
export type GroupColumnDef<TData, TValue = unknown> = DisplayColumnDef<TData, TValue> & { | ||
columns?: Column<TData>[] | ||
} | ||
|
||
export interface ColumnHelper<TData> { | ||
accessor: <TKey extends keyof TData>( | ||
accessorKey: TKey, | ||
columnDef?: DisplayColumnDef<TData, TData[TKey]> | ||
) => Column<TData> | ||
|
||
display: (columnDef: DisplayColumnDef<TData>) => Column<TData> | ||
|
||
group: (columnDef: GroupColumnDef<TData>) => Column<TData> | ||
} | ||
|
||
export function createColumnHelper<TData>(): ColumnHelper<TData> { | ||
return { | ||
accessor: <TKey extends keyof TData>( | ||
accessorKey: TKey, | ||
columnDef: Partial<DisplayColumnDef<TData, TData[TKey]>> = {} | ||
): Column<TData> => ({ | ||
id: accessorKey, | ||
accessorKey, | ||
header: columnDef.header ?? String(accessorKey), | ||
sortable: columnDef.sortable ?? true, | ||
className: columnDef.className, | ||
width: columnDef.width, | ||
group: columnDef.group, | ||
pinned: columnDef.pinned, | ||
cell: columnDef.cell, | ||
}), | ||
|
||
display: (columnDef) => ({ | ||
id: columnDef.id ?? String(Math.random()) as keyof TData, | ||
accessorKey: '' as keyof TData, | ||
...columnDef, | ||
}), | ||
|
||
group: (columnDef) => ({ | ||
id: columnDef.id ?? String(Math.random()) as keyof TData, | ||
accessorKey: '' as keyof TData, | ||
...columnDef, | ||
}), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.