1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Move TableHeader to its own component

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-08-08 09:38:35 +03:00
parent bd965c93f4
commit 3df4c306e5
3 changed files with 45 additions and 32 deletions

View File

@ -3,10 +3,10 @@
* 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 } from '@tanstack/react-table'
import type { Table } from "@tanstack/react-table";
import { TableHeader } from "./table-header";
interface TableProps<T> {
table: Table<T>;
@ -17,35 +17,7 @@ export function Table<Data>({ className, table }: TableProps<Data>) {
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>
<TableHeader table={table}/>
<tbody>
{table.getRowModel().rows.map(row => (
<tr key={row.id}>

View File

@ -1,5 +1,4 @@
.sortableColumn {
cursor: pointer;
user-select: none;
}
}

View File

@ -0,0 +1,42 @@
import styles from "./table-header.module.scss";
import React from "react";
import type { Table } from "@tanstack/react-table";
import { flexRender } from '@tanstack/react-table';
interface TableHeaderProps<Data> {
table: Table<Data>;
}
export function TableHeader<Data>({ table }: TableHeaderProps<Data>) {
return (
<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>
)
}