work on data-table
This commit is contained in:
parent
55a01f7099
commit
47676edb13
2 changed files with 74 additions and 20 deletions
|
|
@ -10,9 +10,56 @@ import {
|
||||||
SortingState,
|
SortingState,
|
||||||
useReactTable,
|
useReactTable,
|
||||||
} from '@tanstack/react-table';
|
} from '@tanstack/react-table';
|
||||||
import { useState } from 'react';
|
import { useState, useRef, useEffect } from 'react';
|
||||||
import { TableVirtuoso } from 'react-virtuoso';
|
import { TableVirtuoso } from 'react-virtuoso';
|
||||||
|
|
||||||
|
// Tooltip wrapper for cells that might overflow
|
||||||
|
function CellWithTooltip({ children, className }: { children: React.ReactNode; className?: string }) {
|
||||||
|
const [showTooltip, setShowTooltip] = useState(false);
|
||||||
|
const [tooltipContent, setTooltipContent] = useState('');
|
||||||
|
const cellRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const element = cellRef.current;
|
||||||
|
if (element) {
|
||||||
|
// Check if content is overflowing
|
||||||
|
const isOverflowing = element.scrollWidth > element.clientWidth;
|
||||||
|
if (isOverflowing) {
|
||||||
|
setTooltipContent(element.textContent || '');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [children]);
|
||||||
|
|
||||||
|
const handleMouseEnter = () => {
|
||||||
|
const element = cellRef.current;
|
||||||
|
if (element && element.scrollWidth > element.clientWidth) {
|
||||||
|
setShowTooltip(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleMouseLeave = () => {
|
||||||
|
setShowTooltip(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="relative">
|
||||||
|
<div
|
||||||
|
ref={cellRef}
|
||||||
|
className={className}
|
||||||
|
onMouseEnter={handleMouseEnter}
|
||||||
|
onMouseLeave={handleMouseLeave}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
{showTooltip && tooltipContent && (
|
||||||
|
<div className="absolute z-50 px-2 py-1 bg-gray-900 text-white text-xs rounded shadow-lg whitespace-nowrap -top-8 left-0 max-w-xs">
|
||||||
|
{tooltipContent}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
interface DataTableProps<T> {
|
interface DataTableProps<T> {
|
||||||
data: T[];
|
data: T[];
|
||||||
columns: ColumnDef<T>[];
|
columns: ColumnDef<T>[];
|
||||||
|
|
@ -49,6 +96,10 @@ export function DataTable<T>({
|
||||||
getRowCanExpand,
|
getRowCanExpand,
|
||||||
enableColumnResizing: true,
|
enableColumnResizing: true,
|
||||||
columnResizeMode: 'onChange',
|
columnResizeMode: 'onChange',
|
||||||
|
getCenterTotalSize: () => {
|
||||||
|
// Force table to use full width
|
||||||
|
return '100%';
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
|
|
@ -82,6 +133,7 @@ export function DataTable<T>({
|
||||||
style={{
|
style={{
|
||||||
...style,
|
...style,
|
||||||
width: '100%',
|
width: '100%',
|
||||||
|
tableLayout: 'fixed',
|
||||||
}}
|
}}
|
||||||
className="bg-background"
|
className="bg-background"
|
||||||
/>
|
/>
|
||||||
|
|
@ -112,11 +164,15 @@ export function DataTable<T>({
|
||||||
<td
|
<td
|
||||||
key={cell.id}
|
key={cell.id}
|
||||||
className="px-3 py-2 text-sm text-text-primary"
|
className="px-3 py-2 text-sm text-text-primary"
|
||||||
style={{ width: cell.column.getSize() }}
|
style={{
|
||||||
|
width: `${cell.column.getSize()}px`,
|
||||||
|
minWidth: `${cell.column.getSize()}px`,
|
||||||
|
maxWidth: `${cell.column.getSize()}px`,
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<div className="truncate">
|
<CellWithTooltip className="truncate overflow-hidden text-ellipsis whitespace-nowrap">
|
||||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||||
</div>
|
</CellWithTooltip>
|
||||||
</td>
|
</td>
|
||||||
))}
|
))}
|
||||||
</tr>
|
</tr>
|
||||||
|
|
@ -134,7 +190,11 @@ export function DataTable<T>({
|
||||||
'relative px-3 py-2 text-xs font-medium text-text-secondary uppercase tracking-wider text-left',
|
'relative px-3 py-2 text-xs font-medium text-text-secondary uppercase tracking-wider text-left',
|
||||||
header.column.getCanSort() && 'cursor-pointer select-none hover:text-text-primary'
|
header.column.getCanSort() && 'cursor-pointer select-none hover:text-text-primary'
|
||||||
)}
|
)}
|
||||||
style={{ width: header.getSize() }}
|
style={{
|
||||||
|
width: `${header.getSize()}px`,
|
||||||
|
minWidth: `${header.getSize()}px`,
|
||||||
|
maxWidth: `${header.getSize()}px`,
|
||||||
|
}}
|
||||||
onClick={header.column.getToggleSortingHandler()}
|
onClick={header.column.getToggleSortingHandler()}
|
||||||
>
|
>
|
||||||
{header.isPlaceholder ? null : (
|
{header.isPlaceholder ? null : (
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@ export function ExchangesTable() {
|
||||||
accessorKey: 'id',
|
accessorKey: 'id',
|
||||||
size: 80,
|
size: 80,
|
||||||
cell: ({ getValue }) => (
|
cell: ({ getValue }) => (
|
||||||
<span className="font-mono text-primary-400 text-xs">
|
<span className="font-mono text-primary-400 text-xs block truncate">
|
||||||
{getValue() as string}
|
{getValue() as string}
|
||||||
</span>
|
</span>
|
||||||
),
|
),
|
||||||
|
|
@ -112,7 +112,7 @@ export function ExchangesTable() {
|
||||||
accessorKey: 'code',
|
accessorKey: 'code',
|
||||||
size: 100,
|
size: 100,
|
||||||
cell: ({ getValue }) => (
|
cell: ({ getValue }) => (
|
||||||
<span className="font-mono text-text-primary text-sm font-medium">
|
<span className="font-mono text-text-primary text-sm font-medium block truncate">
|
||||||
{getValue() as string}
|
{getValue() as string}
|
||||||
</span>
|
</span>
|
||||||
),
|
),
|
||||||
|
|
@ -130,7 +130,6 @@ export function ExchangesTable() {
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
style={{ width: cell.column.getSize() }}
|
|
||||||
value={editValue}
|
value={editValue}
|
||||||
onChange={e => setEditValue(e.target.value)}
|
onChange={e => setEditValue(e.target.value)}
|
||||||
onBlur={() => handleCellEdit(row.original.id, 'name', editValue)}
|
onBlur={() => handleCellEdit(row.original.id, 'name', editValue)}
|
||||||
|
|
@ -150,11 +149,12 @@ export function ExchangesTable() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="cursor-pointer hover:bg-surface-secondary rounded px-2 py-1 transition-colors text-sm"
|
className="cursor-pointer hover:bg-surface-secondary rounded px-2 py-1 transition-colors text-sm truncate overflow-hidden text-ellipsis whitespace-nowrap"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setEditingCell({ id: row.original.id, field: 'name' });
|
setEditingCell({ id: row.original.id, field: 'name' });
|
||||||
setEditValue(getValue() as string);
|
setEditValue(getValue() as string);
|
||||||
}}
|
}}
|
||||||
|
title={getValue() as string}
|
||||||
>
|
>
|
||||||
{getValue() as string}
|
{getValue() as string}
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -167,7 +167,7 @@ export function ExchangesTable() {
|
||||||
accessorKey: 'country',
|
accessorKey: 'country',
|
||||||
size: 80,
|
size: 80,
|
||||||
cell: ({ getValue }) => (
|
cell: ({ getValue }) => (
|
||||||
<span className="text-text-secondary text-sm">{getValue() as string}</span>
|
<span className="text-text-secondary text-sm block truncate">{getValue() as string}</span>
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -175,11 +175,8 @@ export function ExchangesTable() {
|
||||||
header: 'Currency',
|
header: 'Currency',
|
||||||
accessorKey: 'currency',
|
accessorKey: 'currency',
|
||||||
size: 80,
|
size: 80,
|
||||||
cell: ({ getValue, cell }) => (
|
cell: ({ getValue }) => (
|
||||||
<span
|
<span className="font-mono text-text-secondary text-sm block truncate">
|
||||||
className="font-mono text-text-secondary text-sm"
|
|
||||||
style={{ width: cell.column.getSize() }}
|
|
||||||
>
|
|
||||||
{getValue() as string}
|
{getValue() as string}
|
||||||
</span>
|
</span>
|
||||||
),
|
),
|
||||||
|
|
@ -189,13 +186,10 @@ export function ExchangesTable() {
|
||||||
header: 'Active',
|
header: 'Active',
|
||||||
accessorKey: 'active',
|
accessorKey: 'active',
|
||||||
size: 80,
|
size: 80,
|
||||||
cell: ({ getValue, row, cell }) => {
|
cell: ({ getValue, row }) => {
|
||||||
const isActive = getValue() as boolean;
|
const isActive = getValue() as boolean;
|
||||||
return (
|
return (
|
||||||
<label
|
<label className="relative inline-flex items-center cursor-pointer">
|
||||||
className="relative inline-flex items-center cursor-pointer"
|
|
||||||
style={{ width: cell.column.getSize() }}
|
|
||||||
>
|
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
checked={isActive}
|
checked={isActive}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue