| 1 |
import type { Field } from '@/types/field' |
| 2 |
import { parentChildFieldConfigs } from './parentChildFieldTemplates' |
| 3 |
|
| 4 |
export function buildNewField( |
| 5 |
fieldData: Partial<Field> & { type: string }, |
| 6 |
index: number, |
| 7 |
getLabel: (key: string) => string, |
| 8 |
): Field { |
| 9 |
const config = parentChildFieldConfigs[fieldData.type] |
| 10 |
if (config) { |
| 11 |
// Parent-child field type |
| 12 |
const subFields = config.buildSubFields(getLabel) |
| 13 |
return { |
| 14 |
...(fieldData as Field), |
| 15 |
id: fieldData.id ?? 0, |
| 16 |
formId: fieldData.formId ?? 0, |
| 17 |
fieldIndex: index, |
| 18 |
type: config.type, |
| 19 |
label: fieldData.label ?? getLabel(config.type), |
| 20 |
defaultValue: '', |
| 21 |
value: '', |
| 22 |
required: false, |
| 23 |
[config.subFieldKey]: subFields, |
| 24 |
limitMaxLength: fieldData.limitMaxLength || false, |
| 25 |
maxLength: fieldData.maxLength || 255, |
| 26 |
labelPosition: fieldData.labelPosition || 'top', |
| 27 |
noDuplicates: fieldData.noDuplicates || false, |
| 28 |
} as Field |
| 29 |
} |
| 30 |
// Regular field type |
| 31 |
return { |
| 32 |
...(fieldData as Field), |
| 33 |
id: fieldData.id ?? 0, |
| 34 |
formId: fieldData.formId ?? 0, |
| 35 |
fieldIndex: index, |
| 36 |
defaultValue: '', |
| 37 |
value: '', |
| 38 |
required: false, |
| 39 |
limitMaxLength: fieldData.limitMaxLength || false, |
| 40 |
maxLength: fieldData.maxLength || 255, |
| 41 |
labelPosition: fieldData.labelPosition || 'top', |
| 42 |
noDuplicates: fieldData.noDuplicates || false, |
| 43 |
confirmFieldEnabled: fieldData.confirmFieldEnabled || false, |
| 44 |
confirmFieldLabel: '', |
| 45 |
confirmFieldPlaceholder: '', |
| 46 |
confirmFieldHideLabel: fieldData.confirmFieldEnabled || false, |
| 47 |
} |
| 48 |
} |
| 49 |
|