# Form layout components

Primitives for typical form layout with responsive label positioning and data binding.

---

## `ixfx-form`

Main container for form sections. It wraps its content in a native `<form>`, so submit, Enter-to-submit, validation, `FormData` and `form.elements` work as they do for any form. It also provides `getValues()` and `setValues()` for data binding.

```html
<ixfx-form>
  <ixfx-form-group label="Section A">
    <p>Preamble text for section A...</p>
    <ixfx-form-part label="Some text">
      <input type="text" name="input1" required />
    </ixfx-form-part>
    <ixfx-form-part label="Some other text">
      <input type="text" name="input2" />
    </ixfx-form-part>
  </ixfx-form-group>
  <ixfx-button type="submit">Save</ixfx-button>
</ixfx-form>
```

```js
form.addEventListener('submit', (e) => {
  console.log(form.getValues({ typed: true }));
});
```

### Properties / attributes

| Attribute / Property | Type | Default | Description |
|---------------------|------|---------|-------------|
| `label-align` / `labelAlign` | `'left' \| 'right'` | `'right'` | Label justification in the wide layout. Applies to all groups unless a group overrides it |
| `novalidate` / `noValidate` | boolean | `false` | Skip validation on submit, like `<form novalidate>`. `reportValidity()` still validates |

### Methods and properties

| Member | Returns | Description |
|--------|---------|-------------|
| `getValues(options?)` | `Record<string, unknown>` | Collects values from all named controls. Checkboxes give booleans; all other values are strings, including `type=number`. With `{ typed: true }`, `number` and `range` inputs give numbers (`null` when empty) |
| `setValues(data)` | `void` | Applies values to named inputs. Keys absent from `data` are left untouched. |
| `requestSubmit()` | `void` | Validates (unless `novalidate`), then fires `submit` |
| `checkValidity()` | `boolean` | True if every native control is valid |
| `reportValidity()` | `boolean` | As `checkValidity()`, and shows the browser's message on the first invalid control |
| `reset()` | `void` | Resets native controls |
| `form` | `HTMLFormElement` | The native form holding the content |
| `elements` | `HTMLFormControlsCollection` | The native form's controls |

### Submitting

- `submit` is the standard event: it bubbles, and carries no navigation (`ixfx-form` always cancels the default action, because there is no `action`).
- Submit with a native `<button type="submit">`, `<ixfx-button type="submit">`, `requestSubmit()`, or Enter in a text field.
- Validation uses the browser's: `required`, `pattern`, `min`, `max`, `type=email` and so on.

### What is and isn't a native control

Native `<input>`, `<select>` and `<textarea>` take part in validation, `FormData`, `elements` and `reset()`. Components with their own shadow root (ranges, colour pickers and so on) do not: the browser can't see inside them. `getValues()` and `setValues()` still handle them through their `.value`, but validation and `reset()` skip them.

Don't nest an `ixfx-form` inside another `<form>`: nested forms are invalid HTML.

### Styling inside a shadow root

The form components are light DOM, so a global stylesheet can't reach them inside another component's shadow root. They add their own layout styles to the document, or to the shadow root they are in, when they connect. Consumers need no workaround. The styles are placed first, so your own CSS wins ties. The package also exports `@ixfx/components/style.css` for the global theme variables.

### Value collection rules

- Native `<input type=radio>` sharing a `name` → value of the checked one
- Native/checked-bearing checkbox → boolean
- `<select multiple>` → array of selected values
- Everything else with a `.value` property → used as-is

---------------------|------|---------|-------------|
| `label-align` / `labelAlign` | `'left' \| 'right'` | `'right'` | Label justification in the wide layout. Applies to all groups unless a group overrides it |

### Methods

| Method | Returns | Description |
|--------|---------|-------------|
| `getValues()` | `Record<string, unknown>` | Collects values from all named inputs within the form |
| `setValues(data)` | `void` | Applies values to named inputs. Keys absent from `data` are left untouched. |

### Value collection rules

- Native `<input type=radio>` sharing a `name` → value of the checked one
- Native/checked-bearing checkbox → boolean
- `<select multiple>` → array of selected values
- Everything else with a `.value` property → used as-is

