Skip to main content

LgDateBox

LgDateBox<TValue> enables input or selection of a day through a text field combined with a calendar (datepicker). It is the foundation of the date field family: LgMonthBox (month), LgWeekBox (week), and for similar functionality, LgYearBox (year).

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

The bound type TValue must be DateOnly, DateTime, or DateTimeOffset (or their nullable versions). The field's suffix is reserved: the component automatically places the calendar icon there (Suffix / SuffixType are overwritten at initialization).

Usage​

Demo​

Try this component in the Lagoon showcase.

Simple input​

Binding is done with @bind-Value. The default display format is the short date format of the current culture.

OrderEditPage.razor
<LgDateBox Label="#OrderDate" @bind-Value="_model.OrderDate" />
OrderEditPage.razor.cs
public partial class OrderEditPage : LgPage
{
// Model being edited: the date is nullable to allow an empty field.
private readonly OrderVm _model = new();

private sealed class OrderVm
{
public DateTime? OrderDate { get; set; }
}
}

Selection bounds​

Min and Max disable dates outside the bounds in the calendar and invalidate the corresponding keyboard input.

BookingPage.razor
<LgDateBox Label="#BookingDate"
Min="_minDate"
Max="_maxDate"
@bind-Value="_model.BookingDate" />
BookingPage.razor.cs
public partial class BookingPage : LgPage
{
// Booking window allowed: bounds the calendar and keyboard input.
private readonly DateTime _minDate = new(2026, 1, 1);
private readonly DateTime _maxDate = new(2026, 12, 31);

private readonly BookingVm _model = new();

private sealed class BookingVm
{
public DateOnly? BookingDate { get; set; }
}
}

Display format and keyboard input​

DisplayFormat replaces the short date format of the current culture. KeyboardDisabled forces input through the calendar (the field becomes readonly for keyboard input). AssumeNearbyYear controls the expansion of two-digit years: the input "12/05/28" becomes 2028 as long as the year remains within AssumeNearbyYear years in the future (20 by default; a value less than 1 disables this behavior).

ContractPage.razor
<LgDateBox Label="#SignatureDate"
DisplayFormat="dd/MM/yyyy"
AssumeNearbyYear="30"
@bind-Value="_model.SignatureDate" />

@* Selection only with the mouse, in the calendar. *@
<LgDateBox Label="#EffectiveDate"
KeyboardDisabled="true"
@bind-Value="_model.EffectiveDate" />
ContractPage.razor.cs
public partial class ContractPage : LgPage
{
private readonly ContractVm _model = new();

private sealed class ContractVm
{
public DateTime? SignatureDate { get; set; }
public DateTime? EffectiveDate { get; set; }
}
}

Time part of new values​

When the bound value is an empty DateTime or DateTimeOffset, the new value created at selection uses DefaultTime (midnight by default), DefaultDateTimeKind (Unspecified by default), and for DateTimeOffset, DefaultTimeOffset (local time zone offset by default). If the bound value already contains a date, only the date part is replaced: the existing time is preserved.

MeetingPage.razor
<LgDateBox Label="#MeetingDate"
DefaultTime="_defaultTime"
DefaultDateTimeKind="DateTimeKind.Local"
@bind-Value="_model.MeetingDate" />
MeetingPage.razor.cs
public partial class MeetingPage : LgPage
{
// Time assigned to new dates: avoids creating meetings at midnight.
private readonly TimeOnly _defaultTime = new(9, 0);

private readonly MeetingVm _model = new();

private sealed class MeetingVm
{
public DateTime? MeetingDate { get; set; }
}
}

Responding to value changes​

OnChangingAsync is called before assignment and allows canceling the change (args.Cancel). OnChange is called after. OnFocus notifies the focus event (this is also when the calendar loads).

PeriodPage.razor
<LgDateBox Label="#StartDate"
OnChangingAsync="OnStartDateChangingAsync"
OnChange="OnStartDateChanged"
@bind-Value="_model.StartDate" />
PeriodPage.razor.cs
public partial class PeriodPage : LgPage
{
private readonly PeriodVm _model = new();

// Called before the change: set args.Cancel to reject the new date
// (e.g., consistency with an already-entered end date).
private Task OnStartDateChangingAsync(ChangingEventArgs<DateTime?> args)
{
if (args.NewValue > _model.EndDate)
{
args.Cancel = true;
ShowWarning("#StartAfterEnd");
}
return Task.CompletedTask;
}

// Called after the change: useful for recalculating dependent data.
private void OnStartDateChanged(ChangeEventArgs args)
{
ShowInformation("#PeriodUpdated");
}

private sealed class PeriodVm
{
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
}
}

Required field and reset button​

Required forces the field to be mandatory (otherwise it is inferred from the [Required] attribute of the bound property). ResetButton displays a reset button; its default value comes from the application's behavior configuration.

