Skip to content

Commit 5620538

Browse files
committed
feat: add style to pin column
1 parent acdead1 commit 5620538

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

packages/react/src/react/components/primitives/tanstack-table.tsx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use client'
22

3-
import React, { useRef } from 'react'
3+
import React, { type CSSProperties, useRef } from 'react'
44

55
import { CircleNotchIcon, WarningCircleIcon, WarningIcon } from '@phosphor-icons/react'
66
import { CaretDownIcon, CaretUpDownIcon, CaretUpIcon } from '@phosphor-icons/react/dist/ssr'
77
import {
8+
type Column,
89
flexRender,
910
type Row,
1011
type SortDirection,
@@ -41,6 +42,28 @@ export interface TanstackTableProps<T> {
4142
}
4243
}
4344

45+
const getCommonPinningClassesAndStyle = (column: Column<any>) => {
46+
const isPinned = column.getIsPinned()
47+
const isLastLeftPinnedColumn = isPinned === 'left' && column.getIsLastColumn('left')
48+
const isFirstRightPinnedColumn = isPinned === 'right' && column.getIsFirstColumn('right')
49+
50+
const className = clsx(
51+
isPinned ? 'sticky z-[1]' : 'relative',
52+
isLastLeftPinnedColumn && 'shadow-[inset_-4px_0_4px_-4px_gray]',
53+
isFirstRightPinnedColumn && 'shadow-[inset_4px_0_4px_-4px_gray]'
54+
)
55+
56+
const style: CSSProperties = {
57+
left: isPinned === 'left' ? `${column.getStart('left')}px` : undefined,
58+
right: isPinned === 'right' ? `${column.getAfter('right')}px` : undefined,
59+
width: column.getSize(),
60+
minWidth: column.getSize(),
61+
maxWidth: column.getSize(),
62+
}
63+
64+
return { className, style }
65+
}
66+
4467
export const getSortIcon = (isSorted: false | SortDirection) => {
4568
switch (isSorted) {
4669
case false:
@@ -92,15 +115,19 @@ export function TanstackTable<T>({
92115
const canSort = configuration?.sortBy?.some(
93116
([columnPath]) => columnPath === normalizeColumnId(header.column.id)
94117
)
118+
const { className: pinnedHeaderClassName, style: pinnedHeaderStyle } =
119+
getCommonPinningClassesAndStyle(header.column)
95120
return (
96121
<TableHead
97122
key={normalizeColumnId(header.id)}
98123
className={clsx(
99124
'focus-visible:ring-focus ring-inset',
100125
header.colSpan > 1 && 'border-bluegray-300 border-b',
101126
classNames?.tableHead,
102-
header.column.columnDef.meta?.thClassName
127+
header.column.columnDef.meta?.thClassName,
128+
pinnedHeaderClassName
103129
)}
130+
style={pinnedHeaderStyle}
104131
onClick={
105132
canSort && children ? header.column.getToggleSortingHandler() : undefined
106133
}
@@ -138,10 +165,13 @@ export function TanstackTable<T>({
138165
className={clsx('border-b border-border last:border-b-0', classNames?.tableBodyRow)}
139166
>
140167
{row.getVisibleCells().map((cell) => {
168+
const { className: pinnedCellClassName, style: pinnedCellStyle } =
169+
getCommonPinningClassesAndStyle(cell.column)
141170
return (
142171
<TableCell
143172
key={cell.id}
144-
className={clsx(classNames?.tableCell)}
173+
className={clsx(classNames?.tableCell, pinnedCellClassName)}
174+
style={pinnedCellStyle}
145175
onClick={(e) => onRowClick?.(row, e)}
146176
>
147177
{flexRender(cell.column.columnDef.cell, cell.getContext())}

0 commit comments

Comments
 (0)