Number Field

A number field component allows users to enter and adjust numeric values.

UsageCodeAccessibility
  <sl-number-field aria-label="Number field" placeholder="Type a number" value="20"></sl-number-field>

API

Number Field has a range of properties to define the experience in different use cases.

Properties

NameAttributeTypeDefaultDescription
autocompleteautocompletestring | undefinedSpecifies which type of data the browser can use to pre-fill the input.
customValiditycustom-validitystring | undefinedThe error message to display when the control is invalid.
dirty-booleanfalseA control is dirty if the user has changed the value in the UI.
disableddisabledboolean | undefinedWhether the number field is disabled; when set no interaction is possible.
form-HTMLFormElement | nullThe form associated with the control.
formValue-unknownThe value used when submitting the form.
formatOptionsformat-optionsIntl.NumberFormatOptions | undefinedNumber formatting options. See Intl.NumberFormat options documentation for more details.
formattedValue-stringThe formatted value, to be used as the input value.
input-HTMLInputElementThe input element in the light DOM.
inputSizeinput-sizenumber | undefinedThe size attribute of the input element.
labels-`NodeListOf<HTMLLabelElement>` | nullThe labels associated with the control.
localelocalestringThe component's locale.
maxmaxnumber | undefinedInfinityThe maximum value that is acceptable and valid. If the value is greater, the control will be invalid.
maxLengthmaxlengthnumber | undefinedMaximum length (number of characters).
minminnumber | undefined-InfinityThe minimum value that is acceptable and valid. If the value is less, the control will be invalid.
minLengthminlengthnumber | undefinedMinimum length (number of characters).
namenamestring | undefinedThe name of the form control.
nativeFormValue-FormValueReturns the form value as used in a native <form>. This is always a string, File, FormData or null.
patternpatternstring | undefinedThis will validate the value of the input using the given pattern.
placeholderplaceholderstring | undefinedPlaceholder text in the input.
rawValue-string''The raw (string) value of the input.
readonlyreadonlyboolean | undefinedWhether you can interact with the input or if it is just a static, readonly display.
requiredrequiredboolean | undefinedWhether the number field is a required field.
showValidshow-validbooleanfalseOptional property to indicate the valid state should be shown.
showValidityshow-validity'valid' | 'invalid' | undefinedWhether to show the validity state.
sizesize'md' | 'lg' | undefinedmdThe size of the input.
stepstepnumber | undefined1The amount by which the value will be increased/decreased by a step up/down.
stepButtonsstep-buttons'end' | 'edges' | undefinedStep buttons placement for incrementing / decrementing. No step buttons by default.
touched-booleanfalseA control is marked touched once the user has triggered a blur event on it.
valid-booleanReturns whether the form control is valid or not.
validationMessage-stringString representing a localized (by the browser) message that describes the validation constraints that the control does not satisfy (if any). The string is empty if the control is not a candidate for constraint validation, or it satisfies its constraints.

For true localization, see getLocalizedValidationMessage() instead.

validity-ValidityStateReturns the validity state the control is in.
validityState-'valid' | 'invalid' | 'pending'Returns the current validity state.
valuevalueunknown | undefinedThe text value.
valueAsNumbervalueAsNumberThe number value.

Methods

NameParametersReturnDescription
renderPrefixTemplateResult | typeof nothingRenders the prefix slot content with step down button when step buttons are at edges.

Remember that if you override this method, the step down button will no longer be rendered automatically when stepButtons is set to 'edges'. You will need to implement your own button logic if needed.

renderSuffixTemplateResult | typeof nothingRenders the suffix slot content with step buttons.

Remember that if you override this method, the step buttons will no longer be rendered automatically. You will need to implement your own button logic if needed.

stepDowndecrement: numbervoidDecreases the current value by the step amount.
stepUpincrement: numbervoidIncreases the current value by the step amount.
reportValiditybooleanReturns whether the control is valid. If the control is invalid, calling this will also cause an invalid event to be dispatched. After calling this, the control will also report the validity to the user.
getLocalizedValidationMessagestringThis returns a localized validation message. It does not support all ValidityState properties, since some require more context than we have here. If you need to support more, you can override this method in your own form control.
setCustomValiditymessage: string | Promise<string>voidSets a custom validation message for the form control. If the message is not an empty string, that will make the control invalid. By setting it to an empty string again, you can make the control valid again.
renderInputSlotTemplateResultRender the input slot; separate method so it is composable for child components.
parseValuevalue: stringvoidMethod that parses the string input and converts it to a specific value. Override this method if you want to convert the value in a different way. Throw an error if the value is invalid.
onBlurvoidHandles the blur event when the input field loses focus. Emits a sl-blur event if the component had focus and updates the state.
onChangevoidThis method is called when the input changes.
onFocusvoidHandles the focus event when the input field gains focus. Emits a focus event and updates the focus ring state.
onInput{ target }: Event & { target: HTMLInputElement }voidHandles input events to update the raw and parsed values.
onKeydownevent: KeyboardEventvoidHandles the keydown event for the field. Simulates the native behavior of submitting a form when the Enter key is pressed.
onPrefixSlotChangeevent: Event & { target: HTMLSlotElement }voidHandles changes to the prefix slot. Detects and adds any FieldButton elements assigned to the prefix slot to the fieldButtons state for further processing.
onSlotChangeevent: Event & { target: HTMLSlotElement }voidHandles changes to the input slot. Updates the input element reference and synchronizes its attributes with the component's properties.
onSuffixSlotChangeevent: Event & { target: HTMLSlotElement }voidHandles changes to the suffix slot. Detects and adds any FieldButton elements assigned to the suffix slot to the fieldButtons state for further processing.

Events

NameEvent typeDescription
sl-form-controlSlFormControlEventEmits when the form control is added to the DOM.
sl-update-stateSlUpdateStateEventEmits when the UI state (dirty, pristine, touched or untouched) of the form control changes.
sl-update-validitySlUpdateValidityEventEmits when the validity of the form control changes.
sl-validateSlValidateEventEmits when the form control can be validated.
sl-blurSlBlurEventEmits when the focus leaves the component.
sl-changeSlChangeEvent<string | undefined>Emits when the value changes.
sl-focusSlFocusEventEmits when the component gains focus.

Slots

NameDescription
prefixUsed for step buttons when stepButtons is set to 'edges'. If overridden, the step down button will not be rendered automatically, and you will need to implement your own button logic.
suffixUsed for step buttons internally (when stepButtons is set). If overridden, the step buttons will not be rendered automatically, and you will need to implement your own button logic.
inputThe slot for the input element

Interactive example