# Input

A text field with prefix and suffix addons and validation states.

## Label and value

A label sits above, tied to the field by its `id`. `bind:value` keeps a state in step with the field.

## Validation

`required` and `type` are the browser's: submit it empty, or with an invalid email, and the browser stops it. `invalid` marks the field for any other error, the server's included; the message under it is linked by `aria-describedby`.

## Disabled and readonly

A disabled field is out of the form; a readonly one is submitted and can be selected, not changed.

## Prefix and suffix

The `prefix` snippet sits inside the field, before the text; the `suffix` snippet sits after it and can hold a control. Both at once frame a value with its units.

## Examples

```svelte
<script lang="ts">
  import Button from '@qualia-studios/design-ui/components/Button.svelte';
  import Input from '@qualia-studios/design-ui/components/Input.svelte';

  let email = $state('');
  let password = $state('');
  let search = $state('');
  let amount = $state<number | undefined>();
  let showPassword = $state(false);
  let newsletter = $state('');
  let submittedEmail = $state<string | null>(null);
  let nativeSubmitted = $state(false);
  let serverError = $derived(
    submittedEmail && submittedEmail.endsWith('@blocked.dev')
      ? 'This email domain is not allowed.'
      : '',
  );
  function handleSubscribe(e: SubmitEvent) {
    e.preventDefault();
    submittedEmail = newsletter;
  }
</script>
```

### Example 1: A labelled field

```svelte
<div class="col">
  <label for="default">Label</label>
  <Input id="default" placeholder="Type something" />
</div>
```

### Example 2: A bound field

```svelte
<div class="col">
  <label for="bound">Email</label>
  <Input
    id="bound"
    type="email"
    placeholder="you@domain.com"
    bind:value={email}
  />
  <small>Current value: <code>{email || '(empty)'}</code></small>
</div>
```

### Example 3: Browser validation

Submit it empty, or with a bad email.

```svelte
<form
  class="col"
  onsubmit={(e) => {
    e.preventDefault();
    nativeSubmitted = true;
  }}
>
  <label for="native">Email</label>
  <Input id="native" type="email" name="email" required />
  <Button type="submit" class="primary small">Submit</Button>
  {#if nativeSubmitted}
    <small>Submitted!</small>
  {/if}
</form>
```

### Example 4: An invalid field

```svelte
<div class="col">
  <label for="invalid">Username</label>
  <Input
    id="invalid"
    invalid
    value="bad value"
    aria-describedby="invalid-msg"
  />
  <small id="invalid-msg" class="error">Username is already taken.</small>
</div>
```

### Example 5: Disabled and readonly

```svelte
<div class="col">
  <label for="disabled">Disabled</label>
  <Input id="disabled" value="cannot edit" disabled />
  <label for="readonly">Readonly</label>
  <Input id="readonly" value="read only value" readonly />
</div>
```

### Example 6: A prefix icon

```svelte
<div class="col">
  <label for="search">Search</label>
  <Input id="search" placeholder="Search projects" bind:value={search}>
    {#snippet prefix()}
      <div class="icon search"></div>
    {/snippet}
  </Input>
</div>
```

### Example 7: A show and hide toggle in the suffix

```svelte
<div class="col">
  <label for="password">Password</label>
  <Input
    id="password"
    type={showPassword ? 'text' : 'password'}
    placeholder="••••••••"
    bind:value={password}
  >
    {#snippet suffix()}
      <Button
        type="button"
        class="text small"
        onclick={() => (showPassword = !showPassword)}
      >
        {showPassword ? 'Hide' : 'Show'}
      </Button>
    {/snippet}
  </Input>
</div>
```

### Example 8: An amount between a prefix and a suffix

```svelte
<div class="col">
  <label for="amount">Amount</label>
  <Input id="amount" type="number" placeholder="0.00" bind:value={amount}>
    {#snippet prefix()}<span>$</span>{/snippet}
    {#snippet suffix()}<span>USD</span>{/snippet}
  </Input>
</div>
```

### Example 9: A server error on a newsletter form

Try foo@blocked.dev to see the error.

```svelte
<form class="col" onsubmit={handleSubscribe}>
  <label for="newsletter">Get updates</label>
  <Input
    id="newsletter"
    type="email"
    name="email"
    placeholder="you@domain.com"
    required
    bind:value={newsletter}
    invalid={!!serverError}
    aria-describedby="newsletter-msg"
  >
    {#snippet suffix()}
      <Button type="submit" class="primary small">Subscribe</Button>
    {/snippet}
  </Input>
  <small id="newsletter-msg" class:error={!!serverError}>
    {#if serverError}
      {serverError}
    {:else if submittedEmail}
      Subscribed {submittedEmail}.
    {:else}
      We'll never share your email.
    {/if}
  </small>
</form>
```

---

From the Qualia design guide: https://q-core-3b2732.gitlab.io/components/input. Every page as markdown: https://q-core-3b2732.gitlab.io/llms.txt
