<cs-number-input>
StableFormsSince 0.1

Number inputs let users enter and edit numeric values, with optional stepper buttons for incrementing and decrementing. Use them for quantities, measurements, and other numeric form fields.

This component works with standard <form> elements. See form controls for form submission and client-side validation.

Examples

Label

Use the label attribute to give the input an accessible label. For labels that contain HTML, use the label slot instead.

Hint

Add descriptive hint to an input with the hint attribute. For hints that contain HTML, use the hint slot instead.

Placeholder

Use the placeholder attribute to add a placeholder.

Appearance

Use the appearance attribute to change the input’s visual appearance.

Disabled

Use the disabled attribute to disable an input.

Readonly

Use the readonly attribute to keep a value visible but uneditable. Unlike disabled, a readonly input stays focusable and its value is still submitted with the form.

Size

Use the size attribute to change an input’s size.

Pill

Use the pill attribute to give inputs rounded edges.

Min, Max & Step

Use the min and max attributes to set a minimum and maximum value. Use the step attribute to change the granularity the value must adhere to when using the stepper buttons or arrow keys.

Hiding the Steppers

Add the without-steppers attribute to remove the increment and decrement buttons.

The keyboard still works.
With the steppers hidden, the arrow keys can still increment and decrement the value.

Custom Stepper Icons

Use the increment-icon and decrement-icon slots to replace the default stepper button icons.

Start & End Decorations

Use the start and end slots to add presentational elements like <cs-icon> within the input.

Customizing Label Position

Use CSS parts to customize the way form controls are drawn. This example uses CSS grid to position the label to the left of the control, but the possible orientations are nearly endless. The same technique works for inputs, textareas, radio groups, and similar form controls.

Validation

Use the required attribute to make the field required. Combine with min and max for range validation.



Submit Reset

API

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick this component, use one of the following snippets.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/number-input/number-input.js';
import './cornerstone/components/number-input/number-input.js';
import CsNumberInput from '@cruglobal/cornerstone-components/react/number-input/index.js';

Slots

Learn more about using slots.

NameDescription
decrement-iconAn icon to use in lieu of the default decrement icon.
endAn element, such as <cs-icon>, placed at the end of the input control (before steppers).
hintText that describes how to use the input. Alternatively, you can use the hint attribute.
increment-iconAn icon to use in lieu of the default increment icon.
labelThe input's label. Alternatively, you can use the label attribute.
startAn element, such as <cs-icon>, placed at the start of the input control.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
appearanceappearanceThe input's visual appearance.'filled' | 'outlined' | 'filled-outlined''outlined'Yes
autocompleteautocompleteSpecifies what permission the browser has to provide assistance in filling out form field values. Refer to this page on MDN for available values.string
autofocusautofocusIndicates that the input should receive focus on page load.boolean
defaultValuevalueThe default value of the form control. Primarily used for resetting the form control.string | nullYes
disableddisabledDisables the form control.booleanfalse
enterkeyhintenterkeyhintUsed to customize the label or icon of the Enter key on virtual keyboards.'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send'
formBy default, form controls are associated with the nearest containing <form> element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work.HTMLFormElement | null
hinthintThe input's hint. If you need to display HTML, use the hint slot instead.string''
inputmodeinputmodeTells the browser what type of data will be entered by the user, allowing it to display the appropriate virtual keyboard on supportive devices.'numeric' | 'decimal''numeric'
labellabelThe input's label. If you need to display HTML, use the label slot instead.string''
maxmaxThe input's maximum value.number
minminThe input's minimum value.number
namenameThe name of the input, submitted as a name/value pair with form data.string | nullnullYes
pillpillDraws a pill-style input with rounded edges.booleanfalseYes
placeholderplaceholderPlaceholder text to show as a hint when the input is empty.string''
readonlyreadonlyMakes the input readonly.booleanfalseYes
requiredrequiredMakes the input a required field.booleanfalseYes
sizesizeThe input's size.'xs' | 's' | 'm' | 'l' | 'xl''m'Yes
ssrHintssr-hintOnly required for SSR. Set to true if you're slotting in a hint element so the server-rendered markup includes the hint before the component hydrates on the client.booleanfalse
ssrLabelssr-labelOnly required for SSR. Set to true if you're slotting in a label element so the server-rendered markup includes the label before the component hydrates on the client.booleanfalse
stepstepSpecifies the granularity that the value must adhere to, or the special value any which means no stepping is implied, allowing any numeric value.number | 'any'1
validationTargetOverride this to change where constraint validation popups are anchored.undefined | HTMLElement
validatorsValidators are static because they have observedAttributes, essentially attributes to "watch" for changes. Whenever these attributes change, we want to be notified and update the validator.Validator[][]
valueThe current value of the input, submitted as a name/value pair with form data.
withoutStepperswithout-steppersHides the increment/decrement stepper buttons.booleanfalse

Methods

Learn more about methods.

NameDescriptionArguments
blur()Removes focus from the input.
focus()Sets focus on the input.options: FocusOptions
formStateRestoreCallback()Called when the browser is trying to restore element’s state to state in which case reason is "restore", or when the browser is trying to fulfill autofill on behalf of user in which case reason is "autocomplete". In the case of "restore", state is a string, File, or FormData object previously set as the second argument to setFormValue.state: string | File | FormData | null, reason: 'autocomplete' | 'restore'
resetValidity()Reset validity is a way of removing manual custom errors and native validation.
select()Selects all the text in the input.
setCustomValidity()Do not use this when creating a "Validator". This is intended for end users of components. We track manually defined custom errors so we don't clear them on accident in our validators.message: string
stepDown()Decrements the value by the step amount.
stepUp()Increments the value by the step amount.

Events

Learn more about events.

NameDescription
beforeinputEmitted before the value changes. Can be cancelled with event.preventDefault() to prevent the value from changing.
blurEmitted when the control loses focus.
changeEmitted when an alteration to the control's value is committed by the user.
cs-invalidEmitted when the form control has been checked for validity and its constraints aren't satisfied.
focusEmitted when the control gains focus.
inputEmitted when the control receives input.

Custom States

Learn more about custom states.

NameDescriptionCSS selector
blankThe input is empty.:state(blank)
focusedThe input has focus.:state(focused)

CSS Parts

Learn more about CSS parts.

NameDescriptionCSS selector
endThe container that wraps the end slot.::part(end)
form-control-labelThe label.::part(form-control-label)
hintThe hint element.::part(hint)
inputThe internal <input> control.::part(input)
number-inputThe component's outer wrapper.::part(number-input)
startThe container that wraps the start slot.::part(start)
stepperBoth stepper buttons (for shared styling).::part(stepper)
stepper-decrementThe decrement (-) button on the start side.::part(stepper-decrement)
stepper-incrementThe increment (+) button on the end side.::part(stepper-increment)

Dependencies

This component automatically imports the following elements. Sub-dependencies, if any exist, are included in this list.