Keep cloned collection edits inside their owning driver dialog
This commit is contained in:
+126
-73
@@ -1,14 +1,12 @@
|
||||
@* Generic modal-per-row list editor. The parent owns the List<TRow> (a MUTABLE row VM,
|
||||
because driver contracts are immutable records). This renders a read-only table with
|
||||
Add/Edit/Delete and a modal that edits a CLONED working copy — commit on Save, discard
|
||||
on Cancel. NewRow builds a default VM; Clone copies one for the working copy; Validate
|
||||
(optional) returns an error string to block commit or null to allow. *@
|
||||
@* Parent-owned mutable rows use a cloned inline working copy: Save commits to the parent
|
||||
draft; Cancel discards it. The surrounding driver dialog owns focus trapping and Escape. *@
|
||||
@typeparam TRow
|
||||
|
||||
@implements IDisposable
|
||||
@{ var generation = _generation; var items = Items; var changed = ItemsChanged; _anchors.RemoveAll(a => IndexOf(items, a) < 0); }
|
||||
<section class="panel rise mt-3" style="@_styleDelay">
|
||||
<div class="panel-head d-flex align-items-center">
|
||||
<div class="panel-head d-flex align-items-center gap-2 flex-wrap">
|
||||
<span>@Title (@Items.Count)</span>
|
||||
<button type="button" class="btn btn-sm btn-outline-primary ms-auto" @onclick="Add">+ Add @ItemNoun</button>
|
||||
<button @ref="_addButton" type="button" class="btn btn-sm btn-outline-primary ms-auto" disabled="@(_session is not null || _busy)" @onclick="() => Add(generation, items, changed)">+ Add @ItemNoun</button>
|
||||
</div>
|
||||
@if (Items.Count == 0)
|
||||
{
|
||||
@@ -16,19 +14,21 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="table-wrap">
|
||||
<div class="table-wrap" role="region" aria-label="@Title" tabindex="0">
|
||||
<table class="data-table">
|
||||
<thead>@HeaderTemplate</thead>
|
||||
<tbody>
|
||||
@for (var i = 0; i < Items.Count; i++)
|
||||
{
|
||||
var idx = i;
|
||||
<tr @key="Items[idx]">
|
||||
@RowTemplate(Items[idx])
|
||||
var row = Items[idx];
|
||||
var anchor = AnchorFor(row, idx);
|
||||
<tr @key="row">
|
||||
@RowTemplate(row)
|
||||
<td class="text-end" style="white-space:nowrap">
|
||||
<div class="btn-group btn-group-sm" role="group">
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="() => Edit(idx)">Edit</button>
|
||||
<button type="button" class="btn btn-outline-danger" @onclick="() => Delete(idx)">Delete</button>
|
||||
<div class="btn-group btn-group-sm" role="group" aria-label="@($"{ItemNoun} {idx + 1} actions")">
|
||||
<button @ref="anchor.EditButton" type="button" class="btn btn-outline-secondary" disabled="@(_session is not null || _busy)" aria-label="@($"Edit {ItemNoun} {idx + 1}")" @onclick="() => Edit(generation, items, changed, anchor)">Edit</button>
|
||||
<button type="button" class="btn btn-outline-danger" disabled="@(_session is not null || _busy)" aria-label="@($"Delete {ItemNoun} {idx + 1}")" @onclick="() => Delete(generation, items, changed, anchor)">Delete</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -39,32 +39,22 @@
|
||||
}
|
||||
</section>
|
||||
|
||||
@if (_modalOpen && _working is not null)
|
||||
@if (_session is { } session)
|
||||
{
|
||||
<div class="modal-backdrop fade show" style="display:block"></div>
|
||||
<div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
|
||||
<div class="modal-dialog modal-lg" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">@(_editIndex is null ? $"Add {ItemNoun}" : $"Edit {ItemNoun}")</h5>
|
||||
<button type="button" class="btn-close" aria-label="Close" @onclick="Cancel"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
@EditTemplate(_working)
|
||||
@if (!string.IsNullOrEmpty(_validationError))
|
||||
{
|
||||
<div class="text-danger small mt-2">@_validationError</div>
|
||||
}
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<div class="btn-group" role="group">
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="Cancel">Cancel</button>
|
||||
<button type="button" class="btn btn-primary" @onclick="Commit">Save</button>
|
||||
</div>
|
||||
</div>
|
||||
<section class="panel mt-3" role="group" aria-labelledby="@(_id + "-title")" aria-describedby="@(string.IsNullOrEmpty(_validationError) ? null : _id + "-error")">
|
||||
<div class="panel-head"><h3 @ref="_editorTitle" id="@(_id + "-title")" class="h6 mb-0" tabindex="-1">@session.Caption</h3></div>
|
||||
<div class="p-3">
|
||||
<fieldset disabled="@_busy">@session.Editor(session.Working)</fieldset>
|
||||
@if (!string.IsNullOrEmpty(_validationError))
|
||||
{
|
||||
<div @ref="_errorElement" id="@(_id + "-error")" role="alert" tabindex="-1" class="text-danger small mt-2 text-break">@_validationError</div>
|
||||
}
|
||||
<div class="d-flex gap-2 flex-wrap mt-3">
|
||||
<button type="button" class="btn btn-outline-secondary" disabled="@_busy" @onclick="() => Cancel(session)">Cancel</button>
|
||||
<button type="button" class="btn btn-primary" disabled="@_busy" @onclick="() => Commit(session)">Save</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
@@ -80,54 +70,117 @@
|
||||
[Parameter, EditorRequired] public Func<TRow, TRow> Clone { get; set; } = default!;
|
||||
[Parameter] public Func<TRow, IReadOnlyList<TRow>, int?, string?>? Validate { get; set; }
|
||||
|
||||
private string _styleDelay => $"animation-delay:{AnimationDelay}";
|
||||
private bool _modalOpen;
|
||||
private int? _editIndex;
|
||||
private TRow? _working;
|
||||
private sealed class RowAnchor(TRow row, int index)
|
||||
{
|
||||
public TRow Row { get; } = row;
|
||||
public int Index { get; set; } = index;
|
||||
public ElementReference EditButton;
|
||||
}
|
||||
private sealed record EditSession(long Generation, List<TRow> Items, EventCallback Changed,
|
||||
RowAnchor? Original, TRow Working, RenderFragment<TRow> Editor,
|
||||
Func<TRow, IReadOnlyList<TRow>, int?, string?>? Validate, string Caption);
|
||||
private readonly string _id = "collection-" + Guid.NewGuid().ToString("N");
|
||||
private readonly List<RowAnchor> _anchors = [];
|
||||
private List<TRow>? _lastItems;
|
||||
private EventCallback _lastChanged;
|
||||
private EditSession? _session;
|
||||
private string? _validationError;
|
||||
private long _generation;
|
||||
private bool _busy, _disposed;
|
||||
private ElementReference _addButton, _editorTitle, _errorElement;
|
||||
private (long Generation, string Kind, RowAnchor? Anchor)? _focus;
|
||||
private string _styleDelay => $"animation-delay:{AnimationDelay}";
|
||||
|
||||
private void Add()
|
||||
protected override void OnParametersSet()
|
||||
{
|
||||
_editIndex = null;
|
||||
_working = NewRow();
|
||||
_validationError = null;
|
||||
_modalOpen = true;
|
||||
if (!ReferenceEquals(_lastItems, Items) || !_lastChanged.Equals(ItemsChanged))
|
||||
{
|
||||
_lastItems = Items; _lastChanged = ItemsChanged; ++_generation;
|
||||
_session = null; _busy = false; _validationError = null; _focus = null; _anchors.Clear();
|
||||
}
|
||||
else _anchors.RemoveAll(a => IndexOf(Items, a) < 0);
|
||||
}
|
||||
|
||||
private void Edit(int index)
|
||||
private static bool SameRow(TRow a, TRow b) => typeof(TRow).IsValueType
|
||||
? EqualityComparer<TRow>.Default.Equals(a, b) : ReferenceEquals(a, b);
|
||||
private RowAnchor AnchorFor(TRow row, int index)
|
||||
{
|
||||
_editIndex = index;
|
||||
_working = Clone(Items[index]);
|
||||
_validationError = null;
|
||||
_modalOpen = true;
|
||||
var anchor = _anchors.FirstOrDefault(a => SameRow(a.Row, row) && (!typeof(TRow).IsValueType || a.Index == index));
|
||||
if (anchor is null) { anchor = new(row, index); _anchors.Add(anchor); }
|
||||
anchor.Index = index;
|
||||
return anchor;
|
||||
}
|
||||
private static int IndexOf(List<TRow> items, RowAnchor anchor) => typeof(TRow).IsValueType
|
||||
? (anchor.Index < items.Count && SameRow(items[anchor.Index], anchor.Row) ? anchor.Index : -1)
|
||||
: items.FindIndex(row => SameRow(row, anchor.Row));
|
||||
private bool Current(long generation, List<TRow> items, EventCallback changed) =>
|
||||
!_disposed && generation == _generation && ReferenceEquals(Items, items) && ItemsChanged.Equals(changed);
|
||||
private bool Current(EditSession session) => ReferenceEquals(_session, session) && Current(session.Generation, session.Items, session.Changed);
|
||||
|
||||
private async Task Delete(int index)
|
||||
private void Add(long generation, List<TRow> items, EventCallback changed)
|
||||
{
|
||||
Items.RemoveAt(index);
|
||||
await ItemsChanged.InvokeAsync();
|
||||
if (!Current(generation, items, changed) || _busy || _session is not null) return;
|
||||
Open(items, changed, null, NewRow());
|
||||
}
|
||||
|
||||
private void Cancel()
|
||||
private void Edit(long generation, List<TRow> items, EventCallback changed, RowAnchor original)
|
||||
{
|
||||
_modalOpen = false;
|
||||
_working = default;
|
||||
_editIndex = null;
|
||||
_validationError = null;
|
||||
if (!Current(generation, items, changed) || _busy || _session is not null || IndexOf(items, original) < 0) return;
|
||||
Open(items, changed, original, Clone(original.Row));
|
||||
}
|
||||
|
||||
private async Task Commit()
|
||||
private void Open(List<TRow> items, EventCallback changed, RowAnchor? original, TRow working)
|
||||
{
|
||||
if (_working is null) return;
|
||||
_validationError = Validate?.Invoke(_working, Items, _editIndex);
|
||||
if (_validationError is not null) return;
|
||||
|
||||
if (_editIndex is int i) Items[i] = _working;
|
||||
else Items.Add(_working);
|
||||
|
||||
_modalOpen = false;
|
||||
_working = default;
|
||||
_editIndex = null;
|
||||
await ItemsChanged.InvokeAsync();
|
||||
if (working is null) return;
|
||||
++_generation;
|
||||
_session = new(_generation, items, changed, original, working, EditTemplate, Validate, $"{(original is null ? "Add" : "Edit")} {ItemNoun}");
|
||||
_validationError = null; _focus = (_generation, "heading", null);
|
||||
}
|
||||
private void Cancel(EditSession session)
|
||||
{
|
||||
if (!Current(session) || _busy) return;
|
||||
_session = null; _validationError = null; ++_generation;
|
||||
_focus = (_generation, "return", session.Original);
|
||||
}
|
||||
private async Task Commit(EditSession session)
|
||||
{
|
||||
if (!Current(session) || _busy) return;
|
||||
int? index = session.Original is null ? null : IndexOf(session.Items, session.Original);
|
||||
_validationError = index is < 0 ? "This row is no longer available. Cancel this draft and review the current rows."
|
||||
: session.Validate?.Invoke(session.Working, session.Items, index);
|
||||
if (_validationError is not null) { _focus = (_generation, "error", null); return; }
|
||||
if (index is int i) session.Items[i] = session.Working;
|
||||
else session.Items.Add(session.Working);
|
||||
_session = null; ++_generation;
|
||||
_focus = (_generation, "return", index is int position ? AnchorFor(session.Working, position) : null);
|
||||
await Notify(_generation, session.Items, session.Changed);
|
||||
}
|
||||
private async Task Delete(long generation, List<TRow> items, EventCallback changed, RowAnchor original)
|
||||
{
|
||||
if (!Current(generation, items, changed) || _busy || _session is not null) return;
|
||||
var index = IndexOf(items, original); if (index < 0) return;
|
||||
items.RemoveAt(index); ++_generation; _focus = (_generation, "return", null);
|
||||
await Notify(_generation, items, changed);
|
||||
}
|
||||
private async Task Notify(long generation, List<TRow> items, EventCallback changed)
|
||||
{
|
||||
_busy = true;
|
||||
try { await changed.InvokeAsync(); }
|
||||
catch (Exception) when (!Current(generation, items, changed)) { }
|
||||
finally { if (Current(generation, items, changed)) _busy = false; }
|
||||
}
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (_focus is not { } focus || _busy) return;
|
||||
_focus = null;
|
||||
if (_disposed || focus.Generation != _generation) return;
|
||||
var target = focus.Kind switch
|
||||
{
|
||||
"heading" => _editorTitle,
|
||||
"error" => _errorElement,
|
||||
_ => focus.Anchor is { } anchor && IndexOf(Items, anchor) >= 0 ? anchor.EditButton : _addButton
|
||||
};
|
||||
try { await target.FocusAsync(); }
|
||||
catch (Microsoft.JSInterop.JSDisconnectedException) { }
|
||||
catch (InvalidOperationException) { } // Static rendering has no focus interop.
|
||||
}
|
||||
public void Dispose() { _disposed = true; ++_generation; _session = null; _focus = null; }
|
||||
}
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
var savedAction = OnSaved;
|
||||
var closeAction = VisibleChanged;
|
||||
}
|
||||
<ModalFrame Visible="@Visible" Title="@(_isNew ? "New driver" : "Configure driver")" Target="@presentation" Busy="@Pending" DialogClass="modal-xl" OnClose="() => ClosePresentation(presentation, closeAction)">
|
||||
<ModalFrame Visible="@Visible" Title="@(_isNew ? "New driver" : "Configure driver")" Target="@presentation" Busy="@Pending" DialogClass="modal-xl modal-dialog-scrollable" OnClose="() => ClosePresentation(presentation, closeAction)">
|
||||
<TitleContent>@(_isNew ? "New driver" : "Configure driver") · <span class="mono">@_driverType</span></TitleContent>
|
||||
<ChildContent>
|
||||
<fieldset disabled="@(Pending || _loading)" @key="_openGeneration">
|
||||
|
||||
Reference in New Issue
Block a user