2024-07-30 15:59:37 +03:00
|
|
|
'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-10 01:32:16 +03:00
|
|
|
|
2025-02-20 20:22:05 +03:00
|
|
|
import { type ILibraryItem } from '../backend/types';
|
2024-07-30 15:59:37 +03:00
|
|
|
|
2025-02-20 20:22:05 +03:00
|
|
|
interface SelectLibraryItemProps extends Styling {
|
2025-04-10 11:03:55 +03:00
|
|
|
id?: string;
|
2025-02-19 22:32:50 +03:00
|
|
|
value: ILibraryItem | null;
|
|
|
|
onChange: (newValue: ILibraryItem | null) => void;
|
2024-07-30 15:59:37 +03:00
|
|
|
|
2025-02-19 22:32:50 +03:00
|
|
|
items?: ILibraryItem[];
|
2024-07-30 15:59:37 +03:00
|
|
|
placeholder?: string;
|
|
|
|
noBorder?: boolean;
|
|
|
|
}
|
|
|
|
|
2025-02-12 15:12:59 +03:00
|
|
|
export function SelectLibraryItem({
|
2025-04-10 11:03:55 +03:00
|
|
|
id,
|
2024-07-30 15:59:37 +03:00
|
|
|
items,
|
|
|
|
value,
|
2025-02-04 20:35:18 +03:00
|
|
|
onChange,
|
2024-07-30 15:59:37 +03:00
|
|
|
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);
|
2024-12-13 21:30:49 +03:00
|
|
|
}
|
2024-07-30 15:59:37 +03:00
|
|
|
|
|
|
|
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>
|
2024-07-30 15:59:37 +03:00
|
|
|
);
|
|
|
|
}
|