260706_03_ENH: DateTimePicker 수정 및 TimePicker 추가
This commit is contained in:
@@ -12,12 +12,14 @@ type CalendarSingleProps = CalendarBaseProps & {
|
|||||||
mode: "single";
|
mode: "single";
|
||||||
selected?: Date;
|
selected?: Date;
|
||||||
onSelect?: (date: Date | undefined) => void;
|
onSelect?: (date: Date | undefined) => void;
|
||||||
|
initialFocus?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type CalendarRangeProps = CalendarBaseProps & {
|
type CalendarRangeProps = CalendarBaseProps & {
|
||||||
mode: "range";
|
mode: "range";
|
||||||
selected?: CalendarDateRange;
|
selected?: CalendarDateRange;
|
||||||
onSelect?: (range: CalendarDateRange | undefined) => void;
|
onSelect?: (range: CalendarDateRange | undefined) => void;
|
||||||
|
initialFocus?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CalendarProps = CalendarSingleProps | CalendarRangeProps;
|
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-range-picker
|
||||||
kri-date-picker
|
kri-date-picker
|
||||||
kri-date-time-picker
|
kri-date-time-picker
|
||||||
|
kri-time-picker
|
||||||
kri-password
|
kri-password
|
||||||
kri-auto-complete
|
kri-auto-complete
|
||||||
kri-data-table
|
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-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-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-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-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-data-table.json
|
||||||
npx shadcn@latest add https://ui-registry.kric.com/r/kri-loading-overlay.json
|
npx shadcn@latest add https://ui-registry.kric.com/r/kri-loading-overlay.json
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kri-ui-registry",
|
"name": "kri-ui-registry",
|
||||||
"version": "0.3.1",
|
"version": "0.4.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "KRI internal shadcn UI registry",
|
"description": "KRI internal shadcn UI registry",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
|
import * as React from 'react'
|
||||||
import { CalendarIcon, X } from 'lucide-react'
|
import { CalendarIcon, X } from 'lucide-react'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { Calendar } from '@/components/ui/calendar'
|
import { Calendar } from '@/components/ui/calendar'
|
||||||
import { Input } from '@/components/ui/input'
|
|
||||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
||||||
|
import { KriTimePicker } from '@/components/common/KriTimePicker'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
|
|
||||||
export type KriDateTimePickerProps = {
|
export type KriDateTimePickerProps = {
|
||||||
@@ -17,7 +18,6 @@ export type KriDateTimePickerProps = {
|
|||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
// value는 <input type="datetime-local">과 동일한 'yyyy-MM-ddTHH:mm' 포맷을 쓴다
|
|
||||||
const parseDateTime = (value: string): { date: Date | undefined; time: string } => {
|
const parseDateTime = (value: string): { date: Date | undefined; time: string } => {
|
||||||
if (!value) return { date: undefined, time: '' }
|
if (!value) return { date: undefined, time: '' }
|
||||||
|
|
||||||
@@ -33,14 +33,28 @@ const parseDateTime = (value: string): { date: Date | undefined; time: string }
|
|||||||
return { date, time: timePart }
|
return { date, time: timePart }
|
||||||
}
|
}
|
||||||
|
|
||||||
const toValue = (date: Date | undefined, time: string): string => {
|
const formatDate = (date: Date | undefined) => {
|
||||||
if (!date) return ''
|
if (!date) return ''
|
||||||
|
|
||||||
const year = date.getFullYear()
|
const year = date.getFullYear()
|
||||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||||
const day = String(date.getDate()).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({
|
export function KriDateTimePicker({
|
||||||
@@ -52,45 +66,63 @@ export function KriDateTimePicker({
|
|||||||
contentClassName,
|
contentClassName,
|
||||||
disabled = false,
|
disabled = false,
|
||||||
}: KriDateTimePickerProps) {
|
}: KriDateTimePickerProps) {
|
||||||
|
const [open, setOpen] = React.useState(false)
|
||||||
const { date, time } = parseDateTime(value)
|
const { date, time } = parseDateTime(value)
|
||||||
const hasValue = date !== undefined
|
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 (
|
return (
|
||||||
<div className={cn('flex items-center gap-2', className)}>
|
<div className={cn('relative', className)}>
|
||||||
<Popover>
|
<Popover open={open} onOpenChange={setOpen}>
|
||||||
<PopoverTrigger asChild>
|
<PopoverTrigger asChild>
|
||||||
<Button
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
className={cn(
|
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',
|
!hasValue && 'text-muted-foreground',
|
||||||
triggerClassName,
|
triggerClassName,
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<CalendarIcon className="h-4 w-4" />
|
<CalendarIcon className="mr-2 h-4 w-4" />
|
||||||
<span className="truncate">{hasValue ? value.split('T')[0] : placeholder}</span>
|
<span className="truncate">{toDisplayText(date, time, placeholder)}</span>
|
||||||
</Button>
|
</Button>
|
||||||
</PopoverTrigger>
|
</PopoverTrigger>
|
||||||
<PopoverContent className={cn('w-auto p-0', contentClassName)} align="start">
|
<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>
|
</PopoverContent>
|
||||||
</Popover>
|
</Popover>
|
||||||
|
|
||||||
<Input
|
|
||||||
type="time"
|
|
||||||
className="w-32"
|
|
||||||
value={time}
|
|
||||||
disabled={disabled || !hasValue}
|
|
||||||
onChange={(event) => onChange(toValue(date, event.target.value))}
|
|
||||||
/>
|
|
||||||
|
|
||||||
{hasValue && !disabled ? (
|
{hasValue && !disabled ? (
|
||||||
<button
|
<button
|
||||||
type="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"
|
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={() => onChange('')}
|
onClick={(event) => {
|
||||||
|
event.stopPropagation()
|
||||||
|
onChange('')
|
||||||
|
}}
|
||||||
aria-label="날짜/시간 초기화"
|
aria-label="날짜/시간 초기화"
|
||||||
>
|
>
|
||||||
<X className="h-3.5 w-3.5" />
|
<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": [
|
"registryDependencies": [
|
||||||
"button",
|
"button",
|
||||||
"calendar",
|
"calendar",
|
||||||
"input",
|
"popover",
|
||||||
"popover"
|
"scroll-area"
|
||||||
],
|
],
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"lucide-react"
|
"lucide-react"
|
||||||
@@ -78,6 +78,28 @@
|
|||||||
"path": "KriDateTimePicker.tsx",
|
"path": "KriDateTimePicker.tsx",
|
||||||
"type": "registry:component",
|
"type": "registry:component",
|
||||||
"target": "@components/common/KriDateTimePicker.tsx"
|
"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