DeadlinePage.razor
<LgEditForm Model="_model" OnValidSubmit="OnValidSubmitAsync">
<LgDateBox Label="#Deadline"
Required="true"
ResetButton="true"
@bind-Value="_model.Deadline" />
<LgButton Text="#Save" IsSubmit="true" />
</LgEditForm>
DeadlinePage.razor.cs
public partial class DeadlinePage : LgPage
{
private readonly DeadlineVm _model = new();

// Triggered only when the form is valid: the date is guaranteed to be filled.
private async Task OnValidSubmitAsync(EditContext editContext)
{
await Task.Delay(100);
ShowSuccess("#DeadlineSaved");
}

private sealed class DeadlineVm
{
public DateTime? Deadline { get; set; }
}
}

First day of the week​

WeekStart changes the first day displayed in the calendar (Monday by default).

PlanningPage.razor
<LgDateBox Label="#PlanningDate"
WeekStart="DayOfWeek.Sunday"
@bind-Value="_model.PlanningDate" />
PlanningPage.razor.cs
public partial class PlanningPage : LgPage
{
private readonly PlanningVm _model = new();

private sealed class PlanningVm
{
public DateOnly? PlanningDate { get; set; }
}
}

Properties​

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

NameTypeDefaultDeclared inDescription
AssumeNearbyYearint20LgDateBoxNumber of years in the future for which a two-digit year entered is attached to the current century. A value less than 1 disables this behavior.
DefaultDateTimeKindDateTimeKindUnspecifiedLgDateBoxDateTimeKind used to create a new DateTime.
DefaultTimeTimeOnly?nullLgDateBoxTime used to create a new DateTime or DateTimeOffset (midnight if null).
DefaultTimeOffsetTimeSpan?nullLgDateBoxTime offset used to create a new DateTimeOffset (local time zone offset if null).
DisplayFormatstringnullLgDateBoxDate display format (short date format of the current culture if null).
KeyboardDisabledboolfalseLgDateBoxPrevents keyboard editing: the value can only be selected from the calendar.
MaxDateTime?nullLgDateBoxLatest selectable date; later dates are disabled.
MinDateTime?nullLgDateBoxEarliest selectable date; earlier dates are disabled.
OnChangeDisabledboolfalseLgDateBoxDisables the DOM onchange event in favor of oninput (used to avoid errors when the control is removed from rendering).
OnChangingAsyncFunc<ChangingEventArgs<TValue>, Task>nullLgDateBoxCallback invoked before value change; the change can be canceled there.
OnFocusEventCallback<FocusEventArgs>—LgDateBoxCallback invoked on field focus.
PlaceholderstringnullLgDateBoxHint text displayed when the field is empty.
WeekStartDayOfWeekMondayLgDateBoxFirst day of the week in the calendar.
PrefixstringnullLgInputRenderBasePrefix displayed in the field.
PrefixTypeInputLabelTypeTextLgInputRenderBaseNature of the prefix.
Suffixstring(calendar icon)LgInputRenderBaseSuffix displayed in the field. Reserved: the component places the calendar icon there.
SuffixTypeInputLabelTypeIconNameLgInputRenderBaseNature of the suffix. Reserved by the component.
AdditionalAttributesIReadOnlyDictionary<string, object>nullLgInputBaseAdditional HTML attributes applied to the rendered element.
AutoFocusboolfalseLgInputBaseFocuses 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 content for the label area.
OnChangeEventCallback<ChangeEventArgs>—LgInputBaseCallback invoked after value change.
OnResetEventCallback<ChangeEventArgs>—LgInputBaseCallback invoked when the field is reset by the reset button.
PolicyEditstringnullLgInputBasePolicy required to edit the field.
PolicyVisiblestringnullLgInputBasePolicy required to display the field.
ReadOnlyboolfalseLgInputBaseDisplays the field as read-only.
Requiredbool?nullLgInputBaseRequired field. If null, inferred from the [Required] attribute of the bound property.
ResetButtonbool?nullLgInputBaseDisplays the reset button. If null, the application's behavior configuration applies.
ResetTextstringnullLgInputBaseReset button text.
ResetTextAriaLabelstringnullLgInputBaseAccessible label (aria-label) of the reset button.
TooltipstringnullLgInputBaseTooltip.
TooltipIsHtmlboolfalseLgInputBaseIndicates whether the Tooltip content is in HTML format (otherwise plain text).
ValueTValue—LgInputBaseBound value (@bind-Value).
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

LgDateBox also consumes parameters automatically provided by ancestor components (you do not 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.
OnFocusEventCallback<FocusEventArgs>On field focus.
OnResetEventCallback<ChangeEventArgs>On reset by the reset button.

Associated enumerations​

InputLabelType​

Nature of the content of Prefix / Suffix.

ValueDescription
IconNameName of an icon from the library.
TextPlain text (default value).

Best practices / accessibility​

  • Prefer DateOnly? when only the date matters: this avoids surprises from time and time zone parts specific to DateTime / DateTimeOffset.
  • Do not set Suffix / SuffixType: they are overwritten by the calendar icon at component initialization.
  • For a "month," "week," or "year" field, use the specialized component instead of LgDateBox with a custom format.
Complete reference

All parameters (including inherited ones): API Reference — LgDateBox.