# Field

A control with its label, hint and error. Built on ui-sdk's Field: the label names the control, the hint describes it, and the error marks it invalid and is announced. The error sits in the gap under the control, aligned to its end edge, so showing it never moves the form.

- Beta: ready to use; its API can still change in a minor release.
- Category: Form fields

```tsx
import { Field, Input } from "@leap-crm/design-system";
```

## Best practices

- Do: Wrap every form control in a `Field`, like an `Input`, a `Textarea` or a `Select`, so the label names it and the error is announced.
- Do: Write `error` as what to do, like "Please enter a valid email". It takes the hint's place or sits in the gap under the control, so the form doesn't move.
- Do: Set `invalid` with no message on a field whose partner holds the error, like a start time when the end time says it must be later.
- Do: Put a status badge or an info tooltip in `labelAside`, not in `label`, so it sits on the label's line without becoming part of the control's name.
- Don't: Show `CharacterCount` from the first character on a long limit. Set `showFrom` near the limit: a counter over every short entry nags.
- Don't: Dim a field yourself with opacity. Set `disabled`, which disables the control and dims the whole field together.

## Related

- [Label](./label.md)
- [Input](./input.md)
- [Textarea](./textarea.md)
- [Select](./select.md)

## Examples

What the examples import:

```tsx
import { type SubmitEvent, useState } from "react";
import { Button, CharacterCount, Field, Input } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

function ValidationDemo() {
  const [email, setEmail] = useState("jane@citizen");
  const [submitted, setSubmitted] = useState(false);
  const error =
    submitted && !emailPattern.test(email)
      ? "Please enter a valid email"
      : undefined;
  const submit = (event: SubmitEvent<HTMLFormElement>) => {
    event.preventDefault();
    setSubmitted(true);
  };
  return (
    <form noValidate onSubmit={submit} className="flex w-72 flex-col gap-6">
      <Field label="Email" error={error}>
        <Input type="email" value={email} onValueChange={setEmail} required />
      </Field>
      <Field label="Phone" optional>
        <Input type="tel" />
      </Field>
      <Button type="submit" className="self-start">
        Save
      </Button>
    </form>
  );
}

function CounterDemo() {
  const [summary, setSummary] = useState("Settlement booked");
  return (
    <div className="w-72">
      <Field
        label="Summary"
        hint="Shown on the opportunity card."
        counter={
          <CharacterCount length={summary.length} max={40} showFrom={10} />
        }
      >
        <Input value={summary} onValueChange={setSummary} maxLength={40} />
      </Field>
    </div>
  );
}
```

### Label

14 px 450 muted, 8 px above the control. The red asterisk follows the control's own required, so it's never out of step; optional adds a muted Optional.

```tsx
<div className="flex w-72 flex-col gap-6">
  <Field label="Full name">
    <Input defaultValue="Jane Citizen" required />
  </Field>
  <Field label="Phone" optional>
    <Input type="tel" />
  </Field>
</div>
```

### Validation

Press Save. The error appears 4 px under the field without moving Phone, takes the red edge and is announced. Tab back in for the red focus.

```tsx
<ValidationDemo />
```

### Hint and counter

A hint sits under the control; an error replaces it in place. The counter appears once the text is near the limit and turns red at it.

```tsx
<CounterDemo />
```

### Error with a hint

```tsx
<div className="w-72">
  <Field
    label="Mobile"
    hint="We only use this for appointment reminders."
    error="Please enter a valid mobile number"
  >
    <Input type="tel" defaultValue="0400 12" />
  </Field>
</div>
```

## Props

### Field

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `ReactNode` |  | Names the control. The red asterisk follows the control's own `required`. |
| `optional` | `boolean` | `false` | Shows a muted "Optional" after the label. |
| `labelAside` | `ReactNode` |  | Sits on the label's line after it, like a status badge or an info tooltip. |
| `hint` | `ReactNode` |  | A line under the control. An error takes its place, so nothing moves. |
| `error` | `ReactNode` |  | The error, like "Please enter a valid email". It shows the field as invalid and is announced. |
| `invalid` | `boolean` |  | Shows the field as invalid without a message, like when another field holds the error. |
| `counter` | `ReactNode` |  | At the end of the hint line, like a CharacterCount. |
| `name` | `string` |  | The name the control submits under, when it doesn't set its own. |
| `disabled` | `boolean` |  | Disables the control and dims the whole field. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
| `children` (required) | `ReactNode` |  | The control: an Input, a Textarea or any ui-sdk control. |

### CharacterCount

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `length` (required) | `number` |  | How many characters the text has now. |
| `max` (required) | `number` |  | The limit. The count turns red at it. |
| `showFrom` | `number` | `0` | The length above which it appears. |
