# Repeating sections

> Let users add any number of entries, such as claim line items, dependants or previous employers, with per-entry expressions, totals across entries and limits.

A `dynamicpanel` is a section that users repeat as often as they need: one entry per damaged item, per dependant, per previous job. Each entry has the same fields, and the answers are stored as an array of objects.

## A claim with line items

```ts title="home-insurance-claim.ts"
import type { FormSchema } from '@birchui/forms';

export const homeClaim: FormSchema = {
  title: 'Home contents claim',
  elements: [
    { type: 'text', name: 'policyNumber', title: 'Policy number', required: true, pattern: 'HM-[0-9]{6}' },
    { type: 'date', name: 'dateOfLoss', title: 'Date of loss', required: true },
    {
      type: 'dynamicpanel',
      name: 'lineItems',
      title: 'Damaged or stolen items',
      description: 'Add each item you are claiming for.',
      templateTitle: 'Item {panelIndex}: {description}',
      addText: 'Add another item',
      removeText: 'Remove item',
      required: true,
      minPanels: 1,
      maxPanels: 25,
      elements: [
        { type: 'text', name: 'description', title: 'Item', required: true, width: 'half', placeholder: 'e.g. 55-inch television' },
        { type: 'number', name: 'amount', title: 'Replacement cost (GBP)', required: true, min: 1, width: 'half' },
        { type: 'toggle', name: 'hasReceipt', title: 'I have a receipt' },
        { type: 'file', name: 'receipt', title: 'Receipt', visibleIf: '{hasReceipt} = true', accept: '.pdf,image/*' },
      ],
    },
    {
      type: 'computed',
      name: 'claimTotal',
      title: 'Total claimed',
      expression: 'sum({lineItems.amount})',
      format: 'currency',
      currency: 'GBP',
    },
  ],
};
```

The data looks like this:

```ts
{
  policyNumber: 'HM-482913',
  dateOfLoss: '2026-11-08',
  lineItems: [
    { description: '55-inch television', amount: 899, hasReceipt: true, receipt: [/* File */] },
    { description: 'Laptop', amount: 1249 },
  ],
  claimTotal: 2148,
}
```

## Properties

**`DynamicPanelElement`** (interface from `@birchui/forms`, full reference: https://docs.birchui.com/api/forms.md)

```ts
interface DynamicPanelElement extends ElementBase
```

| Property | Type | Description |
| --- | --- | --- |
| `type` | `'dynamicpanel'` |  |
| `name` | `string` |  |
| `elements` | `ElementSchema[]` |  |
| `templateTitle?` | `LocalizedString` |  |
| `minPanels?` | `number` |  |
| `maxPanels?` | `number` |  |
| `initialPanels?` | `number` |  |
| `addText?` | `LocalizedString` |  |
| `removeText?` | `LocalizedString` |  |
| `required?` | `boolean` |  |
| `title?` | `LocalizedString \| undefined` | Inherited from ElementBase. |
| `description?` | `LocalizedString \| undefined` | Inherited from ElementBase. |
| `visibleIf?` | `string \| undefined` | Inherited from ElementBase. |
| `enableIf?` | `string \| undefined` | Inherited from ElementBase. |
| `width?` | `FieldWidth \| undefined` | Inherited from ElementBase. |

| Property | Effect |
| --- | --- |
| `name` | The key of the array in the data. Required. |
| `elements` | The fields of one entry. Their names only need to be unique within the entry. |
| `templateTitle` | The heading of each entry. `{panelIndex}` is the entry number and `{fieldName}` inserts an answer of that entry. Defaults to the section title followed by the number, such as "Damaged or stolen items 2". |
| `addText`, `removeText` | Button labels. They default to "Add {title}" and "Remove". |
| `minPanels` | The minimum number of entries. **Remove** is disabled at the minimum. |
| `maxPanels` | The maximum number of entries. **Add** is disabled at the maximum. |
| `initialPanels` | How many empty entries to show at first, when `data` has none. Defaults to `minPanels`, or 0. |
| `required` | At least one entry is required. |

New entries start with the `defaultValue` of each field.

## Expressions inside an entry

Expressions on fields inside an entry are evaluated for that entry:

| Reference | Meaning |
| --- | --- |
| `{amount}` | A field of the current entry. Names of the entry's fields take precedence over form fields with the same name. |
| `{panel.amount}` | The same, written explicitly. |
| `{panelIndex}` | The entry's number, starting at 1. |
| `{parentPanel.amount}` | A field of the enclosing entry, when sections are nested. |
| `{dateOfLoss}` | Any other name refers to the form's top-level answers. |

In the example, the receipt upload appears per entry, only for items whose `hasReceipt` toggle is on. A validator can compare fields of the same entry:

```ts
{
  type: 'date',
  name: 'endDate',
  title: 'To',
  validators: [{ type: 'expression', expression: '{panel.endDate} >= {panel.startDate}', message: 'End after the start date.' }],
}
```

## Totals and checks across entries

Outside the section, `{lineItems.amount}` is a list of the `amount` of every entry, and `{lineItems}` is the whole array. Combine them with list functions:

```text
sum({lineItems.amount})
count({lineItems})
max({lineItems.amount}) > 5000
count({lineItems}) >= 10
```

A form-level rule can then react, for example a declaration that appears only for claims over a threshold:

```ts
{
  type: 'checkbox',
  name: 'highValueDeclaration',
  title: 'I understand that items over £2,500 need an inspection before payment.',
  visibleIf: 'max({lineItems.amount}) > 2500',
  required: true,
}
```

## Validation

Each field in each entry is validated on its own. Errors use the field's data path as the name, such as `lineItems[1].amount`, and the error summary links straight to that field. The section itself checks `required`, `minPanels` and `maxPanels`, with messages such as "Add at least 2 entries."

## Add and remove entries from code

| Method | Purpose |
| --- | --- |
| `addPanel(name)` | Adds an empty entry to the section and focuses its first field. |
| `removePanel(name, index)` | Removes the entry at `index`, counted from 0. |
| `getValue(path)`, `setValue(path, value)` | Read or write any value, including inside entries: `form.setValue('lineItems[0].amount', 949)`. |

`addPanel()` respects `maxPanels`. `removePanel()` removes the entry even below `minPanels`, and validation then reports the shortfall. Both emit `valueChange` with the section's name and its new array. Pre-fill entries by passing an array in `data`:

```ts
createForm(container, {
  schema: homeClaim,
  data: {
    policyNumber: 'HM-482913',
    lineItems: [
      { description: '55-inch television', amount: 899 },
      { description: 'Soundbar', amount: 279 },
    ],
  },
});
```

## Accessibility

Each entry is a `section` labelled by its heading. Remove buttons include the entry title in their accessible name, such as "Remove item: Item 2: Laptop", so screen reader users know which entry they remove. After adding an entry, focus moves to its first field; after removing one, focus returns to the **Add** button.

## Panels that do not repeat

To group fields without repeating them, use a `panel`. A panel's fields write to the same data object as the fields around it, while a repeating section always writes an array. See [Field types](https://docs.birchui.com/forms/field-types.md#panel).
