260706_03_ENH: DateTimePicker 수정 및 TimePicker 추가

This commit is contained in:
2026-07-06 15:44:35 +09:00
parent 99e7acf584
commit 6a2aa2088c
7 changed files with 194 additions and 23 deletions

View File

@@ -1,10 +1,11 @@
'use client'
import * as React from 'react'
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 { KriTimePicker } from '@/components/common/KriTimePicker'
import { cn } from '@/lib/utils'
export type KriDateTimePickerProps = {
@@ -17,7 +18,6 @@ export type KriDateTimePickerProps = {
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: '' }
@@ -33,14 +33,28 @@ const parseDateTime = (value: string): { date: Date | undefined; time: string }
return { date, time: timePart }
}
const toValue = (date: Date | undefined, time: string): string => {
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}T${time || '00:00'}`
return `${year}-${month}-${day}`
}
const toValue = (date: Date | undefined, time: string): string => {
const dateText = formatDate(date)
if (!dateText) return ''
return `${dateText}T${time || '00:00'}`
}
const toDisplayText = (date: Date | undefined, time: string, placeholder: string) => {
const dateText = formatDate(date)
if (!dateText) return placeholder
return `${dateText} ${time || '00:00'}`
}
export function KriDateTimePicker({
@@ -52,45 +66,63 @@ export function KriDateTimePicker({
contentClassName,
disabled = false,
}: KriDateTimePickerProps) {
const [open, setOpen] = React.useState(false)
const { date, time } = parseDateTime(value)
const hasValue = date !== undefined
const handleDateSelect = (nextDate: Date | undefined) => {
if (!nextDate) return
onChange(toValue(nextDate, time || '00:00'))
}
const handleTimeChange = (nextTime: string) => {
if (!date) return
onChange(toValue(date, nextTime))
}
return (
<div className={cn('flex items-center gap-2', className)}>
<Popover>
<div className={cn('relative', className)}>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
type="button"
variant="outline"
disabled={disabled}
className={cn(
'w-40 justify-start bg-white text-left font-normal',
'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">{hasValue ? value.split('T')[0] : placeholder}</span>
<CalendarIcon className="mr-2 h-4 w-4" />
<span className="truncate">{toDisplayText(date, time, 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'))} />
<div className="sm:flex">
<Calendar
mode="single"
selected={date}
onSelect={handleDateSelect}
initialFocus
/>
<KriTimePicker
value={time || '00:00'}
onChange={handleTimeChange}
disabled={!date}
/>
</div>
</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('')}
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" />