mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Initial sketch of new react table component
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
parent
0eeb5fe300
commit
c3c81529f6
45
src/renderer/components/list/table-theme.module.scss
Normal file
45
src/renderer/components/list/table-theme.module.scss
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
.tableTheme {
|
||||||
|
table {
|
||||||
|
border-spacing: 0;
|
||||||
|
border-top: thin solid var(--hrColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
thead {
|
||||||
|
border-bottom: thin solid var(--hrColor);
|
||||||
|
color: var(--textColorAccent);
|
||||||
|
padding-top: 7px;
|
||||||
|
height: 33px;
|
||||||
|
}
|
||||||
|
|
||||||
|
thead tr {
|
||||||
|
margin: 0 var(--margin);
|
||||||
|
}
|
||||||
|
|
||||||
|
tr {
|
||||||
|
margin: calc(var(--margin) * 1.6) var(--margin);
|
||||||
|
}
|
||||||
|
|
||||||
|
th {
|
||||||
|
white-space: nowrap;
|
||||||
|
text-align: left;
|
||||||
|
padding: 0 var(--padding);
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
.headIcon {
|
||||||
|
font-weight: normal;
|
||||||
|
color: var(--textColorAccent);
|
||||||
|
}
|
||||||
|
|
||||||
|
.disabledArrow {
|
||||||
|
opacity: 0.3;
|
||||||
|
}
|
||||||
|
|
||||||
|
td {
|
||||||
|
padding: 0 var(--padding);
|
||||||
|
}
|
||||||
|
|
||||||
|
.actions {
|
||||||
|
@apply justify-end;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,44 +1,5 @@
|
|||||||
.table {
|
|
||||||
border-spacing: 0;
|
|
||||||
border-top: thin solid var(--hrColor);
|
|
||||||
}
|
|
||||||
|
|
||||||
.thead {
|
.sortableColumn {
|
||||||
@apply overflow-hidden font-bold;
|
cursor: pointer;
|
||||||
border-bottom: thin solid var(--hrColor);
|
user-select: none;
|
||||||
color: var(--textColorAccent);
|
|
||||||
padding-top: 7px;
|
|
||||||
height: 33px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.thead .tr {
|
|
||||||
margin: 0 var(--margin);
|
|
||||||
}
|
|
||||||
|
|
||||||
.tr {
|
|
||||||
@apply flex;
|
|
||||||
margin: calc(var(--margin) * 1.6) var(--margin);
|
|
||||||
}
|
|
||||||
|
|
||||||
.th {
|
|
||||||
@apply relative whitespace-nowrap;
|
|
||||||
padding: 0 var(--padding);
|
|
||||||
}
|
|
||||||
|
|
||||||
.headIcon {
|
|
||||||
font-weight: normal;
|
|
||||||
color: var(--textColorAccent);
|
|
||||||
}
|
|
||||||
|
|
||||||
.disabledArrow {
|
|
||||||
opacity: 0.3;
|
|
||||||
}
|
|
||||||
|
|
||||||
.td {
|
|
||||||
@apply flex items-center;
|
|
||||||
padding: 0 var(--padding);
|
|
||||||
}
|
|
||||||
|
|
||||||
.actions {
|
|
||||||
@apply justify-end;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,103 +4,76 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import styles from "./react-table.module.scss";
|
import styles from "./react-table.module.scss";
|
||||||
import React, { useCallback, useMemo } from "react";
|
import React from "react";
|
||||||
import type { Row, UseTableOptions } from "react-table";
|
import {
|
||||||
import { useFlexLayout, useSortBy, useTable } from "react-table";
|
flexRender,
|
||||||
import { Icon } from "../icon";
|
useReactTable,
|
||||||
import { cssNames } from "../../utils";
|
getSortedRowModel,
|
||||||
|
} from '@tanstack/react-table'
|
||||||
|
import type { TableOptions, Table, SortingState } from "@tanstack/react-table";
|
||||||
|
|
||||||
export interface ReactTableProps<Data extends object> extends UseTableOptions<Data> {
|
interface TableProps<T> extends TableOptions<T> {
|
||||||
headless?: boolean;
|
className?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ReactTable<Data extends object>({ columns, data, headless }: ReactTableProps<Data>) {
|
export function Table<Data>({ columns, data, getCoreRowModel, className }: TableProps<Data>) {
|
||||||
const defaultColumn = useMemo(
|
const [sorting, setSorting] = React.useState<SortingState>([])
|
||||||
() => ({
|
|
||||||
minWidth: 20,
|
|
||||||
width: 100,
|
|
||||||
}),
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
|
|
||||||
const {
|
const table = useReactTable({
|
||||||
getTableProps,
|
data,
|
||||||
getTableBodyProps,
|
columns,
|
||||||
headerGroups,
|
getCoreRowModel,
|
||||||
rows,
|
state: {
|
||||||
prepareRow,
|
sorting,
|
||||||
} = useTable(
|
|
||||||
{
|
|
||||||
columns,
|
|
||||||
data,
|
|
||||||
defaultColumn,
|
|
||||||
},
|
},
|
||||||
useFlexLayout,
|
onSortingChange: setSorting,
|
||||||
useSortBy,
|
getSortedRowModel: getSortedRowModel(),
|
||||||
);
|
});
|
||||||
|
|
||||||
const renderRow = useCallback(
|
|
||||||
(row: Row<Data>) => {
|
|
||||||
prepareRow(row);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className={styles.tr} key={row.id}>
|
|
||||||
{row.cells.map((cell, index) => (
|
|
||||||
<div
|
|
||||||
{...cell.getCellProps()}
|
|
||||||
key={cell.getCellProps().key}
|
|
||||||
className={cssNames(styles.td, columns[index].accessor)}
|
|
||||||
>
|
|
||||||
{cell.render("Cell")}
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
[columns, prepareRow],
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div {...getTableProps()} className={styles.table}>
|
<div className={className}>
|
||||||
{!headless && (
|
<table>
|
||||||
<div className={styles.thead}>
|
<thead>
|
||||||
{headerGroups.map(headerGroup => (
|
{table.getHeaderGroups().map(headerGroup => (
|
||||||
<div
|
<tr key={headerGroup.id}>
|
||||||
{...headerGroup.getHeaderGroupProps()}
|
{headerGroup.headers.map(header => (
|
||||||
key={headerGroup.getHeaderGroupProps().key}
|
<th key={header.id}>
|
||||||
className={styles.tr}
|
{header.isPlaceholder ? null : (
|
||||||
>
|
<div
|
||||||
{headerGroup.headers.map(column => (
|
{...{
|
||||||
<div
|
className: header.column.getCanSort()
|
||||||
{...column.getHeaderProps(column.getSortByToggleProps())}
|
? styles.sortableColumn
|
||||||
key={column.getHeaderProps().key}
|
: '',
|
||||||
className={styles.th}
|
onClick: header.column.getToggleSortingHandler(),
|
||||||
>
|
}}
|
||||||
{column.render("Header")}
|
>
|
||||||
{/* Sort direction indicator */}
|
{flexRender(
|
||||||
<span>
|
header.column.columnDef.header,
|
||||||
{column.isSorted
|
header.getContext()
|
||||||
? column.isSortedDesc
|
|
||||||
? <Icon material="arrow_drop_down" small/>
|
|
||||||
: <Icon material="arrow_drop_up" small/>
|
|
||||||
: !column.disableSortBy && (
|
|
||||||
<Icon
|
|
||||||
material="arrow_drop_down"
|
|
||||||
small
|
|
||||||
className={styles.disabledArrow}
|
|
||||||
/>
|
|
||||||
)}
|
)}
|
||||||
</span>
|
{{
|
||||||
</div>
|
asc: ' 🔼',
|
||||||
|
desc: ' 🔽',
|
||||||
|
}[header.column.getIsSorted() as string] ?? null}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</th>
|
||||||
))}
|
))}
|
||||||
</div>
|
</tr>
|
||||||
))}
|
))}
|
||||||
</div>
|
</thead>
|
||||||
)}
|
<tbody>
|
||||||
|
{table.getRowModel().rows.map(row => (
|
||||||
<div {...getTableBodyProps()}>
|
<tr key={row.id}>
|
||||||
{rows.map(renderRow)}
|
{row.getVisibleCells().map(cell => (
|
||||||
</div>
|
<td key={cell.id}>
|
||||||
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||||
|
</td>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
</div>
|
</div>
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user