PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / frontend / src / utils / fieldUtils.ts

fieldUtils.ts in The Innovative Form Builder – IvyForms 1.0, at frontend/src/utils/fieldUtils.ts

38 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // Utility helpers for field operations used by the builder
2 import type { Field, NameSubField, AddressSubField } from '@/types/field'
3
4 export function duplicateFieldForStore(
5 original: Field,
6 newIndex: number,
7 getLabel: (k: string) => string,
8 ): Field {
9 const origClone = JSON.parse(JSON.stringify(original)) as Field
10 const copyBase: Field = {
11 ...origClone,
12 id: 0,
13 fieldIndex: newIndex,
14 label: `${origClone.label}${getLabel('copy_label')}`,
15 limitMaxLength: origClone.limitMaxLength ?? false,
16 maxLength: origClone.maxLength ?? 255,
17 labelPosition: origClone.labelPosition ?? 'default',
18 noDuplicates: origClone.noDuplicates ?? false,
19 inputPrefix: origClone.inputPrefix || '',
20 inputSuffix: origClone.inputSuffix || '',
21 }
22
23 // If it's name/address parent-child, ensure subfields are duplicated and ids reset
24 if (origClone.type === 'name') {
25 const sub = (origClone.nameFields ?? []) as NameSubField[]
26 copyBase.nameFields = sub.map((nf) => ({ ...nf, id: '' }))
27 // also ensure nameFieldTypes are recomputed to match
28 copyBase.nameFieldTypes = (copyBase.nameFields || []).map((_, idx) => `nameField${idx + 1}`)
29 } else if (origClone.type === 'address') {
30 const sub = (origClone.addressFields ?? []) as AddressSubField[]
31 copyBase.addressFields = sub.map((af) => ({ ...af, id: '' }))
32 // recompute addressFieldTypes from addressFields
33 copyBase.addressFieldTypes = (copyBase.addressFields || []).map((af) => af.type)
34 }
35
36 return copyBase
37 }
38