# Reusable lists

> One list component for every array whose items share a shape.

Source: https://forms.lunarhue.dev/react/plugin-composition/lists

A list component shared by many forms takes an array handle. Make it generic
in the item, and it takes lists whose items carry more keys than it knows
about. Both lists below use one component.

```tsx title="skills.tsx"
import { useForm } from "@lunarhue/forms-react";
import { forms } from "./forms";
import { NamedList } from "./NamedList";

const profile = forms.define({
  defaultValues: {
    skills: [{ id: 1, name: "Writing" }],
    languages: [{ id: 2, name: "English", level: "native" }],
  },
  onSubmit: (value) => value,
});

export default function SkillsForm() {
  const form = useForm(profile, { id: "skills" });
  return (
    <form.Form>
      {(formProps) => (
        <form {...formProps}>
          <h3>Skills</h3>
          <NamedList items={form.root.at("skills")} create={(base) => base} />
          <h3>Languages</h3>
          <NamedList items={form.root.at("languages")} create={(base) => ({ ...base, level: "basic" })} />
          <button type="submit">Save</button>
        </form>
      )}
    </form.Form>
  );
}
```

```ts title="forms.ts"
import { createForms } from "@lunarhue/forms-react";
import { arrays } from "@lunarhue/forms-react/plugins";
import { TextField } from "./TextField";

export const forms = createForms({ plugins: [arrays()], fields: { TextField } });

export interface Named {
  id: number;
  name: string;
}

export const NamedFieldset = forms.fieldset<Named>();
```

```tsx title="NamedList.tsx"
import { type ArrayHandle, type FormError, type HandleOf, Items } from "@lunarhue/forms-react";
import { type Named, NamedFieldset } from "./forms";

function NameField<T extends Named>({ field }: { field: HandleOf<typeof NamedFieldset, T> }) {
  const scope = NamedFieldset.scope(field);
  return <scope.TextField name="name" label="Name" />;
}

// Generic in the item, so it takes lists whose items have more keys. It can't
// build those keys itself, so the caller passes `create`.
export function NamedList<T extends Named>({
  items,
  create,
}: {
  items: ArrayHandle<T[], FormError, "arrays">;
  create: (base: Named) => T;
}) {
  return (
    <div>
      <Items handle={items}>
        {(item, index) => (
          <div>
            <NameField field={item} />
            <button type="button" onClick={() => items.remove(index)}>
              Remove
            </button>
          </div>
        )}
      </Items>
      <button type="button" onClick={() => items.push(create({ id: Date.now(), name: "" }))}>
        Add
      </button>
    </div>
  );
}
```

```tsx title="TextField.tsx"
import { type FieldApi, inputProps } from "@lunarhue/forms-react";

export function TextField({ field, label }: { field: FieldApi<string>; label: string }) {
  return (
    <label>
      {label}
      <input {...inputProps(field)} />
    </label>
  );
}
```

## A generic list

Type the prop as `ArrayHandle<T[], FormError, "arrays">`, where `"arrays"`
says it needs the [`arrays()`](https://forms.lunarhue.dev/react/plugins/arrays.md) plugin. The component can't
build the keys it doesn't know, so it takes a function that makes a full item:

```tsx
function NamedList<T extends Named>({
  items,
  create,
}: {
  items: ArrayHandle<T[], FormError, "arrays">;
  create: (base: Named) => T;
}) {
  return (
    <div>
      <Items handle={items}>{(item) => <NameField field={item} />}</Items>
      <button type="button" onClick={() => items.push(create({ id: Date.now(), name: "" }))}>
        Add
      </button>
    </div>
  );
}

<NamedList items={form.root.at("skills")} create={(base) => base} />;
<NamedList items={form.root.at("languages")} create={(base) => ({ ...base, level: "basic" })} />;
```

## Each row

A row's handle is generic too, so render it through a
[generic fieldset](https://forms.lunarhue.dev/react/composition/fieldsets.md#generic-fieldsets):

```tsx
const NamedFieldset = forms.fieldset<Named>();

function NameField<T extends Named>(props: { field: HandleOf<typeof NamedFieldset, T> }) {
  const scope = NamedFieldset.scope(props.field);
  return <scope.TextField name="name" label="Name" />;
}
```
