'use client' import { Fragment, DragEvent, useState, useMemo } from 'react' import { cn } from '@/lib/ui-styles' import type { FieldForm, DragHoverState } from '../../template-manager/types' import FieldValidationConfig from './FieldValidationConfig' import FieldConditionsConfig from './FieldConditionsConfig' interface StandardFieldOption { key: string label: string } interface LayoutOption { value: string label: string } interface TemplateFieldsEditorProps { fields: FieldForm[] dragFieldIndex: number | null dragHover: DragHoverState | null editingFieldIndex: number | null standardFields: StandardFieldOption[] getStandardFieldTemplate?: (key: string) => FieldForm | null fieldTypes: string[] layoutOptions: LayoutOption[] lockedCategoryFields?: Set onEditingFieldChange: (index: number | null) => void onFieldDragStart: (event: DragEvent, index: number) => void onFieldDragOver: (event: DragEvent, index: number, surface: 'editor') => void onFieldDrop: (event: DragEvent) => void onFieldDragEnd: () => void onContainerDragOver: (event: DragEvent) => void onContainerDrop: (event: DragEvent) => void onContainerDragLeave: (event: DragEvent) => void onMoveField: (index: number, direction: 'up' | 'down') => void onRemoveField: (index: number) => void onDuplicateField?: (index: number) => void onAddField: () => void renderDropPlaceholder: (key: string) => React.ReactNode onUpdateField: (index: number, patch: Partial) => void } const editorInputClass = 'w-full px-3 py-2 text-sm rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-900 text-slate-900 dark:text-white placeholder:text-slate-400 outline-none transition-colors duration-150 focus:border-slate-500 dark:focus:border-slate-500 focus:ring-2 focus:ring-slate-200 dark:focus:ring-slate-700' const editorCheckboxClass = 'w-3.5 h-3.5 rounded border-slate-300 dark:border-slate-600 text-slate-700 focus:ring-2 focus:ring-slate-300 dark:focus:ring-slate-600' // 字段key验证函数 function validateFieldKey(key: string, currentIndex: number, allFields: FieldForm[]): { valid: boolean error?: string warning?: string } { const trimmedKey = key.trim() // 空值检查 if (!trimmedKey) { return { valid: false, error: '字段键名不能为空' } } // 格式检查:只允许字母、数字、下划线,且必须以字母开头 const keyPattern = /^[a-zA-Z][a-zA-Z0-9_]*$/ if (!keyPattern.test(trimmedKey)) { return { valid: false, error: '键名必须以字母开头,只能包含字母、数字和下划线' } } // 长度检查 if (trimmedKey.length > 50) { return { valid: false, error: '键名长度不能超过50个字符' } } // 唯一性检查 const duplicateIndex = allFields.findIndex( (f, idx) => idx !== currentIndex && f.key.trim() === trimmedKey ) if (duplicateIndex !== -1) { return { valid: false, error: `键名重复:字段 ${duplicateIndex + 1} 已使用此键名` } } // 警告:建议使用驼峰命名 if (trimmedKey.includes('_') && trimmedKey.toLowerCase() === trimmedKey) { return { valid: true, warning: '建议使用驼峰命名(如 productModel)而非下划线命名' } } return { valid: true } } export default function TemplateFieldsEditor({ fields, dragFieldIndex, dragHover, editingFieldIndex, standardFields, getStandardFieldTemplate, fieldTypes, layoutOptions, lockedCategoryFields, onEditingFieldChange, onFieldDragStart, onFieldDragOver, onFieldDrop, onFieldDragEnd, onContainerDragOver, onContainerDrop, onContainerDragLeave, onMoveField, onRemoveField, onDuplicateField, onAddField, renderDropPlaceholder, onUpdateField, }: TemplateFieldsEditorProps) { // 删除确认对话框状态 const [deleteConfirmIndex, setDeleteConfirmIndex] = useState(null) // 实时验证所有字段的key const fieldValidations = useMemo(() => { return fields.map((field, index) => validateFieldKey(field.key, index, fields)) }, [fields]) // 复制字段功能 const handleDuplicateField = (index: number) => { if (onDuplicateField) { onDuplicateField(index) } } // 删除字段(带确认) const handleDeleteField = (index: number) => { if (fields.length === 1) { // 不显示确认对话框,直接提示 return } setDeleteConfirmIndex(index) } const confirmDelete = () => { if (deleteConfirmIndex !== null) { onRemoveField(deleteConfirmIndex) setDeleteConfirmIndex(null) } } const cancelDelete = () => { setDeleteConfirmIndex(null) } return (

