'use client' import { throttle } from 'es-toolkit'; import { Box, css, sx, type BoxProps } from '@hanzo/ui' import * as React from 'react'; import { cn } from './utils'; import { Link } from './link'; // Inline east arrow icon const ArrowIcon = (props: React.SVGProps) => ( ); // --------------------------------------------------------------------------- // Chakra-style prop adapter // Consumers pass Chakra shorthand style props (width, minW, pr, etc.) directly // to table components. This helper converts them to a React CSSProperties object // so we can apply them as inline styles without requiring changes to all callers. // --------------------------------------------------------------------------- interface ChakraStyleProps { width?: React.CSSProperties['width']; w?: React.CSSProperties['width']; minWidth?: React.CSSProperties['minWidth']; minW?: React.CSSProperties['minWidth']; maxWidth?: React.CSSProperties['maxWidth']; maxW?: React.CSSProperties['maxWidth']; height?: React.CSSProperties['height']; h?: React.CSSProperties['height']; textAlign?: React.CSSProperties['textAlign']; verticalAlign?: React.CSSProperties['verticalAlign']; whiteSpace?: React.CSSProperties['whiteSpace']; wordBreak?: React.CSSProperties['wordBreak']; textTransform?: React.CSSProperties['textTransform']; fontSize?: React.CSSProperties['fontSize']; lineHeight?: React.CSSProperties['lineHeight']; animation?: React.CSSProperties['animation']; tableLayout?: React.CSSProperties['tableLayout']; p?: number | string; px?: number | string; py?: number | string; pr?: number | string; pl?: number | string; pt?: number | string; pb?: number | string; m?: number | string; mx?: number | string; my?: number | string; mr?: number | string; ml?: number | string; mt?: number | string; mb?: number | string; position?: React.CSSProperties['position']; top?: number | string; left?: number | string; right?: number | string; bottom?: number | string; zIndex?: React.CSSProperties['zIndex']; backgroundColor?: React.CSSProperties['backgroundColor'] | Record; boxShadow?: React.CSSProperties['boxShadow']; alignItems?: React.CSSProperties['alignItems']; fontWeight?: React.CSSProperties['fontWeight']; color?: React.CSSProperties['color'] | string; overflow?: React.CSSProperties['overflow']; borderBottomStyle?: React.CSSProperties['borderBottomStyle']; borderRadius?: React.CSSProperties['borderRadius']; display?: React.CSSProperties['display']; // Chakra pseudo-prop pass-throughs (ignored in Tailwind) _first?: Record; _last?: Record; } const SPACING_SCALE = 4; // 1 unit = 4px (Chakra default) function toPixels(value: number | string | undefined): string | undefined { if (value === undefined) return undefined; if (typeof value === 'string') return value; return `${ value * SPACING_SCALE }px`; } function extractStyles(props: Record): { style: React.CSSProperties; rest: Record } { const style: React.CSSProperties = {}; const rest: Record = {}; for (const [ key, value ] of Object.entries(props)) { if (value === undefined) continue; switch (key) { case 'width': case 'w': style.width = value as React.CSSProperties['width']; break; case 'minWidth': case 'minW': style.minWidth = value as React.CSSProperties['minWidth']; break; case 'maxWidth': case 'maxW': style.maxWidth = value as React.CSSProperties['maxWidth']; break; case 'height': case 'h': style.height = value as React.CSSProperties['height']; break; case 'textAlign': style.textAlign = value as React.CSSProperties['textAlign']; break; case 'verticalAlign': style.verticalAlign = value as React.CSSProperties['verticalAlign']; break; case 'whiteSpace': style.whiteSpace = value as React.CSSProperties['whiteSpace']; break; case 'wordBreak': style.wordBreak = value as React.CSSProperties['wordBreak']; break; case 'textTransform': style.textTransform = value as React.CSSProperties['textTransform']; break; case 'fontSize': style.fontSize = value as React.CSSProperties['fontSize']; break; case 'lineHeight': style.lineHeight = value as React.CSSProperties['lineHeight']; break; case 'animation': style.animation = value as React.CSSProperties['animation']; break; case 'tableLayout': style.tableLayout = value as React.CSSProperties['tableLayout']; break; case 'p': style.padding = toPixels(value as number | string); break; case 'px': { const px = toPixels(value as number | string); style.paddingLeft = px; style.paddingRight = px; break; } case 'py': { const py = toPixels(value as number | string); style.paddingTop = py; style.paddingBottom = py; break; } case 'pr': style.paddingRight = toPixels(value as number | string); break; case 'pl': style.paddingLeft = toPixels(value as number | string); break; case 'pt': style.paddingTop = toPixels(value as number | string); break; case 'pb': style.paddingBottom = toPixels(value as number | string); break; case 'm': style.margin = toPixels(value as number | string); break; case 'mx': { const mx = toPixels(value as number | string); style.marginLeft = mx; style.marginRight = mx; break; } case 'my': { const my = toPixels(value as number | string); style.marginTop = my; style.marginBottom = my; break; } case 'mr': style.marginRight = toPixels(value as number | string); break; case 'ml': style.marginLeft = toPixels(value as number | string); break; case 'mt': style.marginTop = toPixels(value as number | string); break; case 'mb': style.marginBottom = toPixels(value as number | string); break; case 'position': style.position = value as React.CSSProperties['position']; break; case 'top': style.top = typeof value === 'number' ? `${ value }px` : value as string; break; case 'left': style.left = typeof value === 'number' ? `${ value }px` : value as string; break; case 'right': style.right = typeof value === 'number' ? `${ value }px` : value as string; break; case 'bottom': style.bottom = typeof value === 'number' ? `${ value }px` : value as string; break; case 'zIndex': style.zIndex = value as React.CSSProperties['zIndex']; break; case 'backgroundColor': // Skip Chakra responsive objects like { _light: ..., _dark: ... } if (typeof value === 'string') { style.backgroundColor = value; } break; case 'boxShadow': style.boxShadow = value as React.CSSProperties['boxShadow']; break; case 'alignItems': style.alignItems = value as React.CSSProperties['alignItems']; break; case 'fontWeight': style.fontWeight = value as React.CSSProperties['fontWeight']; break; case 'color': if (typeof value === 'string') { style.color = value; } break; case 'overflow': style.overflow = value as React.CSSProperties['overflow']; break; case 'borderBottomStyle': style.borderBottomStyle = value as React.CSSProperties['borderBottomStyle']; break; case 'borderRadius': style.borderRadius = value as React.CSSProperties['borderRadius']; break; case 'display': style.display = value as React.CSSProperties['display']; break; // Chakra pseudo-props: silently drop case '_first': case '_last': break; default: rest[key] = value; break; } } return { style, rest }; } // --------------------------------------------------------------------------- // Responsive prop helper // Consumers pass Chakra responsive objects like { base: '1200px', lg: '1000px' }. // Convert them to inline style using the base value (mobile-first). // --------------------------------------------------------------------------- function resolveResponsive(value: unknown): string | undefined { if (value === undefined || value === null) return undefined; if (typeof value === 'string') return value; if (typeof value === 'number') return `${ value }px`; if (typeof value === 'object' && value !== null && 'base' in value) { return (value as Record).base; } return undefined; } // --------------------------------------------------------------------------- // TableRoot // --------------------------------------------------------------------------- export interface TableRootProps extends Omit, Omit, 'color'> { children?: React.ReactNode; minWidth?: React.CSSProperties['minWidth'] | Record; minW?: React.CSSProperties['minWidth'] | Record; } export const TableRoot = React.forwardRef( function TableRoot(props, ref) { const { className, style: styleProp, children, ...other } = props; // Handle responsive minWidth/minW const rawMinW = other.minWidth ?? other.minW; const resolvedMinW = resolveResponsive(rawMinW); if (resolvedMinW) { other.minWidth = resolvedMinW; delete other.minW; } const { style: extracted, rest } = extractStyles(other as Record); const merged = { ...extracted, ...styleProp }; return ( ) } > { children } ); }, ); // --------------------------------------------------------------------------- // TableHeader (thead) // --------------------------------------------------------------------------- export interface TableHeaderProps extends ChakraStyleProps, Omit, 'color'> { children?: React.ReactNode; } export const TableHeader = React.forwardRef( function TableHeader(props, ref) { const { className, style: styleProp, children, ...other } = props; const { style: extracted, rest } = extractStyles(other as Record); const merged = { ...extracted, ...styleProp }; return ( ) } > { children } ); }, ); // --------------------------------------------------------------------------- // TableBody (tbody) // --------------------------------------------------------------------------- export interface TableBodyProps extends ChakraStyleProps, Omit, 'color'> { children?: React.ReactNode; } export const TableBody = React.forwardRef( function TableBody(props, ref) { const { className, style: styleProp, children, ...other } = props; const { style: extracted, rest } = extractStyles(other as Record); const merged = { ...extracted, ...styleProp }; return ( ) } > { children } ); }, ); // --------------------------------------------------------------------------- // TableRow (tr) // --------------------------------------------------------------------------- export interface TableRowProps extends ChakraStyleProps, Omit, 'color'> { children?: React.ReactNode; } export const TableRow = React.forwardRef( function TableRow(props, ref) { const { className, style: styleProp, children, ...other } = props; const { style: extracted, rest } = extractStyles(other as Record); const merged = { ...extracted, ...styleProp }; return ( ) } > { children } ); }, ); // --------------------------------------------------------------------------- // TableCell (td) // --------------------------------------------------------------------------- export interface TableCellProps extends Omit, Omit, 'color'> { isNumeric?: boolean; children?: React.ReactNode; display?: string; justifyContent?: string; } export const TableCell = React.forwardRef( function TableCell(props, ref) { const { isNumeric, className, style: styleProp, children, ...other } = props; const { style: extracted, rest } = extractStyles(other as Record); const merged = { ...(isNumeric ? { textAlign: 'right' as const } : {}), ...extracted, ...styleProp, }; return ( ) } > { children } ); }, ); // --------------------------------------------------------------------------- // TableColumnHeader (th) // --------------------------------------------------------------------------- export interface TableColumnHeaderProps extends ChakraStyleProps, Omit, 'color'> { isNumeric?: boolean; children?: React.ReactNode; } export const TableColumnHeader = React.forwardRef( function TableColumnHeader(props, ref) { const { isNumeric, className, style: styleProp, children, ...other } = props; const { style: extracted, rest } = extractStyles(other as Record); const merged = { ...(isNumeric ? { textAlign: 'right' as const } : {}), ...extracted, ...styleProp, }; return ( ) } > { children } ); }, ); // --------------------------------------------------------------------------- // TableColumnHeaderSortable // --------------------------------------------------------------------------- export interface TableColumnHeaderSortableProps extends TableColumnHeaderProps { sortField: F; sortValue: string; onSortToggle: (sortField: F) => void; disabled?: boolean; indicatorPosition?: 'left' | 'right'; contentAfter?: React.ReactNode; } export const TableColumnHeaderSortable = (props: TableColumnHeaderSortableProps) => { const { sortField, sortValue, onSortToggle, children, disabled, indicatorPosition = 'left', contentAfter, ...rest } = props; const handleSortToggle = React.useCallback(() => { onSortToggle(sortField); }, [ onSortToggle, sortField ]); const isActive = sortValue.includes(sortField); const isAsc = sortValue.toLowerCase().includes('asc'); return ( { isActive && ( ) } { children } { contentAfter } ); }; // --------------------------------------------------------------------------- // TableHeaderSticky // --------------------------------------------------------------------------- const ACTION_BAR_SHADOW = '0 4px 4px -4px rgb(0 0 0 / 10%), 0 2px 4px -4px rgb(0 0 0 / 6%)'; export const TableHeaderSticky = (props: TableHeaderProps) => { const { top, children, className, style: styleProp, ...rest } = props; const ref = React.useRef(null); const [ isStuck, setIsStuck ] = React.useState(false); const handleScroll = React.useCallback(() => { if (Number(ref.current?.getBoundingClientRect().y) <= (Number(top) || 0)) { setIsStuck(true); } else { setIsStuck(false); } }, [ top ]); React.useEffect(() => { const throttledHandleScroll = throttle(handleScroll, 300); window.addEventListener('scroll', throttledHandleScroll); return () => { window.removeEventListener('scroll', throttledHandleScroll); }; }, [ handleScroll ]); return ( { children } ); }; // --------------------------------------------------------------------------- // TableCaption // --------------------------------------------------------------------------- export interface TableCaptionProps extends ChakraStyleProps, Omit, 'color'> { children?: React.ReactNode; } export const TableCaption = React.forwardRef( function TableCaption(props, ref) { const { className, style: styleProp, children, ...other } = props; const { style: extracted, rest } = extractStyles(other as Record); const merged = { ...extracted, ...styleProp }; return ( ) } > { children } ); }, ); // --------------------------------------------------------------------------- // TableFooter (tfoot) // --------------------------------------------------------------------------- export interface TableFooterProps extends ChakraStyleProps, Omit, 'color'> { children?: React.ReactNode; } export const TableFooter = React.forwardRef( function TableFooter(props, ref) { const { className, style: styleProp, children, ...other } = props; const { style: extracted, rest } = extractStyles(other as Record); const merged = { ...extracted, ...styleProp }; return ( ) } > { children } ); }, ); // --------------------------------------------------------------------------- // TableScrollWrapper // --------------------------------------------------------------------------- export interface TableScrollWrapperProps extends React.HTMLAttributes { children?: React.ReactNode; } export const TableScrollWrapper = React.forwardRef( function TableScrollWrapper(props, ref) { const { className, children, ...rest } = props; return ( { children } ); }, );