123 lines
3.3 KiB
TypeScript
123 lines
3.3 KiB
TypeScript
'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'
|
|
|
|
export type KriDateRange = {
|
|
from: string
|
|
to: string
|
|
}
|
|
|
|
export type KriDateRangePickerProps = {
|
|
value: KriDateRange
|
|
onChange: (value: KriDateRange) => void
|
|
placeholder?: string
|
|
className?: string
|
|
triggerClassName?: string
|
|
contentClassName?: string
|
|
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,
|
|
placeholder = '조회 기간 선택',
|
|
className,
|
|
triggerClassName,
|
|
contentClassName,
|
|
disabled = false,
|
|
}: KriDateRangePickerProps) {
|
|
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}`
|
|
: value.from
|
|
? `${value.from} ~`
|
|
: value.to
|
|
? `~ ${value.to}`
|
|
: placeholder
|
|
|
|
const hasValue = value.from.length > 0 || value.to.length > 0
|
|
|
|
return (
|
|
<div className={cn('relative', className)}>
|
|
<Popover>
|
|
<PopoverTrigger asChild>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={disabled}
|
|
className={cn(
|
|
'h-9 w-full justify-start bg-white pr-8 text-left font-normal',
|
|
!hasValue && 'text-muted-foreground',
|
|
triggerClassName,
|
|
)}
|
|
>
|
|
<CalendarIcon className="h-4 w-4" />
|
|
<span className="truncate">{displayText}</span>
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<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>
|
|
|
|
{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>
|
|
)
|
|
}
|
|
|
|
export default KriDateRangePicker
|