дублирование в списке и отключить копирование при открытом диалговом окне

This commit is contained in:
2025-07-25 15:54:05 +03:00
parent 34c1cf6e71
commit b47ec33655
3 changed files with 93 additions and 39 deletions

View File

@@ -9,6 +9,7 @@ import {
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'
@@ -36,6 +37,8 @@ export const selectDefinition: ElementDefinition<SelectConfig, string> = {
return cells.map(target => ({ target, value: value || '' }))
},
Editor: ({ config, onChange }) => {
const { warning } = useToast()
const handleAddOption = () => {
onChange({
...config,
@@ -48,6 +51,19 @@ export const selectDefinition: ElementDefinition<SelectConfig, string> = {
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) =>
@@ -74,31 +90,59 @@ export const selectDefinition: ElementDefinition<SelectConfig, string> = {
</Button>
</div>
<div className="max-h-32 space-y-2 overflow-y-auto">
{config.options.map((option, index) => (
<div key={index} className="flex gap-2">
<Input
placeholder="Значение"
value={option.value}
onChange={e =>
handleUpdateOption(index, 'value', e.target.value)
}
/>
<Input
placeholder="Подпись"
value={option.label}
onChange={e =>
handleUpdateOption(index, 'label', e.target.value)
}
/>
<Button
variant="outline"
size="icon"
onClick={() => handleRemoveOption(index)}
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
))}
{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 (
<div key={index} className="flex gap-2">
<div className="flex-1">
<Input
placeholder="Значение"
value={option.value}
onChange={e =>
handleUpdateOption(index, 'value', e.target.value)
}
className={isDuplicateValue ? 'border-red-500' : ''}
/>
{isDuplicateValue && (
<p className="mt-1 text-xs text-red-500">
Это значение уже используется
</p>
)}
</div>
<div className="flex-1">
<Input
placeholder="Подпись"
value={option.label}
onChange={e =>
handleUpdateOption(index, 'label', e.target.value)
}
className={isDuplicateLabel ? 'border-red-500' : ''}
/>
{isDuplicateLabel && (
<p className="mt-1 text-xs text-red-500">
Такая подпись уже существует
</p>
)}
</div>
<Button
variant="outline"
size="icon"
onClick={() => handleRemoveOption(index)}
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
)
})}
</div>
</div>
</div>