字段配置

添加用户上传时需要填写的字段

先选预设再按需调整,使用上下箭头按钮调整字段顺序。

{fields.map((field, index) => { const isDragging = dragFieldIndex === index const placeholderBefore = dragFieldIndex !== null && dragHover && dragHover.index === index && dragHover.position === 'before' const placeholderAfter = dragFieldIndex !== null && dragHover && dragHover.index === index && dragHover.position === 'after' const standardOption = standardFields.find((opt) => opt.key === field.key) const selectValue = standardOption ? standardOption.key : '__custom' // 判断是否为下拉选择类型 - 使用多种方式确保准确性 const fieldTypeStr = String(field.type || '').trim() const isSelectType = fieldTypeStr === 'select' const isLockedCategoryField = Boolean(lockedCategoryFields?.has(field.key)) // 获取当前字段的验证结果 const validation = fieldValidations[index] const hasError = !validation.valid const hasWarning = validation.valid && validation.warning return ( {placeholderBefore && renderDropPlaceholder(`editor-before-${index}`)}
onFieldDragStart(e, index)} onDragOver={(e) => onFieldDragOver(e, index, 'editor')} onDrop={onFieldDrop} onDragEnd={onFieldDragEnd} onMouseEnter={() => onEditingFieldChange(index)} onMouseLeave={() => onEditingFieldChange(null)} >
{/* 拖拽手柄 */}
字段 {index + 1} {field.label || field.key || '未命名字段'} {/* 验证状态图标 */} {hasError && ( )} {hasWarning && ( )}
{/* 复制按钮 */}
{/* 验证错误/警告提示 */} {(hasError || hasWarning) && (
{hasError ? ( ) : ( )} {hasError ? validation.error : validation.warning}
)}
{selectValue === '__custom' ? ( { e.stopPropagation() onUpdateField(index, { key: e.target.value }) }} onClick={(e) => e.stopPropagation()} className={editorInputClass} placeholder="例如:productModel" autoComplete="off" /> ) : (

键名:{field.key}

)}
{ e.stopPropagation() onUpdateField(index, { label: e.target.value, autoLabel: false }) }} onClick={(e) => e.stopPropagation()} className={editorInputClass} placeholder="例如:商品型号" autoComplete="off" />
{/* 选项管理提示 - 仅在select或radio类型时显示 */} {(isSelectType || field.type === 'radio') && (

勾选“添加到分类”后,选项可在分类管理中统一维护。

)}
{/* 添加到分类勾选框 - 仅在下拉选择类型时显示 */} {isSelectType ? ( ) : null}
{/* 验证规则配置 */} onUpdateField(index, { validations })} /> {/* 条件显示配置 */} ({ key: f.key, label: f.label || f.key }))} onChange={(conditions) => onUpdateField(index, { conditions })} />
{placeholderAfter && renderDropPlaceholder(`editor-after-${index}`)}
) })}
{/* 删除确认对话框 */} {deleteConfirmIndex !== null && (

确认删除字段

确定要删除字段「{fields[deleteConfirmIndex]?.label || fields[deleteConfirmIndex]?.key || '未命名字段'}」吗?

此操作不可撤销,已使用此模板的素材将无法显示该字段的数据。

)}
) }