Number Input
View as MarkdownA numeric input with increment and decrement buttons.
Features
Permalink to heading "Features"- Follows the spinbutton pattern
- Changes the value with the mouse wheel through
allowMouseWheel - Avoids floating point rounding errors when incrementing, decrementing and snapping to a step
- Keeps stepping while a button is pressed and held
- Rounds the value to a set number of fraction digits
- Scrubbing: change the value by dragging
- Takes its locale from the site language
<primitives:numberInput.root>
<primitives:numberInput.label />
<primitives:numberInput.control>
<primitives:numberInput.decrementTrigger />
<primitives:numberInput.input />
<primitives:numberInput.incrementTrigger />
</primitives:numberInput.control>
<primitives:numberInput.scrubber />
<primitives:numberInput.valueText />
</primitives:numberInput.root>
Installation
Permalink to heading "Installation"typo3 ui:add numberInput
Please copy the files manually from GitHub into your project: NumberInput
Read more about installing Components and Primitives.
Examples
Permalink to heading "Examples"With min/max constraints
Permalink to heading "With min/max constraints"Pass min and max to set the range and step to set the increment. The buttons stop at the limits, and a typed value snaps into the range when the input loses focus. The demo is a percentage from 0 to 100 in steps of 5.
With format options
Permalink to heading "With format options"Pass formatOptions, an object of Intl.NumberFormat options, to round the value to a number of decimals or show it as a currency. The client formats the value, so the server renders it unformatted until the number input hydrates.
With scrubber
Permalink to heading "With scrubber"Wrap an element in numberInput.scrubber to change the value by dragging it horizontally. In the demo that element is the label.
Mouse wheel support
Permalink to heading "Mouse wheel support"Set allowMouseWheel="{true}" to change the value with the mouse wheel while the input has focus. Focus the input in the demo and scroll.
Localization
Permalink to heading "Localization"The labels of the increment and decrement buttons ship in the language file and follow the site language. Pass translations to override them for one number input, for example for a quantity stepper in a shop.
<f:variable
name="numberInputTranslations"
value="{
incrementLabel: '{f:translate(key: \'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:forms.quantity.increase\')}',
decrementLabel: '{f:translate(key: \'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:forms.quantity.decrease\')}'
}"
/>
<ui:numberInput.root translations="{numberInputTranslations}"> ... </ui:numberInput.root>
valueText sets the aria-valuetext of the input, the text a screen reader reads for the value. Write it with a %value% placeholder, e.g. valueText: '%value% pieces'. A client script passes a function of the value instead.
API Reference
Permalink to heading "API Reference"The following tables cover the available props of the Fluid Primitives. For a full list of available client side props and methods, see the Zag.js Machine API.
numberInput.root
Permalink to heading "numberInput.root"Provides shared number input state and wraps all related parts. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
disabled | booleanWhether the number input is disabled. Inherited from a surrounding field when available. | No | - |
invalid | booleanWhether the number input value is invalid. Inherited from a surrounding field when available. | No | - |
required | booleanWhether the number input is required. Inherited from a surrounding field when available. | No | - |
readOnly | booleanWhether the number input is readonly. Inherited from a surrounding field when available. | No | - |
name | stringThe name attribute of the number input. Useful for form submission. Inherited from a surrounding field when available. | No | - |
form | stringThe associate form of the input element. | No | - |
defaultValue | stringThe initial value of the input when rendered. Use when you don't need to control the value of the input. Inherited from a surrounding field when available. | No | '' |
min | floatThe minimum value of the number input. | No | - |
max | floatThe maximum value of the number input. | No | - |
step | floatThe amount to increment or decrement the value by. | No | - |
smallStep | floatThe amount to increment or decrement the value by when the `Alt` key is held. Defaults to `step / 10`. | No | - |
largeStep | floatThe amount to increment or decrement the value by when the `Shift` key is held. Defaults to `10 * step`. | No | - |
allowMouseWheel | booleanWhether to allow mouse wheel to change the value. | No | - |
allowOverflow | booleanWhether to allow the value overflow the min/max range. | No | false |
clampValueOnBlur | booleanWhether to clamp the value when the input loses focus (blur). | No | true |
focusInputOnChange | booleanWhether to focus input when the value changes. | No | true |
spinOnPress | booleanWhether to spin the value when the increment/decrement button is pressed. | No | true |
formatOptions | arrayThe options to pass to the `Intl.NumberFormat` constructor. | No | - |
inputMode | Enum\NumberInputModeHints at the type of data that might be entered by the user and the keyboard shown on mobile devices. | No | Decimal |
pattern | stringThe pattern used to check the `<input>` element's value against. | No | '-?[0-9]*(.[0-9]+)?' |
translations | arraySpecifies the localized strings that identify the accessibility elements and their states. `valueText` (the `aria-valuetext` of the input) takes a `%value%` placeholder, and is a function of the value in a client script. Use `f:translate` for per-template localization overrides when needed. | No | - |
locale | stringThe current locale. Inherited from the site's language. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
rootId | stringThe root ID of the component, used for hydration and identification. | No | - |
ids | arrayThe IDs of of the component parts for composition. | No | [] |
autoMount | booleanWhether the client initializes the component on its own. Set it to false to mount it yourself with `mount()` | No | true |
provider | stringThe `id` of the `.provider` this root takes its `rootId` and props from. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-number-input-root | <uid> |
data-disabled | Present when disabled |
data-focus | Present when focused |
data-invalid | Present when invalid |
data-scrubbing |
numberInput.label
Permalink to heading "numberInput.label"Labels the number input. Renders a <label> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-number-input-label | <uid> |
data-disabled | Present when disabled |
data-focus | Present when focused |
data-invalid | Present when invalid |
data-required | Present when required |
data-scrubbing |
numberInput.control
Permalink to heading "numberInput.control"Groups the input and stepper triggers. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-number-input-control | <uid> |
data-focus | Present when focused |
data-disabled | Present when disabled |
data-invalid | Present when invalid |
data-scrubbing |
numberInput.input
Permalink to heading "numberInput.input"Provides the editable spinbutton input. Renders an <input> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-number-input-input | <uid> |
data-invalid | Present when invalid |
data-disabled | Present when disabled |
data-scrubbing |
numberInput.incrementTrigger
Permalink to heading "numberInput.incrementTrigger"Increases the current value. Renders a <button> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-number-input-increment-trigger | <uid> |
data-disabled | Present when disabled |
data-scrubbing |
numberInput.decrementTrigger
Permalink to heading "numberInput.decrementTrigger"Decreases the current value. Renders a <button> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-number-input-decrement-trigger | <uid> |
data-disabled | Present when disabled |
data-scrubbing |
numberInput.scrubber
Permalink to heading "numberInput.scrubber"Allows changing the value by dragging. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-number-input-scrubber | <uid> |
data-disabled | Present when disabled |
data-scrubbing |
numberInput.valueText
Permalink to heading "numberInput.valueText"Displays the formatted value as text. Renders a <span> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-number-input-value-text | <uid> |
data-disabled | Present when disabled |
data-invalid | Present when invalid |
data-focus | Present when focused |
data-scrubbing |
Machine JavaScript API
Permalink to heading "Machine JavaScript API"| Name | Type | Description |
|---|---|---|
focused | boolean | Whether the input is focused. |
scrubbing | boolean | Whether the input is being scrubbed. |
invalid | boolean | Whether the input is invalid. |
empty | boolean | Whether the input value is empty. |
value | string | The formatted value of the input. |
valueAsNumber | number | The value of the input as a number. |
setValue | (value: number) => void | Function to set the value of the input. |
clearValue | VoidFunction | Function to clear the value of the input. |
increment | VoidFunction | Function to increment the value of the input by the step. |
decrement | VoidFunction | Function to decrement the value of the input by the step. |
setToMax | VoidFunction | Function to set the value of the input to the max. |
setToMin | VoidFunction | Function to set the value of the input to the min. |
focus | VoidFunction | Function to focus the input. |
getRootState | () => RootState | Returns the state of the root. |
getInputState | () => InputState | Returns the state of the input. |
getDecrementTriggerState | () => DecrementTriggerState | Returns the state of the decrement trigger. |
getIncrementTriggerState | () => IncrementTriggerState | Returns the state of the increment trigger. |
Accessibility
Permalink to heading "Accessibility"| Key | Description |
|---|---|
ArrowUp | Increments the value of the number input by a predefined step. |
ArrowDown | Decrements the value of the number input by a predefined step. |
Shift + ArrowUp | Increments the value of the number input by the `largeStep` amount. |
Shift + ArrowDown | Decrements the value of the number input by the `largeStep` amount. |
Alt + ArrowUp | Increments the value of the number input by the `smallStep` amount. |
Alt + ArrowDown | Decrements the value of the number input by the `smallStep` amount. |
Home | Sets the value of the number input to its minimum allowed value. |
End | Sets the value of the number input to its maximum allowed value. |
Enter | Submits the value entered in the number input. |