260706_01_ENH: DataTable 버그 수정 및 Date 관련 컴포넌트 추가
This commit is contained in:
@@ -78,7 +78,7 @@ export function KriMultiSelect<T = unknown>({
|
||||
)
|
||||
|
||||
return (
|
||||
<div className={cn('flex-none overflow-hidden', widthClassName, heightClassName)}>
|
||||
<div className={cn('flex-none', widthClassName, heightClassName)}>
|
||||
<MultiSelect<T>
|
||||
options={options}
|
||||
defaultValue={value}
|
||||
@@ -96,7 +96,7 @@ export function KriMultiSelect<T = unknown>({
|
||||
onSearchValueChange={onSearchValueChange}
|
||||
maxCount={maxCount}
|
||||
hideSelectAll={hideSelectAll}
|
||||
autoSize
|
||||
autoSize={!widthClassName}
|
||||
singleLine
|
||||
resetOnDefaultValueChange
|
||||
deduplicateOptions
|
||||
@@ -154,7 +154,7 @@ export function KriSingleSelect<T = unknown>({
|
||||
const selectedLabel = options.find((option) => option.value === value)?.label
|
||||
|
||||
return (
|
||||
<div className={cn('flex-none overflow-hidden', widthClassName, heightClassName)}>
|
||||
<div className={cn('flex-none', widthClassName, heightClassName)}>
|
||||
<MultiSelect<T>
|
||||
ref={ref}
|
||||
options={options}
|
||||
@@ -194,7 +194,7 @@ export function KriSingleSelect<T = unknown>({
|
||||
preventDeselect={preventDeselect}
|
||||
hideOptionCheck={hideOptionCheck}
|
||||
hideSelectAll
|
||||
autoSize
|
||||
autoSize={!widthClassName}
|
||||
singleLine
|
||||
closeOnSelect
|
||||
resetOnDefaultValueChange
|
||||
|
||||
82
registry/components/KriDatePicker.tsx
Normal file
82
registry/components/KriDatePicker.tsx
Normal file
@@ -0,0 +1,82 @@
|
||||
"use client";
|
||||
|
||||
import {CalendarIcon, X} from "lucide-react";
|
||||
import {Button} from "@/components/ui/button";
|
||||
import {Calendar} from "@/components/ui/calendar";
|
||||
import {Popover, PopoverContent, PopoverTrigger} from "@/components/ui/popover";
|
||||
import {cn} from "@/lib/utils";
|
||||
|
||||
const parseDate = (value: string) => {
|
||||
if (!value) return undefined
|
||||
|
||||
const [year, month, day] = value.split('-').map(Number)
|
||||
if (!year || !month || !day) return undefined
|
||||
|
||||
const date = new Date(year, month - 1, day)
|
||||
if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return date
|
||||
}
|
||||
|
||||
const formatDate = (date: Date | undefined) => {
|
||||
if (!date) return ''
|
||||
|
||||
const year = date.getFullYear()
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
|
||||
return `${year}-${month}-${day}`
|
||||
}
|
||||
|
||||
export function KriDatePicker({
|
||||
value,
|
||||
onChange,
|
||||
placeholder = "날짜 선택",
|
||||
className,
|
||||
}: {
|
||||
value: string;
|
||||
onChange: (value: string) => void;
|
||||
placeholder?: string;
|
||||
className?: string;
|
||||
}) {
|
||||
const selected = parseDate(value);
|
||||
|
||||
return (
|
||||
<div className={cn("relative", className)}>
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
className={cn("h-9 w-full justify-start bg-white pr-8 text-left font-normal", !value && "text-muted-foreground")}
|
||||
>
|
||||
<CalendarIcon className="h-4 w-4" />
|
||||
{selected ? formatDate(selected) : placeholder}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-auto p-0" align="start">
|
||||
<Calendar
|
||||
mode="single"
|
||||
selected={selected}
|
||||
onSelect={(date) => onChange(date ? formatDate(date) : "")}
|
||||
/>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
{value && (
|
||||
<button
|
||||
type="button"
|
||||
className="absolute right-2 top-1/2 inline-flex h-5 w-5 -translate-y-1/2 items-center justify-center rounded-full text-muted-foreground hover:bg-muted hover:text-foreground"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
onChange("");
|
||||
}}
|
||||
aria-label="날짜 초기화"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
'use client'
|
||||
|
||||
import { useRef } from 'react'
|
||||
import { CalendarDays, X } from 'lucide-react'
|
||||
import { CalendarIcon, X } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Calendar } from '@/components/ui/calendar'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
@@ -23,6 +21,30 @@ export type KriDateRangePickerProps = {
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
const parseDate = (value: string) => {
|
||||
if (!value) return undefined
|
||||
|
||||
const [year, month, day] = value.split('-').map(Number)
|
||||
if (!year || !month || !day) return undefined
|
||||
|
||||
const date = new Date(year, month - 1, day)
|
||||
if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return date
|
||||
}
|
||||
|
||||
const formatDate = (date: Date | undefined) => {
|
||||
if (!date) return ''
|
||||
|
||||
const year = date.getFullYear()
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
|
||||
return `${year}-${month}-${day}`
|
||||
}
|
||||
|
||||
export function KriDateRangePicker({
|
||||
value,
|
||||
onChange,
|
||||
@@ -32,7 +54,11 @@ export function KriDateRangePicker({
|
||||
contentClassName,
|
||||
disabled = false,
|
||||
}: KriDateRangePickerProps) {
|
||||
const toInputRef = useRef<HTMLInputElement | null>(null)
|
||||
const selected = {
|
||||
from: parseDate(value.from),
|
||||
to: parseDate(value.to),
|
||||
}
|
||||
const selectedRange = selected.from || selected.to ? selected : undefined
|
||||
|
||||
const displayText = value.from && value.to
|
||||
? `${value.from} ~ ${value.to}`
|
||||
@@ -43,76 +69,52 @@ export function KriDateRangePicker({
|
||||
: placeholder
|
||||
|
||||
const hasValue = value.from.length > 0 || value.to.length > 0
|
||||
const invalidRange = value.from.length > 0 && value.to.length > 0 && value.from > value.to
|
||||
|
||||
const focusAndOpenToDate = () => {
|
||||
const input = toInputRef.current
|
||||
if (!input) return
|
||||
|
||||
input.focus()
|
||||
const inputWithPicker = input as HTMLInputElement & { showPicker?: () => void }
|
||||
inputWithPicker.showPicker?.()
|
||||
if (!inputWithPicker.showPicker) {
|
||||
input.click()
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn('flex items-center gap-2', className)}>
|
||||
<div className={cn('relative', className)}>
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={disabled}
|
||||
className={cn('justify-start min-w-[260px] font-normal', triggerClassName)}
|
||||
className={cn(
|
||||
'h-9 w-full justify-start bg-white pr-8 text-left font-normal',
|
||||
!hasValue && 'text-muted-foreground',
|
||||
triggerClassName,
|
||||
)}
|
||||
>
|
||||
<CalendarDays className="w-4 h-4" />
|
||||
<CalendarIcon className="h-4 w-4" />
|
||||
<span className="truncate">{displayText}</span>
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent align="start" className={cn('w-[360px] space-y-3', contentClassName)}>
|
||||
<Label className="font-semibold">조회 기간</Label>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
type="date"
|
||||
value={value.from}
|
||||
onChange={(e) => {
|
||||
const nextFrom = e.target.value
|
||||
onChange({ ...value, from: nextFrom })
|
||||
if (nextFrom) {
|
||||
requestAnimationFrame(() => {
|
||||
focusAndOpenToDate()
|
||||
})
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<span className="text-sm text-muted-foreground">~</span>
|
||||
<Input
|
||||
ref={toInputRef}
|
||||
type="date"
|
||||
value={value.to}
|
||||
onChange={(e) => onChange({ ...value, to: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{invalidRange && (
|
||||
<p className="text-xs text-red-600">조회 시작일은 종료일보다 이후일 수 없습니다.</p>
|
||||
)}
|
||||
<PopoverContent className={cn('w-auto p-0', contentClassName)} align="start">
|
||||
<Calendar
|
||||
mode="range"
|
||||
selected={selectedRange}
|
||||
onSelect={(range) => {
|
||||
onChange({
|
||||
from: formatDate(range?.from),
|
||||
to: formatDate(range?.to),
|
||||
})
|
||||
}}
|
||||
/>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon-sm"
|
||||
onClick={() => onChange({ from: '', to: '' })}
|
||||
disabled={disabled || !hasValue}
|
||||
aria-label="조회 기간 초기화"
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
</Button>
|
||||
{hasValue && !disabled ? (
|
||||
<button
|
||||
type="button"
|
||||
className="absolute right-2 top-1/2 inline-flex h-5 w-5 -translate-y-1/2 items-center justify-center rounded-full text-muted-foreground hover:bg-muted hover:text-foreground"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation()
|
||||
onChange({ from: '', to: '' })
|
||||
}}
|
||||
aria-label="조회 기간 초기화"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
103
registry/components/KriDateTimePicker.tsx
Normal file
103
registry/components/KriDateTimePicker.tsx
Normal file
@@ -0,0 +1,103 @@
|
||||
'use client'
|
||||
|
||||
import { CalendarIcon, X } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Calendar } from '@/components/ui/calendar'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
export type KriDateTimePickerProps = {
|
||||
value: string
|
||||
onChange: (value: string) => void
|
||||
placeholder?: string
|
||||
className?: string
|
||||
triggerClassName?: string
|
||||
contentClassName?: string
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
// value는 <input type="datetime-local">과 동일한 'yyyy-MM-ddTHH:mm' 포맷을 쓴다
|
||||
const parseDateTime = (value: string): { date: Date | undefined; time: string } => {
|
||||
if (!value) return { date: undefined, time: '' }
|
||||
|
||||
const [datePart, timePart = ''] = value.split('T')
|
||||
const [year, month, day] = datePart.split('-').map(Number)
|
||||
if (!year || !month || !day) return { date: undefined, time: timePart }
|
||||
|
||||
const date = new Date(year, month - 1, day)
|
||||
if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) {
|
||||
return { date: undefined, time: timePart }
|
||||
}
|
||||
|
||||
return { date, time: timePart }
|
||||
}
|
||||
|
||||
const toValue = (date: Date | undefined, time: string): string => {
|
||||
if (!date) return ''
|
||||
|
||||
const year = date.getFullYear()
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
|
||||
return `${year}-${month}-${day}T${time || '00:00'}`
|
||||
}
|
||||
|
||||
export function KriDateTimePicker({
|
||||
value,
|
||||
onChange,
|
||||
placeholder = '날짜/시간 선택',
|
||||
className,
|
||||
triggerClassName,
|
||||
contentClassName,
|
||||
disabled = false,
|
||||
}: KriDateTimePickerProps) {
|
||||
const { date, time } = parseDateTime(value)
|
||||
const hasValue = date !== undefined
|
||||
|
||||
return (
|
||||
<div className={cn('flex items-center gap-2', className)}>
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={disabled}
|
||||
className={cn(
|
||||
'w-40 justify-start bg-white text-left font-normal',
|
||||
!hasValue && 'text-muted-foreground',
|
||||
triggerClassName,
|
||||
)}
|
||||
>
|
||||
<CalendarIcon className="h-4 w-4" />
|
||||
<span className="truncate">{hasValue ? value.split('T')[0] : placeholder}</span>
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className={cn('w-auto p-0', contentClassName)} align="start">
|
||||
<Calendar mode="single" selected={date} onSelect={(nextDate) => onChange(toValue(nextDate, time || '00:00'))} />
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
|
||||
<Input
|
||||
type="time"
|
||||
className="w-32"
|
||||
value={time}
|
||||
disabled={disabled || !hasValue}
|
||||
onChange={(event) => onChange(toValue(date, event.target.value))}
|
||||
/>
|
||||
|
||||
{hasValue && !disabled ? (
|
||||
<button
|
||||
type="button"
|
||||
className="inline-flex h-5 w-5 items-center justify-center rounded-full text-muted-foreground hover:bg-muted hover:text-foreground"
|
||||
onClick={() => onChange('')}
|
||||
aria-label="날짜/시간 초기화"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default KriDateTimePicker
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import type { PaginationState, Row, RowSelectionState, SortingState } from '@tanstack/react-table'
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react'
|
||||
import type { OnChangeFn, PaginationState, Row, 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'
|
||||
@@ -13,25 +13,26 @@ import { DataTablePagination } from './DataTablePagination'
|
||||
import { usePreventClickOnDrag } from './hooks/usePreventClickOnDrag'
|
||||
|
||||
export function DataTable<TData, TValue = unknown>({
|
||||
columns,
|
||||
data,
|
||||
getRowId,
|
||||
highlightRowIds,
|
||||
isClient = true,
|
||||
pageCount: serverPageCount,
|
||||
totalCount: serverTotalCount,
|
||||
onPaginationChange: onServerPaginationChange,
|
||||
onSortingChange: onServerSortingChange,
|
||||
selectMode = 'none',
|
||||
onRowClick,
|
||||
onRowSelectionChange,
|
||||
defaultPageSize = 10,
|
||||
pageSizeOptions = [5, 10, 25, 50],
|
||||
paginationItemsToDisplay = 5,
|
||||
emptyText = '데이터가 없습니다.',
|
||||
className,
|
||||
}: Readonly<DataTableProps<TData, TValue>>) {
|
||||
const [pagination, setPagination] = useState<PaginationState>({ pageIndex: 0, pageSize: defaultPageSize })
|
||||
columns,
|
||||
data,
|
||||
getRowId,
|
||||
highlightRowIds,
|
||||
isClient = true,
|
||||
pageCount: serverPageCount,
|
||||
totalCount: serverTotalCount,
|
||||
pagination: externalPagination,
|
||||
onPaginationChange: onServerPaginationChange,
|
||||
onSortingChange: onServerSortingChange,
|
||||
selectMode = 'none',
|
||||
onRowClick,
|
||||
onRowSelectionChange,
|
||||
defaultPageSize = 10,
|
||||
pageSizeOptions = [5, 10, 25, 50],
|
||||
paginationItemsToDisplay = 5,
|
||||
emptyText = '데이터가 없습니다.',
|
||||
className,
|
||||
}: Readonly<DataTableProps<TData, TValue>>) {
|
||||
const [internalPagination, setInternalPagination] = useState<PaginationState>({ pageIndex: 0, pageSize: defaultPageSize })
|
||||
const [sorting, setSorting] = useState<SortingState>([])
|
||||
const [rowSelection, setRowSelection] = useState<RowSelectionState>({})
|
||||
const drag = usePreventClickOnDrag()
|
||||
@@ -43,22 +44,22 @@ export function DataTable<TData, TValue = unknown>({
|
||||
const selectionColumn = {
|
||||
id: 'select',
|
||||
header: ({ table }) => selectMode === 'multi' ? (
|
||||
<Checkbox
|
||||
checked={table.getIsAllPageRowsSelected() || (table.getIsSomePageRowsSelected() && 'indeterminate')}
|
||||
onCheckedChange={(value) => table.toggleAllPageRowsSelected(Boolean(value))}
|
||||
aria-label="전체 선택"
|
||||
/>
|
||||
<Checkbox
|
||||
checked={table.getIsAllPageRowsSelected() || (table.getIsSomePageRowsSelected() && 'indeterminate')}
|
||||
onCheckedChange={(value) => table.toggleAllPageRowsSelected(Boolean(value))}
|
||||
aria-label="전체 선택"
|
||||
/>
|
||||
) : null,
|
||||
cell: ({ row }) => (
|
||||
<Checkbox
|
||||
checked={row.getIsSelected()}
|
||||
onCheckedChange={(value) => {
|
||||
if (selectMode === 'single') table.resetRowSelection()
|
||||
row.toggleSelected(Boolean(value))
|
||||
}}
|
||||
aria-label="행 선택"
|
||||
onClick={(event) => event.stopPropagation()}
|
||||
/>
|
||||
<Checkbox
|
||||
checked={row.getIsSelected()}
|
||||
onCheckedChange={(value) => {
|
||||
if (selectMode === 'single') table.resetRowSelection()
|
||||
row.toggleSelected(Boolean(value))
|
||||
}}
|
||||
aria-label="행 선택"
|
||||
onClick={(event) => event.stopPropagation()}
|
||||
/>
|
||||
),
|
||||
size: 28,
|
||||
enableSorting: false,
|
||||
@@ -67,6 +68,25 @@ export function DataTable<TData, TValue = unknown>({
|
||||
return [selectionColumn, ...columns]
|
||||
}, [columns, selectMode, selectionEnabled])
|
||||
|
||||
const pagination = isClient ? internalPagination : (externalPagination ?? internalPagination)
|
||||
|
||||
const handlePaginationChange = useCallback<OnChangeFn<PaginationState>>(
|
||||
(updater) => {
|
||||
if (isClient) {
|
||||
setInternalPagination(updater)
|
||||
return
|
||||
}
|
||||
|
||||
if (onServerPaginationChange) {
|
||||
onServerPaginationChange(updater)
|
||||
return
|
||||
}
|
||||
|
||||
setInternalPagination(updater)
|
||||
},
|
||||
[isClient, onServerPaginationChange],
|
||||
)
|
||||
|
||||
const table = useReactTable({
|
||||
data,
|
||||
columns: enhancedColumns,
|
||||
@@ -75,7 +95,7 @@ export function DataTable<TData, TValue = unknown>({
|
||||
enableRowSelection: selectionEnabled,
|
||||
onRowSelectionChange: setRowSelection,
|
||||
onSortingChange: setSorting,
|
||||
onPaginationChange: setPagination,
|
||||
onPaginationChange: handlePaginationChange,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
getSortedRowModel: isClient ? getSortedRowModel() : undefined,
|
||||
getPaginationRowModel: isClient ? getPaginationRowModel() : undefined,
|
||||
@@ -85,10 +105,6 @@ export function DataTable<TData, TValue = unknown>({
|
||||
...(getRowId ? { getRowId } : {}),
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (!isClient) onServerPaginationChange?.(pagination)
|
||||
}, [isClient, onServerPaginationChange, pagination])
|
||||
|
||||
useEffect(() => {
|
||||
if (!isClient) onServerSortingChange?.(sorting)
|
||||
}, [isClient, onServerSortingChange, sorting])
|
||||
@@ -115,68 +131,68 @@ export function DataTable<TData, TValue = unknown>({
|
||||
const totalCount = isClient ? data.length : (serverTotalCount ?? 0)
|
||||
|
||||
return (
|
||||
<div className={cn('w-full space-y-4', className)}>
|
||||
<div className="rounded-md border">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
{table.getHeaderGroups().map((headerGroup) => (
|
||||
<TableRow key={headerGroup.id} className="hover:bg-transparent">
|
||||
{headerGroup.headers.map((header) => {
|
||||
const column = header.column
|
||||
const sorted = column.getIsSorted()
|
||||
const content = header.isPlaceholder ? null : flexRender(column.columnDef.header, header.getContext())
|
||||
<div className={cn('w-full space-y-4', className)}>
|
||||
<div className="rounded-md border">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
{table.getHeaderGroups().map((headerGroup) => (
|
||||
<TableRow key={headerGroup.id} className="hover:bg-transparent">
|
||||
{headerGroup.headers.map((header) => {
|
||||
const column = header.column
|
||||
const sorted = column.getIsSorted()
|
||||
const content = header.isPlaceholder ? null : flexRender(column.columnDef.header, header.getContext())
|
||||
|
||||
return (
|
||||
<TableHead
|
||||
key={header.id}
|
||||
className={cn('text-base font-semibold text-foreground', column.id === 'actions' && 'w-[140px] text-center')}
|
||||
style={{ width: header.getSize() !== 150 ? header.getSize() : undefined }}
|
||||
>
|
||||
{!header.isPlaceholder && column.getCanSort() ? (
|
||||
<Button type="button" variant="ghost" className="flex gap-1 px-0 text-base font-semibold" onClick={column.getToggleSortingHandler()}>
|
||||
<span>{content}</span>
|
||||
{sorted === 'asc' ? <ArrowUp className="size-3" /> : sorted === 'desc' ? <ArrowDown className="size-3" /> : <ArrowUpDown className="text-muted-foreground size-3" />}
|
||||
</Button>
|
||||
) : content}
|
||||
</TableHead>
|
||||
)
|
||||
})}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{table.getRowModel().rows.length ? table.getRowModel().rows.map((row) => (
|
||||
<TableRow
|
||||
key={row.id}
|
||||
data-state={row.getIsSelected() ? 'selected' : undefined}
|
||||
onPointerDown={drag.onPointerDown}
|
||||
onPointerMove={drag.onPointerMove}
|
||||
onPointerUp={drag.onPointerUp}
|
||||
onPointerCancel={drag.onPointerCancel}
|
||||
onClick={drag.wrapClick(() => handleRowClick(row))}
|
||||
className={cn(
|
||||
'text-sm [&>td]:px-2 [&>td]:py-0',
|
||||
selectMode !== 'none' && 'cursor-pointer',
|
||||
'data-[state=selected]:bg-muted data-[state=selected]:font-semibold',
|
||||
highlightRowIds?.includes(row.id) && 'bg-emerald-50',
|
||||
)}
|
||||
>
|
||||
{row.getVisibleCells().map((cell) => <TableCell key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</TableCell>)}
|
||||
</TableRow>
|
||||
)) : (
|
||||
<TableRow><TableCell colSpan={enhancedColumns.length} className="h-24 text-center">{emptyText}</TableCell></TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
return (
|
||||
<TableHead
|
||||
key={header.id}
|
||||
className={cn('text-base font-semibold text-foreground', column.id === 'actions' && 'w-[140px] text-center')}
|
||||
style={{ width: header.getSize() !== 150 ? header.getSize() : undefined }}
|
||||
>
|
||||
{!header.isPlaceholder && column.getCanSort() ? (
|
||||
<Button type="button" variant="ghost" className="flex gap-1 px-0 text-base font-semibold" onClick={column.getToggleSortingHandler()}>
|
||||
<span>{content}</span>
|
||||
{sorted === 'asc' ? <ArrowUp className="size-3" /> : sorted === 'desc' ? <ArrowDown className="size-3" /> : <ArrowUpDown className="text-muted-foreground size-3" />}
|
||||
</Button>
|
||||
) : content}
|
||||
</TableHead>
|
||||
)
|
||||
})}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{table.getRowModel().rows.length ? table.getRowModel().rows.map((row) => (
|
||||
<TableRow
|
||||
key={row.id}
|
||||
data-state={row.getIsSelected() ? 'selected' : undefined}
|
||||
onPointerDown={drag.onPointerDown}
|
||||
onPointerMove={drag.onPointerMove}
|
||||
onPointerUp={drag.onPointerUp}
|
||||
onPointerCancel={drag.onPointerCancel}
|
||||
onClick={drag.wrapClick(() => handleRowClick(row))}
|
||||
className={cn(
|
||||
'text-sm [&>td]:px-2 [&>td]:py-0',
|
||||
selectMode !== 'none' && 'cursor-pointer',
|
||||
'data-[state=selected]:bg-muted data-[state=selected]:font-semibold',
|
||||
highlightRowIds?.includes(row.id) && 'bg-emerald-50',
|
||||
)}
|
||||
>
|
||||
{row.getVisibleCells().map((cell) => <TableCell key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</TableCell>)}
|
||||
</TableRow>
|
||||
)) : (
|
||||
<TableRow><TableCell colSpan={enhancedColumns.length} className="h-24 text-center">{emptyText}</TableCell></TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
|
||||
<DataTablePagination
|
||||
table={table}
|
||||
isClient={isClient}
|
||||
totalCount={totalCount}
|
||||
pageSizeOptions={pageSizeOptions}
|
||||
paginationItemsToDisplay={paginationItemsToDisplay}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<DataTablePagination
|
||||
table={table}
|
||||
isClient={isClient}
|
||||
totalCount={totalCount}
|
||||
pageSizeOptions={pageSizeOptions}
|
||||
paginationItemsToDisplay={paginationItemsToDisplay}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { ColumnDef, PaginationState, SortingState } from '@tanstack/react-table'
|
||||
import type { ColumnDef, PaginationState, SortingState, Updater } from '@tanstack/react-table'
|
||||
|
||||
export type SelectMode = 'none' | 'click-event' | 'single' | 'multi'
|
||||
export type DataTableColumn<TData, TValue = unknown> = ColumnDef<TData, TValue>
|
||||
@@ -23,7 +23,8 @@ export interface DataTableProps<TData, TValue = unknown> {
|
||||
isClient?: boolean
|
||||
pageCount?: number
|
||||
totalCount?: number
|
||||
onPaginationChange?: (pagination: PaginationState) => void
|
||||
pagination?: PaginationState
|
||||
onPaginationChange?: (updater: Updater<PaginationState>) => void
|
||||
onSortingChange?: (sorting: SortingState) => void
|
||||
selectMode?: SelectMode
|
||||
onRowClick?: (row: TData) => void
|
||||
|
||||
@@ -16,12 +16,14 @@ interface DataTablePaginationProps<TData> {
|
||||
}
|
||||
|
||||
export function DataTablePagination<TData>({
|
||||
table,
|
||||
isClient,
|
||||
totalCount,
|
||||
pageSizeOptions,
|
||||
paginationItemsToDisplay,
|
||||
}: DataTablePaginationProps<TData>) {
|
||||
table,
|
||||
isClient,
|
||||
totalCount,
|
||||
pageSizeOptions,
|
||||
paginationItemsToDisplay,
|
||||
}: DataTablePaginationProps<TData>) {
|
||||
'use no memo'
|
||||
|
||||
const currentPage = table.getState().pagination.pageIndex + 1
|
||||
const pageCount = table.getPageCount()
|
||||
const { pages, showLeftEllipsis, showRightEllipsis } = usePagination({
|
||||
@@ -31,66 +33,66 @@ export function DataTablePagination<TData>({
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-between gap-3 p-2 pt-0 max-sm:flex-col">
|
||||
<p className="text-muted-foreground flex-1 whitespace-nowrap text-sm" aria-live="polite">
|
||||
Page <span className="text-foreground">{currentPage}</span> of{' '}
|
||||
<span className="text-foreground">{pageCount}</span>
|
||||
{!isClient && totalCount > 0 ? (
|
||||
<span className="ml-2">(총 <span className="text-foreground">{totalCount}</span>건)</span>
|
||||
) : null}
|
||||
</p>
|
||||
<div className="flex items-center justify-between gap-3 p-2 pt-0 max-sm:flex-col">
|
||||
<p className="text-muted-foreground flex-1 whitespace-nowrap text-sm" aria-live="polite">
|
||||
Page <span className="text-foreground">{currentPage}</span> of{' '}
|
||||
<span className="text-foreground">{pageCount}</span>
|
||||
{!isClient && totalCount > 0 ? (
|
||||
<span className="ml-2">(총 <span className="text-foreground">{totalCount}</span>건)</span>
|
||||
) : null}
|
||||
</p>
|
||||
|
||||
<div className="grow">
|
||||
<Pagination>
|
||||
<PaginationContent>
|
||||
<PaginationItem>
|
||||
<Button size="icon" variant="outline" onClick={() => table.firstPage()} disabled={!table.getCanPreviousPage()} aria-label="첫 페이지">
|
||||
<ChevronsLeftIcon aria-hidden="true" />
|
||||
</Button>
|
||||
</PaginationItem>
|
||||
<PaginationItem>
|
||||
<Button size="icon" variant="outline" onClick={() => table.previousPage()} disabled={!table.getCanPreviousPage()} aria-label="이전 페이지">
|
||||
<ChevronLeftIcon aria-hidden="true" />
|
||||
</Button>
|
||||
</PaginationItem>
|
||||
{showLeftEllipsis ? <PaginationItem><PaginationEllipsis /></PaginationItem> : null}
|
||||
{pages.map((page) => (
|
||||
<PaginationItem key={page}>
|
||||
<Button
|
||||
size="icon"
|
||||
variant={page === currentPage ? 'default' : 'ghost'}
|
||||
onClick={() => table.setPageIndex(page - 1)}
|
||||
aria-current={page === currentPage ? 'page' : undefined}
|
||||
>
|
||||
{page}
|
||||
<div className="grow">
|
||||
<Pagination>
|
||||
<PaginationContent>
|
||||
<PaginationItem>
|
||||
<Button size="icon" variant="outline" onClick={() => table.firstPage()} disabled={!table.getCanPreviousPage()} aria-label="첫 페이지">
|
||||
<ChevronsLeftIcon aria-hidden="true" />
|
||||
</Button>
|
||||
</PaginationItem>
|
||||
))}
|
||||
{showRightEllipsis ? <PaginationItem><PaginationEllipsis /></PaginationItem> : null}
|
||||
<PaginationItem>
|
||||
<Button size="icon" variant="outline" onClick={() => table.nextPage()} disabled={!table.getCanNextPage()} aria-label="다음 페이지">
|
||||
<ChevronRightIcon aria-hidden="true" />
|
||||
</Button>
|
||||
</PaginationItem>
|
||||
<PaginationItem>
|
||||
<Button size="icon" variant="outline" onClick={() => table.lastPage()} disabled={!table.getCanNextPage()} aria-label="마지막 페이지">
|
||||
<ChevronsRightIcon aria-hidden="true" />
|
||||
</Button>
|
||||
</PaginationItem>
|
||||
</PaginationContent>
|
||||
</Pagination>
|
||||
</div>
|
||||
<PaginationItem>
|
||||
<Button size="icon" variant="outline" onClick={() => table.previousPage()} disabled={!table.getCanPreviousPage()} aria-label="이전 페이지">
|
||||
<ChevronLeftIcon aria-hidden="true" />
|
||||
</Button>
|
||||
</PaginationItem>
|
||||
{showLeftEllipsis ? <PaginationItem><PaginationEllipsis /></PaginationItem> : null}
|
||||
{pages.map((page) => (
|
||||
<PaginationItem key={page}>
|
||||
<Button
|
||||
size="icon"
|
||||
variant={page === currentPage ? 'default' : 'ghost'}
|
||||
onClick={() => table.setPageIndex(page - 1)}
|
||||
aria-current={page === currentPage ? 'page' : undefined}
|
||||
>
|
||||
{page}
|
||||
</Button>
|
||||
</PaginationItem>
|
||||
))}
|
||||
{showRightEllipsis ? <PaginationItem><PaginationEllipsis /></PaginationItem> : null}
|
||||
<PaginationItem>
|
||||
<Button size="icon" variant="outline" onClick={() => table.nextPage()} disabled={!table.getCanNextPage()} aria-label="다음 페이지">
|
||||
<ChevronRightIcon aria-hidden="true" />
|
||||
</Button>
|
||||
</PaginationItem>
|
||||
<PaginationItem>
|
||||
<Button size="icon" variant="outline" onClick={() => table.lastPage()} disabled={!table.getCanNextPage()} aria-label="마지막 페이지">
|
||||
<ChevronsRightIcon aria-hidden="true" />
|
||||
</Button>
|
||||
</PaginationItem>
|
||||
</PaginationContent>
|
||||
</Pagination>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-1 justify-end">
|
||||
<Select value={String(table.getState().pagination.pageSize)} onValueChange={(value) => table.setPageSize(Number(value))}>
|
||||
<SelectTrigger className="w-fit whitespace-nowrap" aria-label="페이지당 개수">
|
||||
<SelectValue placeholder="페이지당 개수" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{pageSizeOptions.map((size) => <SelectItem key={size} value={String(size)}>{size} / page</SelectItem>)}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<div className="flex flex-1 justify-end">
|
||||
<Select value={String(table.getState().pagination.pageSize)} onValueChange={(value) => table.setPageSize(Number(value))}>
|
||||
<SelectTrigger className="w-fit whitespace-nowrap" aria-label="페이지당 개수">
|
||||
<SelectValue placeholder="페이지당 개수" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{pageSizeOptions.map((size) => <SelectItem key={size} value={String(size)}>{size} / page</SelectItem>)}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -22,15 +22,15 @@ export function usePreventClickOnDrag(thresholdPx = 6) {
|
||||
}, [])
|
||||
|
||||
const wrapClick = React.useCallback(
|
||||
(callback: () => void) => (event: React.MouseEvent) => {
|
||||
if (state.current.dragging) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
return
|
||||
}
|
||||
callback()
|
||||
},
|
||||
[],
|
||||
(callback: () => void) => (event: React.MouseEvent) => {
|
||||
if (state.current.dragging) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
return
|
||||
}
|
||||
callback()
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
return { onPointerDown, onPointerMove, onPointerUp, onPointerCancel: onPointerUp, wrapClick }
|
||||
|
||||
@@ -48,7 +48,7 @@ export function KriHeader({
|
||||
) : null}
|
||||
|
||||
{logo ? (
|
||||
<button type="button" className="flex shrink-0 items-center" onClick={() => onNavigate?.(homePath)}>
|
||||
<button type="button" className="flex shrink-0 items-center cursor-pointer" onClick={() => onNavigate?.(homePath)}>
|
||||
{logo}
|
||||
</button>
|
||||
) : null}
|
||||
|
||||
@@ -47,7 +47,7 @@ function OptionItem<T>({
|
||||
value={`${option.label} ${option.value}`}
|
||||
disabled={option.disabled}
|
||||
onSelect={onToggle}
|
||||
className="flex items-center"
|
||||
className="flex items-center cursor-pointer"
|
||||
>
|
||||
{!hideOptionCheck ? (
|
||||
<span
|
||||
@@ -124,7 +124,7 @@ export function MultiSelectContent<T>({
|
||||
{selectionMode === 'multiple' && !hideSelectAll && selectableOptions.length > 0 ? (
|
||||
<>
|
||||
<CommandGroup>
|
||||
<CommandItem value="__kri_select_all__" onSelect={onToggleAll}>
|
||||
<CommandItem className="cursor-pointer" value="__kri_select_all__" onSelect={onToggleAll}>
|
||||
<span
|
||||
className={cn(
|
||||
'border-primary flex size-4 items-center justify-center rounded-sm border',
|
||||
|
||||
@@ -24,8 +24,7 @@
|
||||
"description": "시작일과 종료일을 선택하고 초기화할 수 있는 KRI 공통 날짜 범위 컴포넌트입니다.",
|
||||
"registryDependencies": [
|
||||
"button",
|
||||
"input",
|
||||
"label",
|
||||
"calendar",
|
||||
"popover"
|
||||
],
|
||||
"dependencies": [
|
||||
|
||||
Reference in New Issue
Block a user