Skip to main content

LgNumericBox

LgNumericBox<TValue> allows you to input a number. The field filters entered characters, applies a display format, rounds according to the configured mode, and can display increment/decrement buttons. The bound value is a numeric type (int, long, decimal, double, float… or their nullable version).

<LgNumericBox Label="#Quantity" @bind-Value="_model.Quantity" />
  • Namespace : Lagoon.UI.Components
  • Inherits from : LgInputRenderBase<TValue> → LgInputBase<TValue> → LgAriaComponentBase → LgComponentBase
  • NuGet package : Lagoon.UI

Usage​

Demonstration​

Try this component in the Lagoon showcase.

Simple input​

The default number of decimals depends on the bound type (0 for integers) and the default display format is N<Decimals>. On mobile, the numeric virtual keyboard is forced (inputmode="decimal").

OrderLinePage.razor
<LgNumericBox Label="#Quantity" @bind-Value="_model.Quantity" />
OrderLinePage.razor.cs
public partial class OrderLinePage : LgPage
{
private readonly OrderLineVm _model = new();

private sealed class OrderLineVm
{
public int? Quantity { get; set; }
}
}

Decimals, format, and rounding​

Decimals sets the number of decimals, DisplayFormat replaces the display format, and RoundValueMode the rounding mode (by default, the one from the application behavior configuration). Prefix / Suffix (inherited) allow you to display a unit.

PricePage.razor
<LgNumericBox Label="#UnitPrice"
Decimals="2"
RoundValueMode="MidpointRounding.AwayFromZero"
Suffix="€"
SuffixType="InputLabelType.Text"
@bind-Value="_model.UnitPrice" />
PricePage.razor.cs
public partial class PricePage : LgPage
{
private readonly PriceVm _model = new();

private sealed class PriceVm
{
public decimal? UnitPrice { get; set; }
}
}

Increment buttons​

IncrementStep (non-zero) displays increase / decrease buttons in the field, which vary the value by the indicated step.

StockPage.razor
<LgNumericBox Label="#StockLevel"
IncrementStep="10"
@bind-Value="_model.StockLevel" />
StockPage.razor.cs
public partial class StockPage : LgPage
{
private readonly StockVm _model = new();

private sealed class StockVm
{
public int StockLevel { get; set; }
}
}

Respond to value changes​

OnChangingAsync is called before assignment and allows you to cancel the change (args.Cancel); OnChange (inherited) is called after. OnInput notifies on each keystroke, OnKey on each key release, and OnFocus on focus gain. Resetting via the clear button also triggers OnInput, with an empty value (""), unless canceled (OnChangingAsync, ConfirmationMessage). ParsingErrorMessage customizes the message displayed when the input is not a valid number.

DiscountPage.razor
<LgNumericBox Label="#DiscountRate"
Decimals="1"
ParsingErrorMessage="#DiscountFormatError"
OnChangingAsync="OnDiscountChangingAsync"
@bind-Value="_model.DiscountRate" />
DiscountPage.razor.cs
public partial class DiscountPage : LgPage
{
private readonly DiscountVm _model = new();

// Called before the change: constrains the discount without going through form validation.
private Task OnDiscountChangingAsync(ChangingEventArgs<double?> args)
{
if (args.NewValue is < 0 or > 100)
{
args.Cancel = true;
ShowWarning("#DiscountOutOfRange");
}
return Task.CompletedTask;
}

private sealed class DiscountVm
{
public double? DiscountRate { get; set; }
}
}

Properties​

All [Parameter] properties of the component, including inherited ones.

