260706_01_ENH: DataTable 버그 수정 및 Date 관련 컴포넌트 추가

This commit is contained in:
2026-07-06 15:22:51 +09:00
parent 88043ecc34
commit e77fe4368a
13 changed files with 476 additions and 244 deletions

View File

@@ -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>
)
}