260702_01_ENH: 리스트 작업 도중 공용 ui를 적용하기 위한 중간 커밋
This commit is contained in:
@@ -50,8 +50,8 @@ export function KriMultiSelect<T = unknown>({
|
||||
placeholder = 'Select options',
|
||||
widthClassName,
|
||||
heightClassName,
|
||||
spanClassName = "content-text-lg",
|
||||
badgeClassName = "content-text-lg",
|
||||
spanClassName = "text-lg",
|
||||
badgeClassName = "text-lg",
|
||||
className,
|
||||
getValue,
|
||||
getLabel,
|
||||
@@ -89,7 +89,7 @@ export function KriMultiSelect<T = unknown>({
|
||||
onInfoChange={onItemsChange}
|
||||
maxSelected={maxSelected}
|
||||
placeholder={placeholder}
|
||||
className={cn("bg-white content-text-lg", className)}
|
||||
className={cn("bg-white text-lg", className)}
|
||||
spanClassName={spanClassName}
|
||||
badgeClassName={badgeClassName}
|
||||
searchable={searchable}
|
||||
@@ -133,8 +133,8 @@ export function KriSingleSelect<T = unknown>({
|
||||
placeholder = 'Select an option',
|
||||
heightClassName,
|
||||
widthClassName,
|
||||
spanClassName = "content-text-lg",
|
||||
badgeClassName = "content-text-lg",
|
||||
spanClassName = "text-lg",
|
||||
badgeClassName = "text-lg",
|
||||
className,
|
||||
getValue,
|
||||
getLabel,
|
||||
@@ -216,7 +216,7 @@ export function KriSingleSelect<T = unknown>({
|
||||
maxCount={1}
|
||||
maxSelected={1}
|
||||
placeholder={placeholder}
|
||||
className={cn("bg-white content-text-lg", className)}
|
||||
className={cn("bg-white text-lg", className)}
|
||||
spanClassName={spanClassName}
|
||||
badgeClassName={badgeClassName}
|
||||
searchable={searchable}
|
||||
|
||||
120
src/components/common/KriDateRangePicker.tsx
Normal file
120
src/components/common/KriDateRangePicker.tsx
Normal file
@@ -0,0 +1,120 @@
|
||||
'use client'
|
||||
|
||||
import { useRef } from 'react'
|
||||
import { CalendarDays, X } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
export type KriDateRange = {
|
||||
from: string
|
||||
to: string
|
||||
}
|
||||
|
||||
type KriDateRangePickerProps = {
|
||||
value: KriDateRange
|
||||
onChange: (value: KriDateRange) => void
|
||||
placeholder?: string
|
||||
className?: string
|
||||
triggerClassName?: string
|
||||
contentClassName?: string
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
export function KriDateRangePicker({
|
||||
value,
|
||||
onChange,
|
||||
placeholder = '조회 기간 선택',
|
||||
className,
|
||||
triggerClassName,
|
||||
contentClassName,
|
||||
disabled = false,
|
||||
}: KriDateRangePickerProps) {
|
||||
const toInputRef = useRef<HTMLInputElement | null>(null)
|
||||
|
||||
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
|
||||
const invalidRange = value.from.length > 0 && value.to.length > 0 && value.from > value.to
|
||||
|
||||
const focusAndOpenToDate = () => {
|
||||
const input = toInputRef.current
|
||||
if (!input) return
|
||||
|
||||
input.focus()
|
||||
const inputWithPicker = input as HTMLInputElement & { showPicker?: () => void }
|
||||
inputWithPicker.showPicker?.()
|
||||
if (!inputWithPicker.showPicker) {
|
||||
input.click()
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn('flex items-center gap-2', className)}>
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={disabled}
|
||||
className={cn('justify-start min-w-[260px] font-normal', triggerClassName)}
|
||||
>
|
||||
<CalendarDays className="w-4 h-4" />
|
||||
<span className="truncate">{displayText}</span>
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent align="start" className={cn('w-[360px] space-y-3', contentClassName)}>
|
||||
<Label className="font-semibold">조회 기간</Label>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
type="date"
|
||||
value={value.from}
|
||||
onChange={(e) => {
|
||||
const nextFrom = e.target.value
|
||||
onChange({ ...value, from: nextFrom })
|
||||
if (nextFrom) {
|
||||
requestAnimationFrame(() => {
|
||||
focusAndOpenToDate()
|
||||
})
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<span className="text-sm text-muted-foreground">~</span>
|
||||
<Input
|
||||
ref={toInputRef}
|
||||
type="date"
|
||||
value={value.to}
|
||||
onChange={(e) => onChange({ ...value, to: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{invalidRange && (
|
||||
<p className="text-xs text-red-600">조회 시작일은 종료일보다 이후일 수 없습니다.</p>
|
||||
)}
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon-sm"
|
||||
onClick={() => onChange({ from: '', to: '' })}
|
||||
disabled={disabled || !hasValue}
|
||||
aria-label="조회 기간 초기화"
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default KriDateRangePicker
|
||||
@@ -1,5 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import type { CSSProperties } from 'react'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
@@ -9,78 +10,86 @@ export type KriSelectItem = {
|
||||
}
|
||||
|
||||
type KriSelectProps<T> = {
|
||||
/** options 배열: string[] 혹은 T[] */
|
||||
items: T[]
|
||||
/** 현재 선택 값 */
|
||||
value: string
|
||||
/** 값 변경 시 호출 */
|
||||
onChange?: (value: string) => void
|
||||
/** 옵션 변경시 호출 */
|
||||
onChangeOption?: (option: T) => void
|
||||
/** placeholder 텍스트 */
|
||||
placeholder?: string
|
||||
/** 기타 클래스 (예: 'w-40') */
|
||||
className?: string
|
||||
/** value를 어떻게 뽑을지 (없으면 String(item)) */
|
||||
heightClassName?: string
|
||||
triggerTextClassName?: string
|
||||
contentClassName?: string
|
||||
itemClassName?: string
|
||||
getValue?: (item: T) => string
|
||||
/** label을 어떻게 뽑을지 (없으면 String(item)) */
|
||||
getLabel?: (item: T) => string
|
||||
/** 전체 사용 여부 */
|
||||
useAll?: boolean
|
||||
}
|
||||
|
||||
const HEIGHT_CLASS_VALUE: Record<string, CSSProperties['height']> = {
|
||||
'h-6': '1.5rem',
|
||||
'h-7': '1.75rem',
|
||||
'h-8': '2rem',
|
||||
'h-9': '2.25rem',
|
||||
'h-10': '2.5rem',
|
||||
}
|
||||
|
||||
export function KriSelect<T>({
|
||||
items,
|
||||
value,
|
||||
onChange,
|
||||
onChangeOption,
|
||||
placeholder,
|
||||
className,
|
||||
getValue,
|
||||
getLabel,
|
||||
useAll = true,
|
||||
}: KriSelectProps<T>) {
|
||||
|
||||
const extractValue = (item: T) =>
|
||||
getValue ? getValue(item) : String(item);
|
||||
|
||||
const extractLabel = (item: T) =>
|
||||
getLabel ? getLabel(item) : String(item);
|
||||
items,
|
||||
value,
|
||||
onChange,
|
||||
onChangeOption,
|
||||
placeholder,
|
||||
className,
|
||||
heightClassName,
|
||||
triggerTextClassName = 'text-lg',
|
||||
contentClassName,
|
||||
itemClassName,
|
||||
getValue,
|
||||
getLabel,
|
||||
useAll = true,
|
||||
}: KriSelectProps<T>) {
|
||||
const extractValue = (item: T) => (getValue ? getValue(item) : String(item))
|
||||
const extractLabel = (item: T) => (getLabel ? getLabel(item) : String(item))
|
||||
const triggerStyle: CSSProperties | undefined = heightClassName
|
||||
? { height: HEIGHT_CLASS_VALUE[heightClassName] ?? undefined }
|
||||
: undefined
|
||||
|
||||
return (
|
||||
<Select
|
||||
value={value}
|
||||
onValueChange={(v) => {
|
||||
onChange?.(v);
|
||||
<Select
|
||||
value={value}
|
||||
onValueChange={(v) => {
|
||||
onChange?.(v)
|
||||
|
||||
const selected = items.find((item) => extractValue(item) === v);
|
||||
if(selected) {
|
||||
onChangeOption?.(selected);
|
||||
}
|
||||
}}
|
||||
const selected = items.find((item) => extractValue(item) === v)
|
||||
if (selected) {
|
||||
onChangeOption?.(selected)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<SelectTrigger
|
||||
className={cn('w-fit whitespace-nowrap bg-white', triggerTextClassName, heightClassName, className)}
|
||||
style={triggerStyle}
|
||||
aria-label={placeholder ?? 'select'}
|
||||
>
|
||||
<SelectTrigger
|
||||
className={cn(
|
||||
'w-fit whitespace-nowrap bg-white content-text-lg', // 폰트 사이즈
|
||||
className
|
||||
)}
|
||||
aria-label={placeholder ?? 'select'}
|
||||
>
|
||||
<SelectValue placeholder={placeholder} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{ useAll? <SelectItem key="ALL" value="ALL">전체</SelectItem> :null }
|
||||
{items.map((item, idx) => {
|
||||
const v = extractValue(item);
|
||||
const label = extractLabel(item);
|
||||
<SelectValue placeholder={placeholder} />
|
||||
</SelectTrigger>
|
||||
<SelectContent className={contentClassName}>
|
||||
{useAll ? (
|
||||
<SelectItem key="ALL" value="ALL" className={itemClassName}>
|
||||
전체
|
||||
</SelectItem>
|
||||
) : null}
|
||||
{items.map((item, idx) => {
|
||||
const v = extractValue(item)
|
||||
const label = extractLabel(item)
|
||||
|
||||
return (
|
||||
<SelectItem key={v ?? idx} value={v}>
|
||||
{label}
|
||||
</SelectItem>
|
||||
)
|
||||
})}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
return (
|
||||
<SelectItem key={v ?? idx} value={v} className={itemClassName}>
|
||||
{label}
|
||||
</SelectItem>
|
||||
)
|
||||
})}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user