Files
Khaemhat/src/Khaemhat.Web/Components/Ui/KhDateBox.razor
T
Alby96andClaude Opus 5.5 d958e7372d feat: Kh component library, record pages and profile security (M3, part 2)
Adds the building blocks every later screen uses, so modules are built from
one set of components instead of ad hoc markup:

- Form components (KhForm, KhTextBox, KhNumberBox, KhDateBox, KhSelect,
  KhCheck, KhSwitch, KhHotlink) with Italian number and date input, inline
  calculation (=10*3), field messages plus a summary that focuses the field,
  and service errors added by field name.
- Display and window components (panels, tiles, badges, chips, money, tabs
  with Alt+1..9, steps, dialog, side drawer and DrawerService).
- KhIconToolbar that keeps one line and moves less used commands into the
  "..." menu, KhDocHead, and the RecordPage/DocumentPage/MasterDataPage bases
  with standard commands, shortcuts and save/cancel/delete flow (#27, #29).
- Development-only component catalog and sample document page.
- Profile: personal data, password change that ends other sessions, TOTP
  enable/disable and recovery codes through ProfileService (#31).

Refs #27 #29 #31

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:11:16 +01:00

115 lines
3.5 KiB
Plaintext

@inherits KhInputBase<DateOnly?>
@inject TimeProvider Clock
@* A date as gg/mm/aaaa (SPEC §10.7): short forms (5, 5/3, 050326), T today, F end of month, I start of month, + and -
one day (typed as text, or a key press when the field holds a whole date), arrows up and down one day. *@
<KhField Label="@Label" For="@InputId" Required="Required" Hint="@Hint" Messages="Messages" Span="Span">
<span class="in kh-datebox @(Messages.Count > 0 ? "err" : null) @(IsReadOnly ? "ro" : null)">
<input id="@InputId" value="@_text" placeholder="@T["Ui.Date.Placeholder"]" inputmode="numeric" maxlength="10"
readonly="@IsReadOnly" disabled="@Disabled" aria-required="@AriaRequired" aria-invalid="@AriaInvalid"
aria-describedby="@DescribedBy" data-kh-prevent="@(IsReadOnly ? null : "ArrowUp ArrowDown")" data-kh-datestep
@onchange="OnChangeAsync" @onkeydown="OnKeyDownAsync" @attributes="Attributes" @ref="_input" />
<KhIcon Name="calendar" Class="sm" />
</span>
</KhField>
@code {
private string _text = string.Empty;
private DateOnly? _shown;
private bool _first = true;
private ElementReference _input;
/// <summary>The first date accepted.</summary>
[Parameter]
public DateOnly? Min { get; set; }
/// <summary>The last date accepted.</summary>
[Parameter]
public DateOnly? Max { get; set; }
[Inject]
private IJSRuntime JS { get; set; } = default!;
private DateOnly Today => DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(Clock.GetUtcNow(), TimeZoneInfo.Local).DateTime);
/// <inheritdoc />
protected override void OnParametersSet()
{
base.OnParametersSet();
if (_first || (ParseError is null && Value != _shown))
{
_first = false;
_shown = Value;
_text = Value is { } date ? T.Date(date) : string.Empty;
}
}
/// <inheritdoc />
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JS.InvokeVoidAsync("khaemhat.preventKeys", _input);
}
}
/// <inheritdoc />
public override IEnumerable<string> Validate()
{
foreach (var message in base.Validate())
{
yield return message;
yield break;
}
if (Value is { } v && Min is { } min && v < min)
{
yield return T.Format("Ui.Validation.DateMin", T.Date(min));
}
else if (Value is { } w && Max is { } max && w > max)
{
yield return T.Format("Ui.Validation.DateMax", T.Date(max));
}
}
private async Task OnChangeAsync(ChangeEventArgs e)
{
_text = e.Value as string ?? string.Empty;
StateHasChanged();
await Task.Yield();
await ApplyAsync(_text);
}
private async Task OnKeyDownAsync(KeyboardEventArgs e)
{
if (IsReadOnly || e.CtrlKey || e.AltKey)
{
return;
}
var step = e.Key switch
{
"ArrowUp" => "+",
"ArrowDown" => "-",
_ => null,
};
if (step is not null)
{
await ApplyAsync(step);
}
}
private async Task ApplyAsync(string text)
{
if (!ItalianDate.TryParse(text, Today, Value, out var date))
{
ParseError = T["Ui.Validation.Date"];
return;
}
_shown = date;
_text = date is { } d ? T.Date(d) : string.Empty;
await SetValueAsync(date);
}
}