BackchannelUI
  1. Docs
  2. components
  3. NumberField

NumberField

A NumberField allows users to enter a number, and increment or decrement the value using stepper buttons or the keyboard.

Number fields can be built with <input type="number">, but the behavior is very inconsistent across browsers and platforms, it supports limited localized formatting options, and it is challenging to style the stepper buttons. NumberField helps achieve accessible number fields that support internationalized formatting options and can be styled as needed.

Installation

npx shadcn@latest add https://backchanel.com/default/numberfield

Composed Components

A NumberField uses the following components, which may also be used standalone or reused in other components.

Label

A label provides context for an element.

Input

An input allows a user to enter a plain text value with a keyboard.

Button

A button allows a user to perform an action, with mouse, touch, and keyboard interactions.

Reusable Wrapper - Example

If you will use a NumberField in multiple places in your app, you can wrap all of the pieces into a reusable component. This way, the DOM structure, styling code, and other logic are defined in a single place and reused everywhere to ensure consistency.

The installed file includes a reusable wrapper JollyNumberField. This wrapper serves as an excellent starting point for use throughout your codebase.

Please enter a number

The JollyNumberField component extends the props of React Aria NumberField and adds:

PropTypeDefaultDescription
labelstring | undefinedundefinedLabel for the number field
descriptionstring | undefinedundefinedDescription text for the number field
errorMessagestring | ((validation: AriaValidationResult) => string) | undefinedundefinedError message to display or function to generate it

You can copy this wrapper and create variations as needed for different use cases in your application. This approach promotes consistency while still allowing for flexibility in implementation.

Examples

Basic

Decimals

Number formatting

Currency

Percentages

Units

Validation

Step Values

Validation errors

Description

Enter a width in centimeters.

Disabled

Read-only