260714_01_ENH: 테스트 서버에서 기동 후 중간 테스트를 위한 커밋
This commit is contained in:
99
src/components/common/KriTimePicker.tsx
Normal file
99
src/components/common/KriTimePicker.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
'use client'
|
||||
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
export type KriTimePickerProps = {
|
||||
value?: string
|
||||
onChange: (value: string) => void
|
||||
disabled?: boolean
|
||||
minuteStep?: number
|
||||
className?: string
|
||||
}
|
||||
|
||||
const parseTime = (value: string | undefined) => {
|
||||
if (!value) return { hour: undefined, minute: undefined }
|
||||
|
||||
const [hour, minute] = value.split(':').map(Number)
|
||||
|
||||
return {
|
||||
hour: Number.isInteger(hour) && hour >= 0 && hour < 24 ? hour : undefined,
|
||||
minute: Number.isInteger(minute) && minute >= 0 && minute < 60 ? minute : undefined,
|
||||
}
|
||||
}
|
||||
|
||||
const formatTime = (hour: number, minute: number) => {
|
||||
return `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}`
|
||||
}
|
||||
|
||||
export function KriTimePicker({
|
||||
value,
|
||||
onChange,
|
||||
disabled = false,
|
||||
minuteStep = 5,
|
||||
className,
|
||||
}: KriTimePickerProps) {
|
||||
const { hour: selectedHour, minute: selectedMinute } = parseTime(value)
|
||||
const normalizedMinuteStep = Number.isFinite(minuteStep) && minuteStep > 0
|
||||
? Math.min(Math.floor(minuteStep), 60)
|
||||
: 5
|
||||
const hours = Array.from({ length: 24 }, (_, index) => index)
|
||||
const minutes = Array.from(
|
||||
{ length: Math.ceil(60 / normalizedMinuteStep) },
|
||||
(_, index) => index * normalizedMinuteStep,
|
||||
).filter((minute) => minute < 60)
|
||||
|
||||
const handleTimeChange = (type: 'hour' | 'minute', nextValue: number) => {
|
||||
if (disabled) return
|
||||
|
||||
const nextHour = type === 'hour' ? nextValue : selectedHour ?? 0
|
||||
const nextMinute = type === 'minute' ? nextValue : selectedMinute ?? 0
|
||||
|
||||
onChange(formatTime(nextHour, nextMinute))
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn('flex flex-col divide-y sm:h-[300px] sm:flex-row sm:divide-x sm:divide-y-0', className)}>
|
||||
<ScrollArea className="w-64 sm:w-auto">
|
||||
<div className="flex p-2 sm:flex-col">
|
||||
{hours.map((hour) => (
|
||||
<Button
|
||||
key={hour}
|
||||
type="button"
|
||||
size="icon"
|
||||
variant={selectedHour === hour ? 'default' : 'ghost'}
|
||||
className="aspect-square shrink-0 sm:w-full"
|
||||
disabled={disabled}
|
||||
onClick={() => handleTimeChange('hour', hour)}
|
||||
>
|
||||
{hour}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
<ScrollBar orientation="horizontal" className="sm:hidden" />
|
||||
</ScrollArea>
|
||||
|
||||
<ScrollArea className="w-64 sm:w-auto">
|
||||
<div className="flex p-2 sm:flex-col">
|
||||
{minutes.map((minute) => (
|
||||
<Button
|
||||
key={minute}
|
||||
type="button"
|
||||
size="icon"
|
||||
variant={selectedMinute === minute ? 'default' : 'ghost'}
|
||||
className="aspect-square shrink-0 sm:w-full"
|
||||
disabled={disabled}
|
||||
onClick={() => handleTimeChange('minute', minute)}
|
||||
>
|
||||
{String(minute).padStart(2, '0')}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
<ScrollBar orientation="horizontal" className="sm:hidden" />
|
||||
</ScrollArea>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default KriTimePicker
|
||||
Reference in New Issue
Block a user