---

## `ixfx-form-group`

A section within the form, backed by a `<fieldset>`/`<legend>` for proper accessibility semantics.

```html
<ixfx-form-group label="Personal Information">
  <ixfx-form-part label="Full Name">
    <input type="text" name="name" />
  </ixfx-form-part>
  <ixfx-form-part label="Email">
    <input type="email" name="email" />
  </ixfx-form-part>
</ixfx-form-group>
```

### Properties / attributes

| Attribute / Property | Type   | Default | Description |
|---------------------|--------|---------|-------------|
| `label`            | string | `''`    | Section heading text |
| `label-align` / `labelAlign` | `'left' \| 'right'` | inherited | Overrides the form's label justification for this group |

---

## `ixfx-form-part`

Wraps a form control with a label. Uses light DOM so the `<label for>` natively associates with the slotted input.

```html
<ixfx-form-part label="Username">
  <input type="text" name="username" />
</ixfx-form-part>
```

### Properties / attributes

| Attribute / Property | Type   | Default | Description |
|---------------------|--------|---------|-------------|
| `label`            | string | `''`    | Label text displayed for the control |

### How it works

`ixfx-form-part` generates an `<label for="...">` element that is associated with the first control-like child (`<input>`, `<select>`, `<textarea>`, or element with a `name` attribute). The association works natively because both elements live in the same light DOM tree — no shadow boundary to work around.

An `id` is generated for the control if it doesn't already have one.

---

## `ixfx-form-row`

Lets two or more `ixfx-form-part` elements share a row. Must be a direct child of `ixfx-form-group`.

```html
<ixfx-form-group label="Address">
  <ixfx-form-part label="Street"><input name="street" /></ixfx-form-part>
  <ixfx-form-row>
    <ixfx-form-part label="City"><input name="city" /></ixfx-form-part>
    <ixfx-form-part label="Postcode"><input name="postcode" /></ixfx-form-part>
  </ixfx-form-row>
</ixfx-form-group>
```

The group counts the pairs in its widest row (`--ixfx-form-pairs`) and builds a grid of `label | input` columns that many times. Full-width parts span their input across the remaining columns, and all rows (including separate shared-row blocks) line up via subgrid.

- **Wide:** `label1 input1 label2 input2`
- **Narrow:** labels on one line, inputs below, one column per pair

---

## `ixfx-form-options`

Groups option parts (typically radio buttons sharing a `name`) under one label. Must be a direct child of `ixfx-form-group`.

```html
<ixfx-form-options label="Contact method">
  <ixfx-form-part label="Email"><input type="radio" name="contact" value="email" /></ixfx-form-part>
  <ixfx-form-part label="Phone"><input type="radio" name="contact" value="phone" /></ixfx-form-part>
</ixfx-form-options>
```

| Attribute / Property | Type | Default | Description |
|---------------------|------|---------|-------------|
| `label` | string | `''` | Group label, shown in the label column (wide) or above the options (narrow) |
| `orientation` | `'horizontal' \| 'vertical'` | `'horizontal'` | Horizontal: each option is the control with its label centred beneath, flowing and wrapping as space runs out. Vertical: control then label, one option per line |

Gap between horizontal options: `--ixfx-form-option-gap` (default `var(--space-l)`). The element has `role="group"` labelled by its `label`.

---

## Responsive behaviour

The layout switches between two modes based on available width (via CSS container queries):

**Narrow (< 30rem):** Label appears above the input, single-column stacked layout.

**Wide (≥ 30rem):** Label appears to the left of the input, two-column grid. All labels in a group share the width of the widest label (right-justified by default; set `label-align="left"` on the form or a group to left-justify).

Resize the container to see the transition.

In the wide layout the gap between a label and its input is `--ixfx-form-label-gap` (default `var(--space-s)`), and the gap between pairs in a shared row is `--ixfx-form-pair-gap` (default `var(--space-l)`).

---

## TypeScript types

```ts
import type { FormElement, FormGroupElement, FormPartElement } from 'ixfxfun-components';
```
