Files
Heimdall/src/Heimdall.Web/Components/Shared/TimezoneAutocomplete.razor
T

69 lines
2.7 KiB
Plaintext
Raw Normal View History

@* TimezoneAutocomplete.razor — sélecteur de fuseau horaire avec recherche + autocomplete.
La valeur échangée est l'Id IANA (ex: "Europe/Paris"), compatible avec la validation
backend TimeZoneInfo.FindSystemTimeZoneById (CreateSite/UpdateSite). *@
<MudAutocomplete T="TimeZoneInfo"
Value="_selected"
ValueChanged="OnSelectedChanged"
SearchFunc="SearchTimeZones"
ToStringFunc="@(tz => tz?.Id ?? "")"
Label="@EffectiveLabel"
Placeholder="@EffectivePlaceholder"
HelperText="@HelperText"
Variant="Variant.Outlined"
Class="@Class"
Clearable="true"
CoerceValue="true"
DebounceInterval="100" />
@code {
/// <summary>Id IANA du fuseau sélectionné (ex: Europe/Paris), null si vide.</summary>
[Parameter] public string? Value { get; set; }
[Parameter] public EventCallback<string?> ValueChanged { get; set; }
[Parameter] public string Label { get; set; } = "";
[Parameter] public string Placeholder { get; set; } = "";
[Parameter] public string? HelperText { get; set; }
[Parameter] public string? Class { get; set; }
private static readonly TimeZoneInfo[] AllTimeZones = TimeZoneInfo.GetSystemTimeZones().ToArray();
private TimeZoneInfo? _selected;
[Inject] private Heimdall.Web.Services.UiText Text { get; set; } = default!;
private string EffectiveLabel => string.IsNullOrEmpty(Label) ? Text["SharedTimezoneAutocomplete_Label"] : Label;
private string EffectivePlaceholder => string.IsNullOrEmpty(Placeholder) ? Text["SharedTimezoneAutocomplete_Placeholder"] : Placeholder;
protected override void OnParametersSet()
{
if (_selected?.Id != Value)
_selected = string.IsNullOrEmpty(Value)
? null
: AllTimeZones.FirstOrDefault(tz => tz.Id == Value);
}
private async Task OnSelectedChanged(TimeZoneInfo? tz)
{
_selected = tz;
await ValueChanged.InvokeAsync(tz?.Id);
}
private static Task<IEnumerable<TimeZoneInfo>> SearchTimeZones(string? text, CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(text))
return Task.FromResult(AllTimeZones.Take(50));
var needle = text.Trim();
var matches = AllTimeZones
.Where(tz => tz.Id.Contains(needle, StringComparison.OrdinalIgnoreCase)
|| tz.DisplayName.Contains(needle, StringComparison.OrdinalIgnoreCase))
.OrderBy(tz => tz.Id, StringComparer.OrdinalIgnoreCase)
.Take(50);
return Task.FromResult(matches);
}
}