251210_01_ENH : 프로젝트 수정, 에디터 구현중
This commit is contained in:
@@ -30,6 +30,7 @@ import { usePagination } from './hooks/usePagination'
|
||||
import { DataTableProps, DataTableColumn } from './DataTable.type'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
import {usePreventClickOnDrag} from "@/components/data-table/hooks/usePreventClickOnDrag";
|
||||
|
||||
export function DataTable<TData>({
|
||||
columns,
|
||||
@@ -42,8 +43,8 @@ export function DataTable<TData>({
|
||||
onPaginationChange: onServerPaginationChange,
|
||||
onSortingChange: onServerSortingChange,
|
||||
selectMode = 'none',
|
||||
onRowClick,
|
||||
onRowSelectionChange,
|
||||
enableRowClickSelection = false,
|
||||
defaultPageSize = 10,
|
||||
pageSizeOptions = [5, 10, 25, 50],
|
||||
paginationItemsToDisplay = 5
|
||||
@@ -60,6 +61,10 @@ export function DataTable<TData>({
|
||||
// Row selection state
|
||||
const [rowSelection, setRowSelection] = useState<RowSelectionState>({})
|
||||
|
||||
const drag = usePreventClickOnDrag(6);
|
||||
|
||||
const isNotSelection = !['none', 'click-event'].includes(selectMode);
|
||||
|
||||
// 서버 페이지네이션일 때 부모에게 알림
|
||||
useEffect(() => {
|
||||
if (!isClient && onServerPaginationChange) {
|
||||
@@ -76,7 +81,7 @@ export function DataTable<TData>({
|
||||
|
||||
// Row selection 변경 시 부모에게 알림
|
||||
useEffect(() => {
|
||||
if (selectMode !== 'none' && onRowSelectionChange) {
|
||||
if (isNotSelection && onRowSelectionChange) {
|
||||
const selectedRows = table
|
||||
.getSelectedRowModel()
|
||||
.rows.map(row => row.original)
|
||||
@@ -86,7 +91,7 @@ export function DataTable<TData>({
|
||||
|
||||
// selectMode에 따라 select column 추가
|
||||
const enhancedColumns: DataTableColumn<TData, any>[] = [
|
||||
...(selectMode !== 'none'
|
||||
...(isNotSelection
|
||||
? [
|
||||
{
|
||||
id: 'select',
|
||||
@@ -125,13 +130,23 @@ export function DataTable<TData>({
|
||||
|
||||
// Row 클릭 핸들러
|
||||
const handleRowClick = (row: any) => {
|
||||
if (!enableRowClickSelection || selectMode === 'none') return
|
||||
switch (selectMode) {
|
||||
case 'none':
|
||||
return
|
||||
|
||||
if (selectMode === 'single') {
|
||||
table.resetRowSelection()
|
||||
row.toggleSelected(true)
|
||||
} else {
|
||||
row.toggleSelected(!row.getIsSelected())
|
||||
case 'click-event':
|
||||
if (!onRowClick) return
|
||||
onRowClick(row.original)
|
||||
return
|
||||
|
||||
case 'single':
|
||||
table.resetRowSelection()
|
||||
row.toggleSelected(true)
|
||||
return
|
||||
|
||||
default: // 'multi' 등
|
||||
row.toggleSelected(!row.getIsSelected())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +159,7 @@ export function DataTable<TData>({
|
||||
pagination,
|
||||
rowSelection
|
||||
},
|
||||
enableRowSelection: selectMode !== 'none',
|
||||
enableRowSelection: isNotSelection,
|
||||
onRowSelectionChange: setRowSelection,
|
||||
onSortingChange: setSorting,
|
||||
onPaginationChange: setPagination,
|
||||
@@ -218,11 +233,15 @@ export function DataTable<TData>({
|
||||
<TableRow
|
||||
key={row.id}
|
||||
data-state={row.getIsSelected() && 'selected'}
|
||||
onClick={() => handleRowClick(row)}
|
||||
onPointerDown={drag.onPointerDown}
|
||||
onPointerMove={drag.onPointerMove}
|
||||
onPointerUp={drag.onPointerUp}
|
||||
onPointerCancel={drag.onPointerCancel}
|
||||
onClick={drag.wrapClick(() => handleRowClick(row))}
|
||||
className={cn(
|
||||
'content-text-base',
|
||||
'[&>td]:py-0 [&>td]:px-2',
|
||||
enableRowClickSelection && selectMode !== 'none' && 'cursor-pointer',
|
||||
selectMode !== 'none' && 'cursor-pointer',
|
||||
'data-[state=selected]:bg-slate-100 data-[state=selected]:font-semibold',
|
||||
shouldHighlight && 'bg-emerald-50',
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user