1- import * as React from 'react' ;
2- import { useCallback } from 'react' ;
1+ import { Button } from '@/components/ui/button' ;
32import { cn } from '@/lib/cn' ;
43import { flexRender , Header , RowData } from '@tanstack/react-table' ;
5- import { Button } from '@/components/ui/button' ;
6- import { ArrowDown , ArrowUp , ArrowUpDown } from 'lucide-react' ;
4+ import { ArrowDown , ArrowUp , ArrowUpDown , GripVerticalIcon } from 'lucide-react' ;
5+ import * as React from 'react' ;
6+ import { useCallback } from 'react' ;
77
88export interface TableProps extends React . ComponentProps < 'table' > {
99 containerClassName ?: string ;
@@ -54,7 +54,11 @@ export function TableHead({ className, ...props }: React.ComponentProps<'th'>) {
5454 ) ;
5555}
5656
57- export function TableHeadSortable < TData extends RowData > ( { header, onColumnClick, ...props } : React . ComponentProps < 'th' > & {
57+ export function TableHeadSortable < TData extends RowData > ( {
58+ header,
59+ onColumnClick,
60+ ...props
61+ } : React . ComponentProps < 'th' > & {
5862 header : Header < TData , unknown > ;
5963 onColumnClick ?: ( accessorKey : string , willSortByAscending : boolean ) => void ;
6064} ) {
@@ -64,26 +68,53 @@ export function TableHeadSortable<TData extends RowData>({ header, onColumnClick
6468 // @ts -expect-error The accessorKey isn't accessible.
6569 onColumnClick ?.( header . column . columnDef . accessorKey , willSortByAscending ) ;
6670 } , [ header , onColumnClick ] ) ;
67- if ( header . column . columnDef . enableSorting ) {
68- return < TableHead { ...props } className = "px-0" >
69- < Button
70- type = "button"
71- variant = "ghost"
72- className = { cn ( 'rounded-none' , ! header . column . getIsSorted ( ) || header . column . getIsSorted ( ) === 'asc' ? 'cursor-n-resize' : 'cursor-s-resize' ) }
73- onClick = { onClickSort } >
74- { header . isPlaceholder ? null : flexRender ( header . column . columnDef . header , header . getContext ( ) ) }
75- { header . column . getIsSorted ( ) === 'asc'
76- ? < ArrowUp />
77- : header . column . getIsSorted ( ) === 'desc'
78- ? < ArrowDown />
79- : < ArrowUpDown className = "text-gray-600" /> }
80- </ Button >
81- </ TableHead > ;
82- } else {
83- return < TableHead { ...props } className = "px-2" >
84- { header . isPlaceholder ? null : flexRender ( header . column . columnDef . header , header . getContext ( ) ) }
85- </ TableHead > ;
86- }
71+ const enableSorting = header . column . columnDef . enableSorting ;
72+ const enableResizing = header . column . columnDef . enableResizing ;
73+ const content = header . isPlaceholder ? null : flexRender ( header . column . columnDef . header , header . getContext ( ) ) ;
74+ const resetSize = useCallback ( ( ) => {
75+ header . column . resetSize ( ) ;
76+ } , [ header ] ) ;
77+ return < TableHead
78+ { ...props }
79+ style = { { width : `${ header . getSize ( ) } px` } }
80+ className = { enableSorting ? 'px-0' : 'px-2' }
81+ >
82+ < div className = "flex items-center justify-between" >
83+ { enableSorting ? (
84+ < Button
85+ type = "button"
86+ variant = "ghost"
87+ className = { cn ( 'rounded-none' , ! header . column . getIsSorted ( ) || header . column . getIsSorted ( ) === 'asc' ? 'cursor-n-resize' : 'cursor-s-resize' ) }
88+ onClick = { onClickSort } >
89+ { content }
90+ { header . column . getIsSorted ( ) === 'asc'
91+ ? < ArrowUp />
92+ : header . column . getIsSorted ( ) === 'desc'
93+ ? < ArrowDown />
94+ : < ArrowUpDown className = "text-gray-600" /> }
95+ </ Button >
96+ ) : (
97+ content
98+ ) }
99+ { enableResizing && (
100+ < Button
101+ type = "button"
102+ variant = "ghost"
103+ className = "cursor-col-resize"
104+ onMouseDown = { header . getResizeHandler ( ) } // for desktop
105+ onTouchStart = { header . getResizeHandler ( ) } // for mobile
106+ onDoubleClick = { resetSize }
107+ style = { {
108+ transform : header . column . getIsResizing ( )
109+ ? `translateX(${ header . getContext ( ) . table . getState ( ) . columnSizingInfo . deltaOffset } px)`
110+ : '' ,
111+ } }
112+ >
113+ < GripVerticalIcon />
114+ </ Button >
115+ ) }
116+ </ div >
117+ </ TableHead > ;
87118}
88119
89120export function TableCell ( { className, ...props } : React . ComponentProps < 'td' > ) {
0 commit comments