NameTypeDefaultDeclared inDescription
Decimalsint?nullLgNumericBoxNumber of decimals. If null, deduced from the bound type (0 for integers).
DisplayFormatstringnullLgNumericBoxDisplay format. If null, N<Decimals> is used.
IncrementStepTValuedefaultLgNumericBoxStep for increase/decrease buttons. Buttons are displayed only if the step is non-zero.
OnChangeDisabledboolfalseLgNumericBoxDisables the DOM onchange event in favor of oninput (used to avoid errors when the control is removed from rendering).
OnChangingAsyncFunc<ChangingEventArgs<TValue>, Task>nullLgNumericBoxCallback called before value change; the change can be canceled there.
OnFocusEventCallback<FocusEventArgs>—LgNumericBoxCallback called on focus gain.
OnInputEventCallback<ChangeEventArgs>—LgNumericBoxCallback called on each field content modification, including reset via the clear button (empty value "").
OnKeyEventCallback<KeyboardEventArgs>—LgNumericBoxCallback called on key release.
ParsingErrorMessagestring#lgNumericBoxErrFormatLgNumericBoxError message displayed when the input is not a valid number.
PlaceholderstringnullLgNumericBoxPlaceholder text displayed when the field is empty.
RoundValueModeMidpointRounding?nullLgNumericBoxRounding mode. If null, the mode from the application behavior configuration applies.
PrefixstringnullLgInputRenderBasePrefix displayed in the field (e.g. a currency).
PrefixTypeInputLabelTypeTextLgInputRenderBaseNature of the prefix.
SuffixstringnullLgInputRenderBaseSuffix displayed in the field (e.g. a unit).
SuffixTypeInputLabelTypeTextLgInputRenderBaseNature of the suffix.
AdditionalAttributesIReadOnlyDictionary<string, object>nullLgInputBaseAdditional HTML attributes applied to the rendered element.
AutoFocusboolfalseLgInputBaseGives focus to the field on first render.
ConfirmationMessagestringnullLgInputBaseConfirmation message displayed before applying a value change.
ContainerIInputContainer<TValue>nullLgInputBaseInternal use: attaches a GridView cell to the field.
CssClassstringnullLgInputBaseAdditional CSS classes.
DisabledboolfalseLgInputBaseDisables the field.
DisplayNamestringnullLgInputBaseField name used in input validation error messages.
HideValidationMessageboolfalseLgInputBaseHides the validation message below the field.
LabelstringnullLgInputBaseField label.
LabelContentRenderFragmentnullLgInputBaseCustom label area content.
OnChangeEventCallback<ChangeEventArgs>—LgInputBaseCallback called after value change.
OnResetEventCallback<ChangeEventArgs>—LgInputBaseCallback called when the field is reset by the clear button.
PolicyEditstringnullLgInputBasePolicy required to edit the field.
PolicyVisiblestringnullLgInputBasePolicy required to display the field.
ReadOnlyboolfalseLgInputBaseDisplays the field as read-only.
Requiredbool?nullLgInputBaseRequired field. If null, deduced from the [Required] attribute of the bound property.
ResetButtonbool?nullLgInputBaseDisplays the clear button. If null, the application behavior configuration applies.
ResetTextstringnullLgInputBaseClear button text.
ResetTextAriaLabelstringnullLgInputBaseAccessible label (aria-label) of the clear button.
TooltipstringnullLgInputBaseTooltip.
TooltipIsHtmlboolfalseLgInputBaseIndicates whether the Tooltip content is in HTML format (otherwise plain text).
ValueTValue—LgInputBaseBound value (@bind-Value), of numeric type.
ValueChangedEventCallback<TValue>—LgInputBaseCallback for updating the bound value.
ValueExpressionExpression<Func<TValue>>—LgInputBaseExpression identifying the bound property.
AriaLabelstringnullLgAriaComponentBaseValue of the aria-label attribute.
AriaLabelledBystringnullLgAriaComponentBaseValue of the aria-labelledby attribute.
Cascading parameters

LgNumericBox also consumes parameters automatically provided by ancestor components (you don't need to define them): ParentAuthorizeView (LgAuthorizeView) and the edit context of a parent LgEditForm.

Events​

EventTypeDescription
OnChangingAsyncFunc<ChangingEventArgs<TValue>, Task>Before value change; args.Cancel = true cancels the change.
OnChangeEventCallback<ChangeEventArgs>After value change.
OnInputEventCallback<ChangeEventArgs>On each field content modification, including reset via the clear button (empty value "", unless the reset is canceled).
OnKeyEventCallback<KeyboardEventArgs>On key release in the field.
OnFocusEventCallback<FocusEventArgs>On field focus gain.
OnResetEventCallback<ChangeEventArgs>On reset by the clear button.

Best practices / Accessibility​

  • Choose the bound type according to your business need (decimal for amounts): the default number of decimals follows from it.
  • For a unit or currency, prefer Prefix / Suffix over a label in the Label: the information remains visible close to the value.
Complete reference

All parameters (inherited ones included): API Reference — LgNumericBox.