1. Meta Elements
  2. Array

Meta Elements

Array

Array element

Configuration

The array element inherits all the attributes of BaseElement

In addition, it has the following attributes:

Attribute Required Description Type Example
name Yes Name of the field string aliases
schema Yes Validation schema ZodArray zod.array(zod.string())
element Yes The sub-element schema Element {type: 'header', text: 'Hello'}
count Yes Count of array elements Resolvable<number> 4

Example

{}
SVELTE
<script lang="ts">
  import { Base, Form } from '$lib';
  import { get, writable } from 'svelte/store';
  import * as zod from 'zod';

  const count = writable(1);

  const form = Base.newForm([
    {
      type: 'array',
      name: 'aliases',
      schema: zod.array(zod.boolean()),
      count,
      element: {
        type: 'input',
        label: 'Alias',
        schema: zod.boolean()
      }
    },
    {
      type: 'button',
      text: '+',
      click: () => count.set(get(count) + 1),
      params: {
        class: 'px-3 py-1 bg-green-700 text-white rounded mr-1'
      }
    },
    {
      type: 'button',
      text: '-',
      click: () => count.set(get(count) - 1),
      params: () => {
        const base = {
          class: 'px-3 py-1 bg-red-700 text-white rounded disabled:bg-neutral-600'
        };

        if (get(count) <= 0) {
          return { ...base, disabled: true };
        }

        return base;
      }
    }
  ] as const);

  const data = form.getData();
</script>

<Form {form} />

<pre>{JSON.stringify($data, null, 4)}</pre>