import { Button } from '@/component/ui/button' import { Input } from '@/component/ui/input' import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from '@/component/ui/select' import { ElementDefinition } from '@/entitiy/element/model/interface' import { useToast } from '@/lib/hooks/useToast' import { ElementOption } from '@/type/template' import { ChevronDown, Plus, Trash2 } from 'lucide-react' interface SelectConfig { placeholder?: string required?: boolean options: ElementOption[] targetCells: any[] } export const selectDefinition: ElementDefinition = { type: 'select', label: 'Выпадающий список', icon: , version: 1, defaultConfig: { placeholder: '', required: false, options: [], targetCells: [], }, mapToCells: config => config.targetCells || [], mapToCellValues: (config, value) => { const cells = config.targetCells || [] return cells.map(target => ({ target, value: value || '' })) }, Editor: ({ config, onChange }) => { const { warning } = useToast() const handleAddOption = () => { onChange({ ...config, options: [...config.options, { value: '', label: '' }], }) } const handleUpdateOption = ( index: number, field: 'value' | 'label', value: string ) => { // Проверяем дублирование только для подписей (label) if (field === 'label' && value.trim() !== '') { const isDuplicate = config.options.some( (option, i) => i !== index && option.label.trim().toLowerCase() === value.trim().toLowerCase() ) if (isDuplicate) { warning(`Такая подпись уже существует: "${value.trim()}"`) return } } onChange({ ...config, options: config.options.map((option, i) => i === index ? { ...option, [field]: value } : option ), }) } const handleRemoveOption = (index: number) => { onChange({ ...config, options: config.options.filter((_, i) => i !== index), }) } return (
{config.options.map((option, index) => { const isDuplicateLabel = config.options.some( (otherOption, i) => i !== index && option.label.trim() !== '' && otherOption.label.trim().toLowerCase() === option.label.trim().toLowerCase() ) // Убираем проверку дублирования для значений const isDuplicateValue = false return (
handleUpdateOption(index, 'value', e.target.value) } className={isDuplicateValue ? 'border-red-500' : ''} /> {isDuplicateValue && (

Это значение уже используется

)}
handleUpdateOption(index, 'label', e.target.value) } className={isDuplicateLabel ? 'border-red-500' : ''} /> {isDuplicateLabel && (

Такая подпись уже существует

)}
) })}
) }, Preview: ({ config }) => ( ), Render: ({ config, value, onChange }) => { const elementId = (config as any).id || config.placeholder || 'default' const storageKey = `select_${elementId}` const handleValueChange = (newValue: string) => { localStorage.setItem(storageKey, newValue) onChange?.(newValue) } // Если значение не установлено, пытаемся загрузить из localStorage const currentValue = value || localStorage.getItem(storageKey) || '' return ( ) }, }