Portal/rsconcept/frontend/src/features/library/components/select-library-item.tsx

46 lines
1.3 KiB
TypeScript
Raw Normal View History

'use client';
2025-02-20 20:22:05 +03:00
import { type Styling } from '@/components/props';
2025-04-10 11:03:55 +03:00
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
2025-02-20 20:22:05 +03:00
import { type ILibraryItem } from '../backend/types';
2025-02-20 20:22:05 +03:00
interface SelectLibraryItemProps extends Styling {
2025-04-10 11:03:55 +03:00
id?: string;
value: ILibraryItem | null;
onChange: (newValue: ILibraryItem | null) => void;
items?: ILibraryItem[];
placeholder?: string;
noBorder?: boolean;
}
2025-02-12 15:12:59 +03:00
export function SelectLibraryItem({
2025-04-10 11:03:55 +03:00
id,
items,
value,
onChange,
placeholder = 'Выберите схему',
...restProps
}: SelectLibraryItemProps) {
2025-04-10 11:03:55 +03:00
function handleSelect(newValue: string) {
const newItem = items?.find(item => item.id === Number(newValue)) ?? null;
onChange(newItem);
}
return (
2025-04-10 11:03:55 +03:00
<Select onValueChange={handleSelect} defaultValue={value ? String(value.id) : undefined}>
<SelectTrigger id={id} {...restProps}>
<SelectValue placeholder={placeholder} />
</SelectTrigger>
<SelectContent className='max-w-80'>
{items?.map(item => (
<SelectItem key={`${id ?? 'default'}-item-select-${item.id}`} value={String(item.id)}>
{`${item.alias}: ${item.title}`}
</SelectItem>
))}
</SelectContent>
</Select>
);
}