251201_01_ENH : datatable, autocomplete, user관리 완성
This commit is contained in:
138
src/components/common/KriAutoComplete.tsx
Normal file
138
src/components/common/KriAutoComplete.tsx
Normal file
@@ -0,0 +1,138 @@
|
||||
'use client'
|
||||
|
||||
import * as React from 'react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { MultiSelect } from '@/components/ui/multi-select'
|
||||
import type { MultiSelectOption } from '@/components/ui/multi-select'
|
||||
|
||||
type BaseProps<T> = {
|
||||
items: T[]
|
||||
searchable?: boolean
|
||||
searchMode?: "filter" | "inputOnly";
|
||||
onSearchValueChange?: (value: string) => void;
|
||||
placeholder?: string
|
||||
widthClassName?: string
|
||||
heightClassName?: string
|
||||
className?: string
|
||||
getValue?: (item: T) => string
|
||||
getLabel?: (item: T) => string
|
||||
}
|
||||
|
||||
type KriMultiSelectProps<T> = BaseProps<T> & {
|
||||
value: string[]
|
||||
onChange: (value: string[]) => void
|
||||
/** 최대 선택 개수 제한 (없으면 무제한) */
|
||||
maxCount?: number
|
||||
maxSelected?: number
|
||||
hideSelectAll?: boolean
|
||||
}
|
||||
|
||||
export function KriMultiSelect<T>({
|
||||
items,
|
||||
value,
|
||||
onChange,
|
||||
searchable = true,
|
||||
searchMode = "filter",
|
||||
onSearchValueChange,
|
||||
maxCount = 1,
|
||||
maxSelected,
|
||||
hideSelectAll = false,
|
||||
placeholder = 'Select options',
|
||||
widthClassName,
|
||||
heightClassName,
|
||||
className,
|
||||
getValue,
|
||||
getLabel,
|
||||
}: KriMultiSelectProps<T>) {
|
||||
const extractValue = React.useCallback((item: T) => (getValue ? getValue(item) : String(item)), [getValue])
|
||||
const extractLabel = React.useCallback((item: T) => (getLabel ? getLabel(item) : String(item)), [getLabel])
|
||||
|
||||
const options = React.useMemo<MultiSelectOption[]>(
|
||||
() =>
|
||||
items.map((item) => ({
|
||||
value: extractValue(item),
|
||||
label: extractLabel(item),
|
||||
})),
|
||||
[items, extractValue, extractLabel]
|
||||
)
|
||||
|
||||
return (
|
||||
<div className={cn(widthClassName, heightClassName, "flex-none overflow-hidden")}>
|
||||
<MultiSelect
|
||||
options={options}
|
||||
defaultValue={value}
|
||||
onValueChange={onChange}
|
||||
maxSelected={maxSelected}
|
||||
placeholder={placeholder}
|
||||
className={cn("bg-white content-text-lg", className)}
|
||||
searchable={searchable}
|
||||
searchMode={searchMode}
|
||||
onSearchValueChange={onSearchValueChange}
|
||||
maxCount={maxCount}
|
||||
hideSelectAll={hideSelectAll}
|
||||
autoSize
|
||||
singleLine
|
||||
resetOnDefaultValueChange
|
||||
deduplicateOptions
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
type KriSingleSelectProps<T> = BaseProps<T> & {
|
||||
value: string
|
||||
onChange: (value: string) => void
|
||||
}
|
||||
|
||||
export function KriSingleSelect<T>({
|
||||
items,
|
||||
value,
|
||||
onChange,
|
||||
searchable = true,
|
||||
searchMode = "filter",
|
||||
onSearchValueChange,
|
||||
placeholder = 'Select an option',
|
||||
heightClassName,
|
||||
widthClassName,
|
||||
className,
|
||||
getValue,
|
||||
getLabel,
|
||||
}: KriSingleSelectProps<T>) {
|
||||
const extractValue = React.useCallback((item: T) => (getValue ? getValue(item) : String(item)), [getValue])
|
||||
const extractLabel = React.useCallback((item: T) => (getLabel ? getLabel(item) : String(item)), [getLabel])
|
||||
|
||||
const options = React.useMemo<MultiSelectOption[]>(
|
||||
() =>
|
||||
items.map((item) => ({
|
||||
value: extractValue(item),
|
||||
label: extractLabel(item),
|
||||
})),
|
||||
[items, extractValue, extractLabel]
|
||||
)
|
||||
|
||||
const defaultValue = React.useMemo(() => (value ? [value] : []), [value]);
|
||||
|
||||
return (
|
||||
<div className={cn(widthClassName, heightClassName, "flex-none overflow-hidden")}>
|
||||
<MultiSelect
|
||||
options={options}
|
||||
defaultValue={defaultValue}
|
||||
onValueChange={(next) => onChange(next.at(-1) ?? '')}
|
||||
selectionMode="single"
|
||||
maxCount={1}
|
||||
maxSelected={1}
|
||||
placeholder={placeholder}
|
||||
className={cn("bg-white content-text-lg", className)}
|
||||
searchable={searchable}
|
||||
searchMode={searchMode}
|
||||
onSearchValueChange={onSearchValueChange}
|
||||
hideSelectAll
|
||||
autoSize
|
||||
singleLine
|
||||
closeOnSelect
|
||||
resetOnDefaultValueChange
|
||||
deduplicateOptions
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user