Number Input

A text field for numbers, with buttons to step the value up and down.

Usage

Code
import { NumberInput } from '@leverui/components';
Code
<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.

Code
<NumberInput.Control />

Same as:

Code
<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.

3

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.

The entry is invalid

Helper Text

Compose the Field and Field.HelperText components to add helper text to the number input.

Enter a number between 1 and 10

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.

Code
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;
Code
<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).

Code
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.

Code
<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

PropDefaultTypeDescription
allowOverflowtruebooleanLet the value go below min or above max.
clampValueOnBlurtruebooleanClamp the value to min and max on blur.
focusInputOnChangetruebooleanFocus the input when the value changes.
inputModedecimalInputModeData type hint. Picks the mobile keyboard.
largeStep10 * stepnumberStep size while Shift is held.
localeen-USstringBCP 47 tag used to format the number.
maxNumber.MAX_SAFE_INTEGERnumberLargest value allowed.
minNumber.MIN_SAFE_INTEGERnumberSmallest value allowed.
pattern-?[0-9]*(.[0-9]+)?stringPattern the <input> value is checked against.
smallStepstep / 10numberStep size while Alt is held.
spinOnPresstruebooleanKeep stepping while a stepper button is held.
step1numberStep size.
colorPalettegray'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'Color palette.
sizemd'xs' | 'sm' | 'md' | 'lg'Number input size.
variantoutline'outline' | 'subtle' | 'flushed'Number input style.
asdivNative tagHTML tag to render.
asChild—Solid render functionRender your own element with the root's props.
unstyled—booleanTurn off the Lever UI styles.
allowMouseWheel—booleanLet the mouse wheel change the value.
defaultValue—stringStarting value, when uncontrolled.
disabled—booleanDisable the number input.
form—stringForm the input belongs to.
formatOptions—NumberFormatOptionsOptions for Intl.NumberFormat.
id—stringUnique id.
ids—Partial<{ root: string; label: string; input: string; incrementTrigger: string; decrementTrigger: string; scrubber: string }>Your own ids for the parts.
invalid—booleanMark the value as invalid.
name—stringInput name, sent with the value on submit.
onFocusChange—(details: FocusChangeDetails) => voidCalled when focus changes.
onValueChange—(details: ValueChangeDetails) => voidCalled when the value changes.
onValueCommit—(details: ValueChangeDetails) => voidCalled when the value commits, on blur or Enter.
onValueInvalid—(details: ValueInvalidDetails) => voidCalled when the value goes below min or above max.
readOnly—booleanMake the number input read-only.
required—booleanMark the number input as required.
translations—IntlTranslationsTranslated text for screen readers.
value—stringCurrent 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.

Code
.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