# Number field

Source: https://ecosphere.dev/docs/forms/number-field (Ecosphere 1.5). Install `vue-ecosphere` for Vue 3 or `react-ecosphere` for React 18.2+/19: same components, props, markup and stylesheet.

An editable figure in tabular figures, with optional steppers and a unit inside the box.

`import { EcoNumberField } from "vue-ecosphere"`

## Number

A text input with inputmode=numeric (decimal for a fractional step), never type=number.

Vue (vue-ecosphere):

```vue
<EcoNumberField v-model="qty" label="Quantity" hint="Whole units only." first />
```

React (react-ecosphere):

```tsx
const [qty, setQty] = useState<number | null>(12);

<EcoNumberField value={qty} onChange={setQty} label="Quantity" hint="Whole units only." first />
```

## Stepper

Vue (vue-ecosphere):

```vue
<EcoNumberField v-model="seats" label="Seats" stepper :min="1" first />
```

React (react-ecosphere):

```tsx
const [seats, setSeats] = useState<number | null>(8);

<EcoNumberField value={seats} onChange={setSeats} label="Seats" stepper min={1} first />
```

## With a unit

Step down to 5 and the down button disables. Type 900 and leave the field: it becomes 300.

Vue (vue-ecosphere):

```vue
<EcoNumberField v-model="timeout" label="Timeout" stepper unit="sec"
  :min="5" :max="300" :step="5" hint="Between 5 and 300 seconds." first />
```

React (react-ecosphere):

```tsx
const [timeout, setTimeout] = useState<number | null>(30);
<EcoNumberField value={timeout} onChange={setTimeout} label="Timeout" stepper unit="sec"
  min={5} max={300} step={5} hint="Between 5 and 300 seconds." first />
```

## Decimal

A fractional step switches the touch keypad to inputmode=decimal, so the point can be typed.

Vue (vue-ecosphere):

```vue
<EcoNumberField v-model="mark" label="Mark" unit="/ 50" :step="0.5"
  :min="0" :max="50" hint="Half marks allowed." first />
```

React (react-ecosphere):

```tsx
<EcoNumberField value={mark} onChange={setMark} label="Mark" unit="/ 50" step={0.5}
  min={0} max={50} hint="Half marks allowed." first />
```

## Out of range

Step below 11 to clear the error.

Vue (vue-ecosphere):

```vue
<EcoNumberField v-model="retries" label="Retries" stepper :min="0"
  :error="(retries ?? 0) > 10 ? 'Ten retries is the most this plan allows.' : undefined" first />
```

React (react-ecosphere):

```tsx
const [retries, setRetries] = useState<number | null>(14);

<EcoNumberField value={retries} onChange={setRetries} label="Retries" stepper min={0}
  error={(retries ?? 0) > 10 ? "Ten retries is the most this plan allows." : undefined} first />
```

## API

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `v-model` | `number \| null` | null | The figure; null while the box is empty or holds text that is not a number (the input is then aria-invalid). |
| `stepper` | `boolean` | false | Up/down buttons, only where a person nudges the value. ArrowUp/ArrowDown step too. |
| `min / max` | `number` | — | Bounds. A stepper disables at the end of its range, and a typed figure outside them is pulled back to the nearest bound when the field is left. |
| `step` | `number` | 1 | Stepper increment. A fractional step brings up the decimal keypad (inputmode=decimal). |
| `inputmode` | `string` | — | Passed to the input and wins over the default (numeric, or decimal for a fractional step). |
| `unit` | `string` | — | Unit inside the box (input__unit), never in the label. |
| `label / required / hint / error / invalid / first` | `—` | — | Field props. Say the range in the hint. |
| `increaseLabel / decreaseLabel` | `string` | "Increase" / "Decrease" | Stepper button names. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value / onChange` | `number \| null` | null | The figure; null while the box is empty or holds text that is not a number (the input is then aria-invalid). |
| `stepper` | `boolean` | false | Up/down buttons, only where a person nudges the value. ArrowUp/ArrowDown step too. |
| `min / max` | `number` | — | Bounds. A stepper disables at the end of its range, and a typed figure outside them is pulled back to the nearest bound when the field is left. |
| `step` | `number` | 1 | Stepper increment. A fractional step brings up the decimal keypad (inputmode=decimal). |
| `inputmode` | `string` | — | Passed to the input and wins over the default (numeric, or decimal for a fractional step). |
| `unit` | `string` | — | Unit inside the box (input__unit), never in the label. |
| `label / required / hint / error / invalid / first` | `—` | — | Field props. Say the range in the hint. |
| `increaseLabel / decreaseLabel` | `string` | "Increase" / "Decrease" | Stepper button names. |
