| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- @using System.ComponentModel.DataAnnotations
- @using RackPeek.Domain.Resources.SystemResources
- @inject ISystemRepository SystemRepository
- @if (IsOpen)
- {
- <div class="fixed inset-0 z-50 flex items-center justify-center">
- <!-- Backdrop -->
- <div class="absolute inset-0 bg-black/70" @onclick="Cancel"></div>
- <!-- Modal -->
- <div class="relative bg-zinc-900 border border-zinc-800 rounded w-full max-w-lg p-4">
- <!-- Header -->
- <div class="flex justify-between items-center mb-4">
- <div class="text-zinc-100 text-sm font-medium">
- @Title
- </div>
- <button
- class="text-zinc-400 hover:text-zinc-200"
- @onclick="Cancel">
- ✕
- </button>
- </div>
- <!-- Form -->
- <EditForm Model="_model" OnValidSubmit="HandleAccept">
- <DataAnnotationsValidator />
- <div class="space-y-3 text-sm">
- <!-- Search -->
- <div>
- <InputText
- class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
- placeholder="Search systems…"
- Value="@_search"
- ValueChanged="OnSearchChanged"
- ValueExpression="() => _search"
- @oninput="OnSearchInput" />
- </div>
- <!-- Selection -->
- <div>
- <InputSelect
- class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
- @bind-Value="_model.Value">
- <option value="">Select system…</option>
- @foreach (var group in FilteredSystems)
- {
- <optgroup label="@group.Key">
- @foreach (var system in group.Value)
- {
- <option value="@system.Name">
- @system.Name
- </option>
- }
- </optgroup>
- }
- </InputSelect>
- </div>
- </div>
- <!-- Actions -->
- <div class="flex justify-end items-center mt-5 gap-2">
- <button
- type="button"
- class="px-3 py-1 rounded border border-zinc-700 text-zinc-300 hover:bg-zinc-800"
- @onclick="Cancel">
- Cancel
- </button>
- <button
- type="submit"
- class="px-3 py-1 rounded bg-emerald-600 text-black hover:bg-emerald-500"
- disabled="@(!CanAccept)">
- Accept
- </button>
- </div>
- </EditForm>
- </div>
- </div>
- }
- @code {
- /* ---------- Parameters ---------- */
- [Parameter] public bool IsOpen { get; set; }
- [Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
- [Parameter, EditorRequired]
- public string Title { get; set; } = "Select system";
- /// <summary>
- /// Default / pre-selected system name (may be null)
- /// </summary>
- [Parameter]
- public string? Value { get; set; }
- /// <summary>
- /// Called with the selected system name
- /// </summary>
- [Parameter]
- public EventCallback<string?> OnAccept { get; set; }
- /* ---------- State ---------- */
- private SelectionFormModel _model = new();
- private string _search = string.Empty;
- private List<SystemResource> _systems = new();
- private bool CanAccept => !string.IsNullOrWhiteSpace(_model.Value);
- /* ---------- Lifecycle ---------- */
- protected override async Task OnParametersSetAsync()
- {
- if (IsOpen)
- {
- _systems = (await SystemRepository.GetAllAsync())
- .OrderBy(s => s.Type)
- .ThenBy(s => s.Name)
- .ToList();
- _model = new SelectionFormModel
- {
- Value = Value
- };
- _search = string.Empty;
- }
- }
- /* ---------- Computed ---------- */
- private Dictionary<string, List<SystemResource>> FilteredSystems =>
- _systems
- .Where(s =>
- string.IsNullOrWhiteSpace(_search) ||
- s.Name.Contains(_search.Trim(), StringComparison.OrdinalIgnoreCase))
- .GroupBy(s => s.Type ?? "None")
- .ToDictionary(
- g => g.Key,
- g => g.OrderBy(s => s.Name).ToList());
- /* ---------- Actions ---------- */
- private async Task HandleAccept()
- {
- await OnAccept.InvokeAsync(_model.Value);
- await Close();
- }
- private async Task Cancel()
- {
- await Close();
- }
- private async Task Close()
- {
- _model = new();
- _search = string.Empty;
- await IsOpenChanged.InvokeAsync(false);
- }
- /* ---------- Model ---------- */
- private sealed class SelectionFormModel
- {
- [Required]
- public string? Value { get; set; }
- }
- /* ---------- Search ---------- */
- private void OnSearchChanged(string? value)
- {
- _search = value ?? string.Empty;
- }
- private void OnSearchInput(ChangeEventArgs e)
- {
- _search = e.Value?.ToString() ?? string.Empty;
- // Clear selection if it is no longer visible
- if (FilteredSystems
- .SelectMany(g => g.Value)
- .All(s => s.Name != _model.Value))
- {
- _model.Value = null;
- }
- }
- }
|