From 288734f37887694d16752421c87599544543f1ff Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Fri, 24 Jul 2026 15:45:24 +0900 Subject: [PATCH] =?UTF-8?q?260724=5F01=5FENH:=20KriAutoComplete=20?= =?UTF-8?q?=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20Datatable?= =?UTF-8?q?=20pined=20row=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- registry/components/KriAutoComplete.tsx | 5 + registry/components/data-table/DataTable.tsx | 121 +++++++++++++++--- .../components/data-table/DataTable.type.ts | 3 + 4 files changed, 113 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 294cf85..ba09e3c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kri-ui-registry", - "version": "0.9.1", + "version": "0.9.2", "private": true, "description": "KRI internal shadcn UI registry", "scripts": { diff --git a/registry/components/KriAutoComplete.tsx b/registry/components/KriAutoComplete.tsx index eed5189..fd8380a 100644 --- a/registry/components/KriAutoComplete.tsx +++ b/registry/components/KriAutoComplete.tsx @@ -24,6 +24,7 @@ export interface KriAutoCompleteBaseProps { withInfo?: boolean disabled?: boolean 'aria-invalid'?: boolean + modalPopover?: boolean } export interface KriMultiSelectProps extends KriAutoCompleteBaseProps { @@ -73,6 +74,7 @@ export function KriMultiSelect({ onOptionsChange, disabled, 'aria-invalid': ariaInvalid, + modalPopover = true, }: KriMultiSelectProps) { const options = React.useMemo( () => toOptions(items, getValue, getLabel, withInfo), @@ -104,6 +106,7 @@ export function KriMultiSelect({ deduplicateOptions disabled={disabled} aria-invalid={ariaInvalid} + modalPopover={modalPopover} /> ) @@ -149,6 +152,7 @@ function KriSingleSelectInner({ clearValue = '__CLEAR__', disabled, 'aria-invalid': ariaInvalid, + modalPopover = true, }: KriSingleSelectProps, forwardedRef: React.ForwardedRef) { const ref = React.useRef(null) React.useImperativeHandle( @@ -217,6 +221,7 @@ function KriSingleSelectInner({ disabled={disabled} aria-invalid={ariaInvalid} title={showSelectedTooltip ? selectedLabel ?? value : undefined} + modalPopover={modalPopover} /> ) diff --git a/registry/components/data-table/DataTable.tsx b/registry/components/data-table/DataTable.tsx index b9abef9..e1ef49b 100644 --- a/registry/components/data-table/DataTable.tsx +++ b/registry/components/data-table/DataTable.tsx @@ -1,7 +1,7 @@ 'use client' -import { useCallback, useEffect, useMemo, useState } from 'react' -import type { OnChangeFn, PaginationState, Row, RowSelectionState, SortingState } from '@tanstack/react-table' +import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react' +import type { OnChangeFn, PaginationState, Row, RowPinningState, RowSelectionState, SortingState } from '@tanstack/react-table' import { flexRender, getCoreRowModel, getPaginationRowModel, getSortedRowModel, useReactTable } from '@tanstack/react-table' import { ArrowDown, ArrowUp, ArrowUpDown } from 'lucide-react' import { Button } from '@/components/ui/button' @@ -12,6 +12,8 @@ import type { DataTableColumn, DataTableProps } from './DataTable.type' import { DataTablePagination } from './DataTablePagination' import { usePreventClickOnDrag } from './hooks/usePreventClickOnDrag' +const EMPTY_PINNED_ROWS: never[] = [] + export function DataTable({ columns, data, @@ -32,6 +34,9 @@ export function DataTable({ emptyText = '데이터가 없습니다.', className, maxBodyHeight, + disablePagination = false, + topPinnedRows = EMPTY_PINNED_ROWS, + bottomPinnedRows = EMPTY_PINNED_ROWS, }: Readonly>) { const [internalPagination, setInternalPagination] = useState({ pageIndex: 0, pageSize: defaultPageSize }) const [sorting, setSorting] = useState([]) @@ -51,7 +56,7 @@ export function DataTable({ aria-label="전체 선택" /> ) : null, - cell: ({ row }) => ( + cell: ({ row }) => row.getIsPinned() ? null : ( { @@ -92,22 +97,43 @@ export function DataTable({ [isClient, onServerPaginationChange], ) + const combinedData = useMemo( + () => [...topPinnedRows, ...data, ...bottomPinnedRows], + [topPinnedRows, data, bottomPinnedRows], + ) + + const internalGetRowId = useCallback((row: TData, index: number) => { + if (index < topPinnedRows.length) return `__top-pinned-${index}` + if (index >= topPinnedRows.length + data.length) { + return `__bottom-pinned-${index - topPinnedRows.length - data.length}` + } + const originalIndex = index - topPinnedRows.length + return getRowId ? getRowId(data[originalIndex], originalIndex) : String(originalIndex) + }, [topPinnedRows.length, data, getRowId]) + + const rowPinning = useMemo(() => ({ + top: topPinnedRows.map((_, i) => `__top-pinned-${i}`), + bottom: bottomPinnedRows.map((_, i) => `__bottom-pinned-${i}`), + }), [topPinnedRows, bottomPinnedRows]) + const table = useReactTable({ - data, + data: combinedData, columns: enhancedColumns, pageCount: isClient ? undefined : serverPageCount, - state: { sorting, pagination, rowSelection }, - enableRowSelection: selectionEnabled, + state: { sorting, pagination, rowSelection, rowPinning }, + enableRowSelection: (row) => selectionEnabled && !row.getIsPinned(), + enableRowPinning: true, + keepPinnedRows: true, onRowSelectionChange: setRowSelection, onSortingChange: setSorting, onPaginationChange: handlePaginationChange, getCoreRowModel: getCoreRowModel(), getSortedRowModel: isClient ? getSortedRowModel() : undefined, - getPaginationRowModel: isClient ? getPaginationRowModel() : undefined, + getPaginationRowModel: isClient && !disablePagination ? getPaginationRowModel() : undefined, manualPagination: !isClient, manualSorting: !isClient, enableSortingRemoval: false, - ...(getRowId ? { getRowId } : {}), + getRowId: internalGetRowId, }) useEffect(() => { @@ -135,15 +161,50 @@ export function DataTable({ const totalCount = isClient ? data.length : (serverTotalCount ?? 0) + const topRows = table.getTopRows() + const centerRows = table.getCenterRows() + const bottomRows = table.getBottomRows() + + const scrollContainerRef = useRef(null) + const [topOffsets, setTopOffsets] = useState([]) + const [bottomOffsets, setBottomOffsets] = useState([]) + + useLayoutEffect(() => { + if (!maxBodyHeight) return + const container = scrollContainerRef.current + if (!container) return + + const headerHeight = container.querySelector('thead')?.getBoundingClientRect().height ?? 0 + const bodyRows = Array.from(container.querySelectorAll('tbody > tr')) + const topRowEls = bodyRows.slice(0, topRows.length) + const bottomRowEls = bottomRows.length ? bodyRows.slice(-bottomRows.length) : [] + + let topAcc = headerHeight + setTopOffsets(topRowEls.map((element) => { + const offset = topAcc + topAcc += element.getBoundingClientRect().height + return offset + })) + + let bottomAcc = 0 + const offsets: number[] = [] + for (let index = bottomRowEls.length - 1; index >= 0; index -= 1) { + offsets[index] = bottomAcc + bottomAcc += bottomRowEls[index].getBoundingClientRect().height + } + setBottomOffsets(offsets) + }, [maxBodyHeight, topRows.length, bottomRows.length, centerRows.length]) + return (
- + {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { @@ -170,7 +231,19 @@ export function DataTable({ ))} - {table.getRowModel().rows.length ? table.getRowModel().rows.map((row) => ( + {topRows.map((row, index) => ( + td]:px-2 [&>td]:py-1.5', + maxBodyHeight && 'sticky z-10', + )} + style={maxBodyHeight ? { top: topOffsets[index] ?? 0 } : undefined} + > + {row.getVisibleCells().map((cell) => {flexRender(cell.column.columnDef.cell, cell.getContext())})} + + ))} + {centerRows.length ? centerRows.map((row) => ( ({ )) : ( {emptyText} )} + {bottomRows.map((row, index) => ( + td]:px-2 [&>td]:py-1.5', + maxBodyHeight && 'sticky z-10', + )} + style={maxBodyHeight ? { bottom: bottomOffsets[index] ?? 0 } : undefined} + > + {row.getVisibleCells().map((cell) => {flexRender(cell.column.columnDef.cell, cell.getContext())})} + + ))}
- + {!disablePagination && ( + + )}
) } diff --git a/registry/components/data-table/DataTable.type.ts b/registry/components/data-table/DataTable.type.ts index 0fe4cf0..4bf2390 100644 --- a/registry/components/data-table/DataTable.type.ts +++ b/registry/components/data-table/DataTable.type.ts @@ -35,4 +35,7 @@ export interface DataTableProps { emptyText?: string className?: string maxBodyHeight?: string + disablePagination?: boolean + topPinnedRows?: TData[] + bottomPinnedRows?: TData[] }