Number Input
A text field for numbers, with buttons to step the value up and down.
Usage
import { NumberInput } from '@leverui/components';
<NumberInput.Root>
<NumberInput.Label />
<NumberInput.ValueText />
<NumberInput.Control>
<NumberInput.IncrementTrigger />
<NumberInput.DecrementTrigger />
</NumberInput.Control>
<NumberInput.Scrubber />
<NumberInput.Input />
</NumberInput.Root>
For a single component, see the snippet below.
Shortcuts
NumberInputControl
With no children, NumberInput.Control renders
NumberInput.IncrementTrigger and NumberInput.DecrementTrigger.
<NumberInput.Control />
Same as:
<NumberInput.Control>
<NumberInput.IncrementTrigger />
<NumberInput.DecrementTrigger />
</NumberInput.Control>
Examples
Sizes
Pass the size prop to the NumberInput.Root component to change the size of the number input.
Formatting
Pass the formatOptions prop to the NumberInput.Root component to format the number input value. The value of this maps to Intl.NumberFormatOptions and is applied based on the current locale.
Min and Max
Pass the min and max props to the NumberInput.Root component to set the minimum and maximum values of the number input.
If value entered is less than min or greater than max, the value will be clamped to the nearest boundary on blur, or enter key press.
Step
Pass the step prop to the NumberInput.Root component to change the increment or decrement interval of the number input.
Controlled
Pass the value and onValueChange props to the NumberInput.Root component to control the value of the number input.
Mobile Stepper
Here's an example of how to compose the number input as a mobile stepper.
Mouse Wheel
Pass the allowMouseWheel prop to the NumberInput.Root component to enable or disable the mouse wheel to change
Disabled
Pass the disabled prop to the NumberInput.Root component to disable the number input.
Invalid
Use the Field component and the invalid prop to indicate that the number input is invalid.
Helper Text
Compose the Field and Field.HelperText components to add helper text to the number input.
Element
Here's an example of how to compose the number input with the input group component to add an element on either the left or right.
Scrubber
Use the NumberInput.Scrubber component to make the number input supports scrubber interactions.
Hook Form
Here is an example of how to use the NumberInput component with Solid form state.
Closed Component
One component that wraps the parts.
import { omit } from 'solid-js';
import { NumberInput as NumberInputParts, type NumberInputRootProps } from '@leverui/components';
export function NumberInputRoot(props: NumberInputRootProps) {
const rest = omit(props, 'children');
return (
<NumberInputParts.Root variant="outline" {...rest}>
{props.children}
<NumberInputParts.Control>
<NumberInputParts.IncrementTrigger />
<NumberInputParts.DecrementTrigger />
</NumberInputParts.Control>
</NumberInputParts.Root>
);
}
export const NumberInputField = NumberInputParts.Input;
export const NumberInputScrubber = NumberInputParts.Scrubber;
export const NumberInputLabel = NumberInputParts.Label;
<NumberInputRoot>
<NumberInputField />
</NumberInputRoot>
Guides
Why use string values?
Keep a controlled value as a string, not a number. A string keeps the
formatting of the user's language and region, such as currency separators
(1.523,30 and 1,523.30).
const [value, setValue] = createSignal('0');
<NumberInput.Root value={value()} onValueChange={(details) => setValue(details.value)}>
{/* ... */}
</NumberInput.Root>;
To submit a number, read valueAsNumber from NumberInput.Context.
<NumberInput.Root value={value()} onValueChange={(details) => setValue(details.value)}>
<NumberInput.Input />
<NumberInput.Context>
{(context) => <input type="hidden" name="amount" value={context().valueAsNumber} />}
</NumberInput.Context>
</NumberInput.Root>
Props
Root
| Prop | Default | Type | Description |
|---|---|---|---|
allowOverflow | true | boolean | Let the value go below min or above max. |
clampValueOnBlur | true | boolean | Clamp the value to min and max on blur. |
focusInputOnChange | true | boolean | Focus the input when the value changes. |
inputMode | decimal | InputMode | Data type hint. Picks the mobile keyboard. |
largeStep | 10 * step | number | Step size while Shift is held. |
locale | en-US | string | BCP 47 tag used to format the number. |
max | Number.MAX_SAFE_INTEGER | number | Largest value allowed. |
min | Number.MIN_SAFE_INTEGER | number | Smallest value allowed. |
pattern | -?[0-9]*(.[0-9]+)? | string | Pattern the <input> value is checked against. |
smallStep | step / 10 | number | Step size while Alt is held. |
spinOnPress | true | boolean | Keep stepping while a stepper button is held. |
step | 1 | number | Step size. |
colorPalette | gray | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' | Color palette. |
size | md | 'xs' | 'sm' | 'md' | 'lg' | Number input size. |
variant | outline | 'outline' | 'subtle' | 'flushed' | Number input style. |
as | div | Native tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the root's props. |
unstyled | — | boolean | Turn off the Lever UI styles. |
allowMouseWheel | — | boolean | Let the mouse wheel change the value. |
defaultValue | — | string | Starting value, when uncontrolled. |
disabled | — | boolean | Disable the number input. |
form | — | string | Form the input belongs to. |
formatOptions | — | NumberFormatOptions | Options for Intl.NumberFormat. |
id | — | string | Unique id. |
ids | — | Partial<{ root: string; label: string; input: string; incrementTrigger: string; decrementTrigger: string; scrubber: string }> | Your own ids for the parts. |
invalid | — | boolean | Mark the value as invalid. |
name | — | string | Input name, sent with the value on submit. |
onFocusChange | — | (details: FocusChangeDetails) => void | Called when focus changes. |
onValueChange | — | (details: ValueChangeDetails) => void | Called when the value changes. |
onValueCommit | — | (details: ValueChangeDetails) => void | Called when the value commits, on blur or Enter. |
onValueInvalid | — | (details: ValueInvalidDetails) => void | Called when the value goes below min or above max. |
readOnly | — | boolean | Make the number input read-only. |
required | — | boolean | Mark the number input as required. |
translations | — | IntlTranslations | Translated text for screen readers. |
value | — | string | Current value, when you control it. |
NumberInput.PropsProvider sets default props for every root inside it. The
defaults are reactive. A prop on a root wins.
For your own state machine, call useNumberInput() inside a Solid owner, such
as a component, and pass the result to NumberInput.RootProvider.
ValueText shows the formatted value. Scrubber changes the value by
dragging.
Customize with CSS
Load the package CSS first. Then override the tokens or style the part classes. The class names are stable.
.leverui-number-input__input {
border-color: rebeccapurple;
}
The root, input, control, triggers, scrubber, label, and value text each have a
.leverui-number-input__* class. The input also has .leverui-input, so it
gets the Input size, variant, focus, disabled, and invalid styles.
Root size and variant are responsive.
Stepper buttons: set --leverui-number-input-stepper-width or
--leverui-number-input-stepper-radius on the root.
Explorer
Click a part name in the sidebar to highlight it in the preview.
Component Anatomy
Hover to highlight, click to select parts