Skip to content

List

List<T> manages a dynamic array of Group<T> items. It supports adding, removing, and replacing items while triggering the same action/validation system as Field and Group.

Creating a list

typescript
import { Field, Group, List } from '@dynamicforms/vue-forms';

// Define the item template
const itemTemplate = new Group({
  name:  Field.create({ value: '' }),
  score: Field.create<number>({ value: 0 }),
});

// Empty list
const list = new List(itemTemplate);

// Pre-populated list
const list2 = new List(itemTemplate, {
  value: [
    { name: 'Alice', score: 95 },
    { name: 'Bob',   score: 80 },
  ],
});

new List(itemTemplate?, params?)

ParameterTypeDefaultDescription
itemTemplateGroup<T>undefinedTemplate cloned for each new item. If omitted, Group.createFromFormData is used for plain objects.
params.valueRecord<string, any>[][]Initial array of item values
params.originalValueRecord<string, any>[]same as valueBaseline for isChanged
params.enabledbooleantrueWhether the list accepts mutations
params.visibilityDisplayModeDisplayMode.FULLRendering visibility hint
params.validatorsIFieldAction[][]List-level validators
params.actionsIFieldAction[][]List-level actions

Properties

PropertyTypeWritableDescription
valueRecord<string, any>[] | nullyesSerialized array of enabled item values; null when empty
reactiveValueComputedRef<...>noVue computed ref of value
originalValueRecord<string, any>[] | nullnoValue at creation time
isChangedbooleannotrue when value differs from originalValue
validbooleannotrue when the list itself and all items are valid
validatingbooleannotrue while any item has a pending async validator
errorsValidationError[]noList-level validation errors
enabledbooleanyesRendering/serialization hint
visibilityDisplayModeyesRendering visibility hint
touchedbooleanyestrue when any item has been touched; setting propagates to all items

Methods

get(index): Group<T> | undefined

Returns the Group instance at index, or undefined if out of range.

push(item): number

Appends an item to the end of the list. item may be a plain object or an existing Group. Returns the new length of the list. Triggers ListItemAddedAction.

typescript
list.push({ name: 'Charlie', score: 70 });

pop(): Group<T> | undefined

Removes and returns the last item. Triggers ListItemRemovedAction.

insert(item, index): number

Inserts item at index. If index is beyond the current length, fills with empty items first. Returns the actual insertion index. Triggers ListItemAddedAction.

remove(index): Group<T> | undefined

Removes the item at index and returns a detached clone of it. Triggers ListItemRemovedAction.

clear()

Removes all items and triggers a value-changed notification.

registerAction(action): this

Registers an action on the list. Returns this.

validate(revalidate?): void

Validates the list. Pass revalidate: true to cascade to all items.

clone(overrides?): List<T>

Returns a new List with a cloned item template and actions.


See also: Actions reference for ListItemAddedAction and ListItemRemovedAction

Released under the MIT License.