# ivyforms/0.7/frontend/src/utils/fieldUtils.ts

The Innovative Form Builder – IvyForms, version 0.7. 38 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.7/code/frontend/src/utils/fieldUtils.ts
- Raw: https://pluginprobe.com/plugins/ivyforms/0.7/raw/frontend/src/utils/fieldUtils.ts
- Modified: 2025-12-25T16:20:26+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.7/code/frontend/src/utils/fieldUtils.ts#L10-L20`.

```typescript
// Utility helpers for field operations used by the builder
import type { Field, NameSubField, AddressSubField } from '@/types/field'

export function duplicateFieldForStore(
  original: Field,
  newIndex: number,
  getLabel: (k: string) => string,
): Field {
  const origClone = JSON.parse(JSON.stringify(original)) as Field
  const copyBase: Field = {
    ...origClone,
    id: 0,
    fieldIndex: newIndex,
    label: `${origClone.label}${getLabel('copy_label')}`,
    limitMaxLength: origClone.limitMaxLength ?? false,
    maxLength: origClone.maxLength ?? 255,
    labelPosition: origClone.labelPosition ?? 'default',
    noDuplicates: origClone.noDuplicates ?? false,
    inputPrefix: origClone.inputPrefix || '',
    inputSuffix: origClone.inputSuffix || '',
  }

  // If it's name/address parent-child, ensure subfields are duplicated and ids reset
  if (origClone.type === 'name') {
    const sub = (origClone.nameFields ?? []) as NameSubField[]
    copyBase.nameFields = sub.map((nf) => ({ ...nf, id: '' }))
    // also ensure nameFieldTypes are recomputed to match
    copyBase.nameFieldTypes = (copyBase.nameFields || []).map((_, idx) => `nameField${idx + 1}`)
  } else if (origClone.type === 'address') {
    const sub = (origClone.addressFields ?? []) as AddressSubField[]
    copyBase.addressFields = sub.map((af) => ({ ...af, id: '' }))
    // recompute addressFieldTypes from addressFields
    copyBase.addressFieldTypes = (copyBase.addressFields || []).map((af) => af.type)
  }

  return copyBase
}

```
