260706_03_ENH: DateTimePicker 수정 및 TimePicker 추가
This commit is contained in:
@@ -12,12 +12,14 @@ type CalendarSingleProps = CalendarBaseProps & {
|
||||
mode: "single";
|
||||
selected?: Date;
|
||||
onSelect?: (date: Date | undefined) => void;
|
||||
initialFocus?: boolean;
|
||||
};
|
||||
|
||||
type CalendarRangeProps = CalendarBaseProps & {
|
||||
mode: "range";
|
||||
selected?: CalendarDateRange;
|
||||
onSelect?: (range: CalendarDateRange | undefined) => void;
|
||||
initialFocus?: boolean;
|
||||
};
|
||||
|
||||
export type CalendarProps = CalendarSingleProps | CalendarRangeProps;
|
||||
|
||||
14
.tooling/components/ui/scroll-area.tsx
Normal file
14
.tooling/components/ui/scroll-area.tsx
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { HTMLAttributes } from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export function ScrollArea({ className, ...props }: HTMLAttributes<HTMLDivElement>) {
|
||||
return <div className={cn(className)} {...props} />;
|
||||
}
|
||||
|
||||
export function ScrollBar({
|
||||
className,
|
||||
orientation,
|
||||
...props
|
||||
}: HTMLAttributes<HTMLDivElement> & { orientation?: "horizontal" | "vertical" }) {
|
||||
return <div data-orientation={orientation} className={cn(className)} {...props} />;
|
||||
}
|
||||
@@ -30,6 +30,7 @@ kri-select
|
||||
kri-date-range-picker
|
||||
kri-date-picker
|
||||
kri-date-time-picker
|
||||
kri-time-picker
|
||||
kri-password
|
||||
kri-auto-complete
|
||||
kri-data-table
|
||||
@@ -46,6 +47,7 @@ npx shadcn@latest add https://ui-registry.kric.com/r/kri-password.json
|
||||
npx shadcn@latest add https://ui-registry.kric.com/r/kri-date-range-picker.json
|
||||
npx shadcn@latest add https://ui-registry.kric.com/r/kri-date-picker.json
|
||||
npx shadcn@latest add https://ui-registry.kric.com/r/kri-date-time-picker.json
|
||||
npx shadcn@latest add https://ui-registry.kric.com/r/kri-time-picker.json
|
||||
npx shadcn@latest add https://ui-registry.kric.com/r/kri-auto-complete.json
|
||||
npx shadcn@latest add https://ui-registry.kric.com/r/kri-data-table.json
|
||||
npx shadcn@latest add https://ui-registry.kric.com/r/kri-loading-overlay.json
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "kri-ui-registry",
|
||||
"version": "0.3.1",
|
||||
"version": "0.4.0",
|
||||
"private": true,
|
||||
"description": "KRI internal shadcn UI registry",
|
||||
"scripts": {
|
||||
|
||||
@@ -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" />
|
||||
|
||||
99
registry/components/KriTimePicker.tsx
Normal file
99
registry/components/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) => 23 - 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
|
||||
@@ -67,8 +67,8 @@
|
||||
"registryDependencies": [
|
||||
"button",
|
||||
"calendar",
|
||||
"input",
|
||||
"popover"
|
||||
"popover",
|
||||
"scroll-area"
|
||||
],
|
||||
"dependencies": [
|
||||
"lucide-react"
|
||||
@@ -78,6 +78,28 @@
|
||||
"path": "KriDateTimePicker.tsx",
|
||||
"type": "registry:component",
|
||||
"target": "@components/common/KriDateTimePicker.tsx"
|
||||
},
|
||||
{
|
||||
"path": "KriTimePicker.tsx",
|
||||
"type": "registry:component",
|
||||
"target": "@components/common/KriTimePicker.tsx"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "kri-time-picker",
|
||||
"type": "registry:component",
|
||||
"title": "KRI Time Picker",
|
||||
"description": "KRI common 24-hour time picker component.",
|
||||
"registryDependencies": [
|
||||
"button",
|
||||
"scroll-area"
|
||||
],
|
||||
"files": [
|
||||
{
|
||||
"path": "KriTimePicker.tsx",
|
||||
"type": "registry:component",
|
||||
"target": "@components/common/KriTimePicker.tsx"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user