Basic Numeric Fields
Numeric fields are just like text fields but work well with numeric values of any type. The input is automatically restricted to numeric values and it works regardless of the browser locale settings for decimal types.
Min
and Max
attributes allow to restrict the value within the limits. If they are not specified, the full value range for the type (i.e -2147483648
and 2147483647
for int
) is permitted.
The Step
attribute defines how much the value changes when using the up/down buttons on the right, or with the up/down keys on the keyboard. If not specified, the default value is 1.
<MudNumericField @bind-Value="IntValue" Label="Standard" Variant="Variant.Text" Min="0" Max="10" /> <MudNumericField @bind-Value="DoubleValue" Label="Filled" Variant="Variant.Filled" Min="0.0" /> <MudNumericField @bind-Value="DecimalValue" Label="Outlined" Variant="Variant.Outlined" Step=".2M" />
@code { public int IntValue { get; set; } public double DoubleValue { get; set; } public decimal DecimalValue { get; set; } }
<MudNumericField HideSpinButtons="true" @bind-Value="IntValue" Label="Standard" Variant="Variant.Text" Min="0" Max="10" /> <MudNumericField HideSpinButtons="true" @bind-Value="DoubleValue" Label="Filled" Variant="Variant.Filled" Min="0.0" /> <MudNumericField HideSpinButtons="true" @bind-Value="DecimalValue" Label="Outlined" Variant="Variant.Outlined" Step=".2M" />
@code { public int IntValue { get; set; } public double DoubleValue { get; set; } public decimal DecimalValue { get; set; } }
Setting Culture and Format
You can set a custom culture and format which will be used for parsing the numeric value from the input string as well as for presentation.
@using System.Globalization <MudNumericField Immediate="false" Label="de-DE" Format="N2" Culture="@_de" T="double?" @bind-Value="_valueDe" HelperText="@_valueDe.ToString()"/> <MudNumericField Immediate="false" Label="en-US" Format="N2" Culture="@_en" T="double?" @bind-Value="_valueEn" HelperText="@_valueEn.ToString()"/>
@code { public CultureInfo _de = CultureInfo.GetCultureInfo("de-DE"); public CultureInfo _en = CultureInfo.GetCultureInfo("en-US"); public double? _valueDe = 1234.56; public double? _valueEn = 1234.56; }
Binding Value Types vs Nullables
When you bind value types, the numeric field will not be empty even if the user hasn't entered a value yet because a value type always has a value, even when unassigned.
The two-way-bindable Value
property will automatically assume the default value of that type (i.e. 0
for int
).
Thus, if you want your numeric fields to be empty, as long as nothing has been entered yet or after the field has been cleared, use the nullable version of that type (i.e. int?
).
Do not assign null values to Min
, Max
and Step
.
<MudNumericField @bind-Value="intValue" Label="Enter an int" Variant="Variant.Outlined" /> <MudNumericField @bind-Value="doubleValue" Label="Enter a double" Format="F1" Variant="Variant.Outlined" /> <MudNumericField @bind-Value="nullableInt" Label="Enter an int" Variant="Variant.Outlined" /> <MudNumericField @bind-Value="nullableDouble" Label="Enter a double" Format="F1" Variant="Variant.Outlined" />
@code { int intValue; double doubleValue; int? nullableInt; double? nullableDouble; }
Normal vs Immediate vs Debounced
By default, MudNumericField
updates the bound value on Enter or when it loses focus. Set Immediate="true"
to update the value whenever the user types.
You can also set the DebounceInterval
parameter to the number of milliseconds you want to wait before updating the bound value. If you need to know when the interval elapses, you can pass an OnDebounceIntervalElapsed
EventCallback.
Notice how in this example the debounced text only updates 500 ms after you stop typing.
<MudNumericField @bind-Value="_normal" Label="Normal" HelperText="@_normal.ToString()" Variant="Variant.Outlined" /> <MudNumericField @bind-Value="_immediate" Immediate="true" Label="Immediate" HelperText="@_immediate.ToString()" Variant="Variant.Outlined" /> <MudNumericField @bind-Value="_debounced" DebounceInterval="500" OnDebounceIntervalElapsed="HandleIntervalElapsed" Label="Debounced" HelperText="@_debounced.ToString()" Variant="Variant.Outlined" />
@code { double _normal; double _immediate; double _debounced; void HandleIntervalElapsed(string debouncedText) { // At this stage, interval has elapsed. } }
Change the Value with the Mouse Wheel
You can change MudNumericField's value (by step) with Shift + MouseWheel.
You can also set the InvertMouseWheel
parameter to reverse the mouse wheel up and down event.
<MudNumericField @bind-Value="_normal" InvertMouseWheel="@_invertMouseWheel" Label="Use Shift + Mouse Wheel" Variant="Variant.Outlined" /> <MudCheckBox Class="mt-3" @bind-Value="_invertMouseWheel" Label="Revert Mouse Wheel" Color="Color.Primary" />
@code { double _normal = 0; bool _invertMouseWheel = false; }