mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
80 lines
2.2 KiB
TypeScript
80 lines
2.2 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
|
|
import styles from "./react-table.module.scss";
|
|
import React from "react";
|
|
import {
|
|
flexRender,
|
|
useReactTable,
|
|
getSortedRowModel,
|
|
} from '@tanstack/react-table'
|
|
import type { TableOptions, Table, SortingState } from "@tanstack/react-table";
|
|
|
|
interface TableProps<T> extends TableOptions<T> {
|
|
className?: string;
|
|
}
|
|
|
|
export function Table<Data>({ columns, data, getCoreRowModel, className }: TableProps<Data>) {
|
|
const [sorting, setSorting] = React.useState<SortingState>([])
|
|
|
|
const table = useReactTable({
|
|
data,
|
|
columns,
|
|
getCoreRowModel,
|
|
state: {
|
|
sorting,
|
|
},
|
|
onSortingChange: setSorting,
|
|
getSortedRowModel: getSortedRowModel(),
|
|
});
|
|
|
|
return (
|
|
<div className={className}>
|
|
<table>
|
|
<thead>
|
|
{table.getHeaderGroups().map(headerGroup => (
|
|
<tr key={headerGroup.id}>
|
|
{headerGroup.headers.map(header => (
|
|
<th key={header.id}>
|
|
{header.isPlaceholder ? null : (
|
|
<div
|
|
{...{
|
|
className: header.column.getCanSort()
|
|
? styles.sortableColumn
|
|
: '',
|
|
onClick: header.column.getToggleSortingHandler(),
|
|
}}
|
|
>
|
|
{flexRender(
|
|
header.column.columnDef.header,
|
|
header.getContext()
|
|
)}
|
|
{{
|
|
asc: ' 🔼',
|
|
desc: ' 🔽',
|
|
}[header.column.getIsSorted() as string] ?? null}
|
|
</div>
|
|
)}
|
|
</th>
|
|
))}
|
|
</tr>
|
|
))}
|
|
</thead>
|
|
<tbody>
|
|
{table.getRowModel().rows.map(row => (
|
|
<tr key={row.id}>
|
|
{row.getVisibleCells().map(cell => (
|
|
<td key={cell.id}>
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
</td>
|
|
))}
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)
|
|
}
|