# Inputs

Text, number, check and switch inputs. Exact props: `widgets-get-component-props`.

---

## What `onChange` receives

| Component | `onChange` receives |
|-----------|---------------------|
| `DInput` | `value: string` |
| `DInputSearch` | `value: string`, after `debounceMs` (300 ms by default); `onImmediateChange` receives every keystroke |
| `DInputPassword` | `value: string` |
| `DInputCurrency` | `value?: number` |
| `DInputCounter` | `value?: number` |
| `DInputSwitch` | `isChecked: boolean` |
| `DInputCheck` | the change **event** (read `event.target.checked`) |
| `DInputRange` | the change **event** (read `event.target.value`) |

Passing a handler written for an event to a value-based input (`(e) => setValue(e.target.value)`) breaks at runtime.

---

## DInput

From the template's reference feature:

```tsx
<DInput
  iconStart="Search"
  placeholder={t('common.searchPlaceholder')}
  value={searchQuery}
  onChange={setSearchQuery}
/>
```

```tsx
<DInput
  label={t('entities.create.name')}
  value={name}
  onChange={setName}
  invalid={isError}
  hint={isError ? t('entities.create.error') : undefined}
/>
```

| Prop | Use |
|------|-----|
| `label` | Visible label; it also names the field for screen readers |
| `value`, `onChange` | Controlled value (string) |
| `hint` | Help or error text under the field |
| `invalid`, `valid` | Validation state; combine `invalid` with `hint` for the error message |
| `iconStart`, `iconEnd` | Lucide names; `onIconStartClick`/`onIconEndClick` make them clickable |
| `inputStart`, `inputEnd` | Content inside the field, before or after the text |
| `floatingLabel`, `size`, `loading`, `readonly`, `disabled`, `placeholder` | Presentation and state |

Other `<input>` attributes (`type`, `name`, `autoComplete`, `onBlur`, …) are forwarded.

## DInputSearch

A search field with a built-in debounce: `onChange` receives the value once typing pauses for `debounceMs` (300 ms by default; `0` disables it), and `onImmediateChange` receives every keystroke.

Leave it uncontrolled and use the debounced value for the query:

```tsx
<DInputSearch label={t('common.search')} defaultValue="" onChange={setQuery} />
```

If you pass `value`, the field shows only that value: update it from `onImmediateChange`, not from `onChange`, or the text stays frozen until the debounce ends.

## DInputPassword

`DInput` with a visibility toggle; same props. For a strength indicator use `DPasswordStrengthMeter` (`modyo://docs/widgets/components-inputs-advanced`).

## DInputCurrency

```tsx
<DInputCurrency label={t('transfer.amount')} value={amount} onChange={setAmount} minValue={0} />
```

- `value` and `onChange` work with numbers. The field shows the value formatted while it is not being edited, with the currency set on `DContextProvider`; `currencyCode` replaces the symbol shown at the start.
- `minValue` and `maxValue` limit the amount. With `clamp` (on by default) a value out of range is brought into range and `onChange` receives the clamped number; with `clamp={false}` the value is kept and the field is marked invalid.
- To show an amount without editing it, use `DCurrencyText`.

## DInputCounter

A number with increment and decrement buttons. `minValue` and `maxValue` are required.

```tsx
<DInputCounter label={t('order.quantity')} value={quantity} onChange={setQuantity} minValue={1} maxValue={10} />
```

## DInputCheck

Checkbox or radio (`type="checkbox"` or `type="radio"`). `onChange` receives the event:

```tsx
<DInputCheck
  type="checkbox"
  label={t('terms.accept')}
  checked={accepted}
  onChange={(event) => setAccepted(event.target.checked)}
/>
```

`indeterminate` shows the mixed state of a "select all" checkbox.

## DInputSwitch

```tsx
<DInputSwitch label={t('settings.notifications')} checked={enabled} onChange={setEnabled} />
```

## DInputRange

A slider. `onChange` receives the event; `min` and `max` default to 0 and 100:

```tsx
<DInputRange
  label={t('filters.maxAmount')}
  min={0}
  max={5000}
  value={max}
  onChange={(event) => setMax(Number(event.target.value))}
/>
```

---

## Common mistakes

| Wrong | Right |
|-------|-------|
| `onChange={(e) => setValue(e.target.value)}` on `DInput` | `onChange={setValue}` |
| `onChange={setChecked}` on `DInputCheck` | `onChange={(event) => setChecked(event.target.checked)}` |
| An error message in a separate `<small>` | `invalid` + `hint` |
| An input without `label` or `aria-label` | Every field has an accessible name |
