# Parts on fieldsets

> Rules and other parts that travel with a fieldset to every form that uses it.

Source: https://forms.lunarhue.dev/solid/plugin-composition/fieldset-parts

A fieldset can carry parts too. The billing fieldset below brings its own
rule: untick "Same as shipping" and the billing address appears.

```tsx title="billing.tsx"
import { createForm } from "@lunarhue/forms-solid";
import { BillingFields } from "./BillingFields";
import { BillingFieldset, forms } from "./forms";

const order = forms.define(
  {
    defaultValues: {
      shipping: { street: "", city: "" },
      billing: { sameAsShipping: true, address: { street: "", city: "" } },
    },
    onSubmit: (value) => value,
  },
  // Pinned: the rule attaches when the store is made, not when the
  // fieldset's scope is first read.
  BillingFieldset.at("billing"),
);

export default function BillingForm() {
  const form = createForm(order, { id: "billing" });
  return (
    <form.Form>
      {(formProps) => (
        <form {...formProps}>
          <form.TextField name="shipping.street" label="Shipping street" />
          <form.TextField name="shipping.city" label="Shipping city" />
          <BillingFields field={form.root.at("billing")} />
          <button type="submit">Order</button>
        </form>
      )}
    </form.Form>
  );
}
```

```tsx title="BillingFields.tsx"
import { type HandleOf, inputProps } from "@lunarhue/forms-solid";
import { rules } from "@lunarhue/forms-solid/plugins";
import { Show } from "solid-js";
import { BillingFieldset } from "./forms";

export function BillingFields(props: { field: HandleOf<typeof BillingFieldset> }) {
  const scope = BillingFieldset.scope(props.field);
  return (
    <fieldset>
      <legend>Billing</legend>
      <scope.Field name="sameAsShipping">
        {(same) => (
          <label>
            <input {...inputProps(same, { kind: "checkbox" })} />
            Same as shipping
          </label>
        )}
      </scope.Field>
      {/* The rule hides `address`. The component decides what hidden means. */}
      <scope.Field name="address">
        {(address) => (
          <Show when={!rules.meta(address).hidden}>
            <scope.TextField name="address.street" label="Billing street" />
            <scope.TextField name="address.city" label="Billing city" />
          </Show>
        )}
      </scope.Field>
    </fieldset>
  );
}
```

```ts title="forms.ts"
import { createForms } from "@lunarhue/forms-solid";
import { ruled, rules } from "@lunarhue/forms-solid/plugins";
import { TextField } from "./TextField";

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

export interface Address {
  street: string;
  city: string;
}

export interface Billing {
  sameAsShipping: boolean;
  address: Address;
}

// The rule travels with the fieldset. It gets the fieldset's value, not the form's.
export const BillingFieldset = forms.fieldset<Billing, "rules">(
  ruled({ address: (billing) => ({ hidden: billing.sameAsShipping }) }),
);
```

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

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

## Rules on a fieldset

Pass parts after the schema, or after the type argument when there is no
schema. With a type argument, name the plugins the parts need as the second
one:

```ts
const BillingFieldset = forms.fieldset<Billing, "rules">(
  ruled({ address: (billing) => ({ hidden: billing.sameAsShipping }) }),
);

const Billing = forms.fieldset(BillingSchema, ruled({ address: (billing) => ({ hidden: billing.sameAsShipping }) }));
```

A fieldset's rules get the fieldset's value, not the form's, and name fields
below it. To write rules apart from the fieldset, name the value type:
`ruled<Billing>({ ... })`.

## Pinning with at()

A fieldset's parts attach at each field it is used on, the first time its
scope is read. A rule that matters before that, such as one on a step the
user hasn't reached, can be pinned on the definition with `at`:

```ts
const order = forms.define({ defaultValues, onSubmit }, BillingFieldset.at("billing"));
```

They then attach when the store is made. Reading the scope later doesn't
attach them twice.

`at` takes a field name, so a typo is a type error. A field type has the same
method for its parts: see [Parts on field components](https://forms.lunarhue.dev/solid/plugin-composition/field-parts.md).
