Forms
TwNumeric
TwNumeric is a Blazor number input component. Bind any numeric type, set Min, Max and Step, format by culture, and add a prefix, suffix or clear button.
Last updated
Please be aware this component is new, may have bugs and/or be subject to change. Please report any bugs at
https://github.com/twblazor/twblazor/issues.
Basic usage
Bind
Value with @bind-Value. The field is a TwTextfield with the spinbutton role, so screen readers announce its value and limits. Use the − and + buttons, Arrow Up and Arrow Down, or the mouse wheel while it has focus, to step the value.Quantity: 1
Numeric types
T can be any built-in integer type, decimal, double or float. Integer types only accept whole numbers, and the mobile keyboard matches: a number pad for integers, and one with a decimal separator otherwise.Min, max and step
Min and Max limit the value: a typed value outside them is moved to the nearest limit, stepping stops at them, and the − or + button is disabled once it is reached. Step sets how far one step goes, and Page Up and Page Down take ten steps at once.Formatting and culture
Set
Format to any .NET numeric format string, such as N2 for two decimal places with thousands separators. The text is tidied into that format when it is committed. Typed text is parsed in Culture, which defaults to the culture set through options.Localization.Value: 1250
Prefix and suffix
Use
Prefix and Suffix to show a currency symbol or a unit inside the field. They are not part of the value, and screen readers hear them with it through aria-valuetext, such as "£12.50" or "75 kg".Nullable and clearable
Use a nullable
T, such as int?, to let the field be left empty: emptying it sets the value to null. Clearable shows the textfield's clear button, disabled while the field is empty so the buttons never shift under the pointer. Clearing a non-nullable value sets it to zero, kept within Min and Max.Age: null
Update on input
Typed text is committed when the field loses focus or Enter is pressed. Set
BindEvent="oninput" to update the value on every keystroke instead. Text that is not a number is only flagged once the user leaves the field.Total: 0
Allowed characters
Keys that cannot be part of a number are ignored: by default the digits, the culture's separators and, while
Min is below zero, the minus sign. Set Pattern to a regular expression each typed character must match. Pasted text is parsed instead, and flagged when it is not a number.Variants and dense
Variant and Dense work exactly as they do on TwTextfield, so a numeric field lines up with the other inputs.Disabled and readonly
Disabled dims the field and its buttons. ReadOnly keeps the value readable and focusable, but it cannot be stepped or typed over.RAZOR
<div class="grid gap-4 sm:grid-cols-2">
<TwNumeric T="int" Label="Disabled" Value="12" Disabled />
<TwNumeric T="int" Label="Readonly" Value="12" ReadOnly />
</div>Validation
Validation works as it does on
TwTextfield: pass a Validator that returns an error message, or null when the value is valid, or set Invalid and ErrorMessage yourself. Text that is not a number shows "Enter a valid number." The message is tied to the field with aria-describedby, so screen readers read it with the field.You can buy up to 4 tickets.
Theme configuration
Styling is controlled by
TwBlazorTheme.Components (a TwNumericTheme), configured via the
AddTwBlazor() options callback described on the Get Started page.
Every property is a set of Tailwind classes, so changes are picked up by your own Tailwind build.
| Property | Type | Controls |
|---|---|---|
Adornment |
string | |
Button |
string | |
ButtonIcon |
string |
Full reference: TwNumericTheme on the API docs.