Number Field
Allows users to enter a number in a text field or use buttons to increment or decrement the value.
The label
attribute provides a short description of the purpose of the Number Field.
If a visible label can't be used, provide one using the aria-label
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <vwc-number-field label="Wanted quantity"></vwc-number-field>
The placeholder
attribute provides an example of the type of input the user needs to enter.
Avoid using placeholder
text as a substitute for a label. Placeholder text is not a reliable label—it disappears when users type and is not always announced by screen readers. Use a label
element to ensure the Combobox is both visually and programmatically associated with a descriptive label.
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <vwc-number-field placeholder="100" label="Wanted quantity"></vwc-number-field>
The helper-text
attribute provides additional information to help the user enter the correct information.
To add HTML to the helper text, use the helper-text slot.
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <vwc-number-field label="Helper text below" helper-text="Help text" ></vwc-number-field>
Set the value
attribute to set the default value for the number field.
Setting the property on the element will not change the default value, but will change the value shown in the view as well as the submitted value in a form (imitating the native behavior).
Values always use the period (".") as the decimal separator, regardless of the user's locale.
Only the value on the screen is localized.
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <vwc-number-field label="With default value" value="5"></vwc-number-field>
Use the valueAsNumber
property to get or set the value as a number. If no valid value is entered in the field, the valueAsNumber
is NaN
.
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <vwc-number-field label="Quantity"></vwc-number-field> <p>valueAsNumber: <span id="value"></span></p> <script> function update() { document.getElementById('value').textContent = document.querySelector('vwc-number-field').valueAsNumber; } customElements.whenDefined('vwc-number-field').then(update); document.querySelector('vwc-number-field').addEventListener('input', update); </script>
The success-text
attribute provides a custom success message. Any current error state will be overridden by success-text
.
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <vwc-number-field label="Valid value" success-text="Great success" ></vwc-number-field>
The scale
attribute controls the Text Field input element display size.
Use condensed
in situations when space is limited, for example, inside a Data Grid cell.
The reason for using scale
for form elements and not size
(as used in other components such as Button), is that size
is a HTML attribute that can be used on input
elements (and also Number Field) to control the width of the input.
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <div class="container"> <vwc-number-field label="Condensed" scale="condensed"></vwc-number-field> <vwc-number-field label="Normal" scale="normal"></vwc-number-field> </div> <style> .container { display: flex; gap: 16px; } </style>
The shape
attribute controls the border radius of the Number Field input element.
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <div class="container"> <vwc-number-field label="Pill" shape="pill"></vwc-number-field> <vwc-number-field label="Rounded" shape="rounded"></vwc-number-field> </div> <style> .container { display: flex; gap: 16px; } </style>
The appearance
attribute controls the style of the Number Field input element.
Use ghost
in combination with a containing element which provides a border, for example Action Group.
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <div class="container"> <vwc-number-field appearance="fieldset" placeholder="appearance" label="fieldset" ></vwc-number-field> <vwc-number-field appearance="ghost" placeholder="appearance" label="ghost" ></vwc-number-field> </div> <style> .container { display: flex; gap: 16px; } </style>
Add the disabled
attribute to disable the Number field input element.
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <vwc-number-field disabled value="disabled" label="fieldset" appearance="fieldset" ></vwc-number-field>
The readonly
attribute prevents the user from changing the Number Field input element value.
<!-- Feel free to edit the code below. The live preview will update as you make changes. --> <vwc-number-field readonly value="8" label="fieldset" appearance="fieldset" ></vwc-number-field>