feat: grids, radar and document lines (#28)

Lists need one component for the whole app (SPEC §10.6-§10.7): a list
query model that turns search, filters, sort and paging into SQL or runs
over rows in memory, a KhGrid that pages 100,000 rows while scrolling
(or in pages) with sort, selection, keyboard, inline edit, totals, row
menu, saved column layout and CSV/XLSX export, the KhRadar around it
with search, quick filters, a filter builder and saved filters, a radar
window for F4 lookups, and KhLinesGrid for document lines typed from the
keyboard.

Checked in the browser on the component catalog and the sample document.
Fixed on the way: the grid height lost to the flex basis, a new search
kept the old scroll position, Save of a filter needed a second click,
page shortcuts (Ctrl+Canc) reached the record behind a modal window,
Escape did nothing once focus left a dialog, totals did not follow an
inline edit, and amounts exported as 0 instead of 0,00.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 18:47:38 +01:00
1 parent d0f9d0e651
commit 75d60ee4c4
18 files changed
+3776 -28

No files matched your search

@@ -153,6 +153,26 @@ else
</KhDrawer>
break;
case "griglie":
<div class="kh-catalog">
<KhRadar TItem="SampleCustomer" Id="catalog.customers" Columns="CustomerColumns" Source="CatalogSamples.CustomersAsync" KeyOf="c => c.Id"
QuickFilters="CustomerQuickFilters" RowClass="CustomerRowClass" RowMenu="CustomerMenu" OnOpen="OpenCustomer"
ExportName="clienti-di-prova" Label="@T["Pages.Catalog.Grids.Radar"]">
<Toolbar>
<div>
<h2 class="kh-h2">@T["Pages.Catalog.Grids.Radar"]</h2>
<p class="muted">@T["Pages.Catalog.Grids.RadarIntro"]</p>
</div>
</Toolbar>
</KhRadar>
<KhPanel Title="@T["Pages.Catalog.Grids.Pager"]" Icon="grid">
<p class="muted kh-pad">@T["Pages.Catalog.Grids.PagerIntro"]</p>
<KhGrid TItem="SampleCustomer" Columns="PagedColumns" Source="CatalogSamples.CustomersAsync" Virtual="false" PageSize="10" KeyOf="c => c.Id"
Filters="VipOnly" Label="@T["Pages.Catalog.Grids.Pager"]" />
</KhPanel>
</div>
break;
case "toolbar":
<div class="kh-catalog">
<p class="muted">@T["Pages.Catalog.Toolbar.Intro"]</p>
@@ -202,6 +222,7 @@ else
new("pannelli", "layers", T["Pages.Catalog.Sections.Panels"]),
new("navigazione", "columns", T["Pages.Catalog.Sections.Navigation"]),
new("finestre", "layout", T["Pages.Catalog.Sections.Windows"]),
new("griglie", "list", T["Pages.Catalog.Sections.Grids"]),
new("toolbar", "settings", T["Pages.Catalog.Sections.Toolbar"]),
];
@@ -311,5 +332,82 @@ else
private static string KindKey(ToastKind kind) => "Pages.Catalog.Windows.Toast." + kind;
private static readonly IReadOnlyList<ListFilter> VipOnly = [ListFilter.Of("vip", ListFilterOperator.Equals, "true")];
private IReadOnlyList<KhOption<string>> KindChoices =>
[
new("company", T["Pages.Catalog.Grids.KindCompany"]),
new("person", T["Pages.Catalog.Grids.KindPerson"]),
new("public", T["Pages.Catalog.Grids.KindPublic"]),
];
private IReadOnlyList<KhOption<string>> StateChoices =>
[
new("active", T["Pages.Catalog.Grids.StateActive"]),
new("blocked", T["Pages.Catalog.Grids.StateBlocked"]),
new("disabled", T["Pages.Catalog.Grids.StateDisabled"]),
];
private IReadOnlyList<KhColumn<SampleCustomer>> CustomerColumns =>
[
new("code", T["Pages.Catalog.Grids.Code"], c => c.Code) { Kind = KhColumnKind.Code, Width = "96px" },
new("name", T["Pages.Catalog.Grids.Name"], c => c.Name),
new("city", T["Pages.Catalog.Grids.City"], c => c.City),
new("province", T["Pages.Catalog.Grids.Province"], c => c.Province) { Width = "60px" },
new("agent", T["Pages.Catalog.Grids.Agent"], c => c.Agent),
new("kind", T["Pages.Catalog.Grids.Kind"], c => KindChoices.First(k => k.Value == c.Kind).Text) { Choices = KindChoices, Visible = false },
new("revenue", T["Pages.Catalog.Grids.Revenue"], c => c.Revenue) { Kind = KhColumnKind.Money, Total = true },
new("overdue", T["Pages.Catalog.Grids.Overdue"], c => c.Overdue) { Kind = KhColumnKind.Money, Total = true, CellClass = c => c.Overdue > 0 ? "neg" : null },
new("since", T["Pages.Catalog.Grids.Since"], c => c.Since) { Kind = KhColumnKind.Date, Visible = false },
new("vip", T["Pages.Catalog.Grids.Vip"], c => c.Vip) { Kind = KhColumnKind.Bool, Visible = false },
new("state", T["Pages.Catalog.Grids.State"], c => StateChoices.First(k => k.Value == c.State).Text)
{
Choices = StateChoices,
Template = c => @<KhBadge Tone="@(c.State switch { "active" => KhTone.Ok, "blocked" => KhTone.Error, _ => KhTone.Neutral })">@StateChoices.First(k => k.Value == c.State).Text</KhBadge>,
},
];
private IReadOnlyList<KhColumn<SampleCustomer>> PagedColumns =>
[
new("code", T["Pages.Catalog.Grids.Code"], c => c.Code) { Kind = KhColumnKind.Code, Width = "96px" },
new("name", T["Pages.Catalog.Grids.Name"], c => c.Name),
new("revenue", T["Pages.Catalog.Grids.Revenue"], c => c.Revenue) { Kind = KhColumnKind.Money, Total = true },
new("credit", T["Pages.Catalog.Grids.Credit"], c => c.Credit) { Kind = KhColumnKind.Money, Total = true, Edit = EditCreditAsync },
];
private IReadOnlyList<KhQuickFilter> CustomerQuickFilters =>
[
new("kind", T["Pages.Catalog.Grids.Kind"], [new(string.Empty, T["Pages.Catalog.Grids.All"]), .. KindChoices]),
new("agent", T["Pages.Catalog.Grids.Agent"], [new(string.Empty, T["Pages.Catalog.Grids.All"]), new("M. Verdi", "M. Verdi"), new("L. Neri", "L. Neri"), new("A. Bianchi", "A. Bianchi")]),
new("state", T["Pages.Catalog.Grids.State"], [new(string.Empty, T["Pages.Catalog.Grids.All"]), .. StateChoices]),
];
private static string? CustomerRowClass(SampleCustomer customer) => customer.State == "blocked" ? "hl" : null;
private IEnumerable<ToolbarItem> CustomerMenu(SampleCustomer customer)
{
yield return new ToolbarItem("open", "external", T["Pages.Catalog.Grids.OpenCard"]) { OnClick = () => { OpenCustomer(customer); return Task.CompletedTask; } };
yield return new ToolbarItem("copy", "copy", T["Pages.Catalog.Grids.Duplicate"]) { OnClick = () => Clicked("Pages.Catalog.Grids.Duplicate") };
yield return new ToolbarItem("block", "lock", T["Pages.Catalog.Grids.Block"]) { Danger = true, Disabled = customer.State == "blocked", OnClick = () => Clicked("Pages.Catalog.Grids.Block") };
}
private void OpenCustomer(SampleCustomer customer) => Toasts.Show(ToastKind.Info, T.Format("Pages.Catalog.Grids.Opened", customer.Name));
private Task<string?> EditCreditAsync(SampleCustomer customer, string text)
{
if (!ItalianNumber.TryParse(text, out var value) || value is null)
{
return Task.FromResult<string?>(T["Ui.Validation.Number"]);
}
if (value < 0)
{
return Task.FromResult<string?>(T["Pages.Catalog.Grids.CreditInvalid"]);
}
customer.Credit = Math.Round(value.Value, 2);
return Task.FromResult<string?>(null);
}
private void ShowToast(ToastKind kind) => Toasts.Show(kind, T[KindKey(kind)], T["Pages.Catalog.Windows.ToastText"]);
}
@@ -45,29 +45,9 @@ else
@switch (ActiveTab)
{
case TabIds.Lines:
<table class="grid">
<thead>
<tr>
<th scope="col">@T["Pages.Catalog.Lines.Code"]</th>
<th scope="col">@T["Pages.Catalog.Lines.Description"]</th>
<th scope="col" class="num">@T["Pages.Catalog.Lines.Quantity"]</th>
<th scope="col" class="num">@T["Pages.Catalog.Lines.Price"]</th>
<th scope="col" class="num">@T["Pages.Catalog.Lines.Amount"]</th>
</tr>
</thead>
<tbody>
@foreach (var line in Record.Lines)
{
<tr>
<td class="code">@line.Code</td>
<td>@line.Description</td>
<td class="num">@T.Number(line.Quantity ?? 0, 2)</td>
<td class="num"><KhMoney Value="line.Price" /></td>
<td class="num"><KhMoney Value="line.Amount" /></td>
</tr>
}
</tbody>
</table>
<KhLinesGrid TLine="SampleLine" @ref="_lines" Lines="Record.Lines" Columns="LineColumns" NewLine="() => new SampleLine()" IsEmpty="l => l.IsEmpty"
IsDescriptive="l => l.Descriptive" NewDescriptive="() => new SampleLine { Descriptive = true }" DescriptionColumn="LineColumns[1]"
ReadOnly="IsReadOnly" OnChanged="MarkModified" Label="@T["Pages.Catalog.Grids.Lines"]" />
break;
case TabIds.Header:
@@ -91,10 +71,88 @@ else
}
</ChildContent>
</KhRecordLayout>
<KhRadarDialog TItem="SampleItem" Id="catalog.items" Open="_lookupLine is not null" Title="@T["Pages.Catalog.Grids.ItemRadar"]" Columns="ItemColumns"
Source="CatalogSamples.ItemsAsync" KeyOf="i => i.Code" InitialSearch="@_lookupLine?.Code" OnPick="PickItemAsync" OnClose="() => _lookupLine = null"
OnCreate="CreateItem" />
}
@code {
private int _saved;
private KhLinesGrid<SampleLine>? _lines;
private SampleLine? _lookupLine;
private TaskCompletionSource? _lookupDone;
private IReadOnlyList<KhLineColumn<SampleLine>>? _lineColumns;
private IReadOnlyList<KhLineColumn<SampleLine>> LineColumns => _lineColumns ??=
[
new("code", T["Pages.Catalog.Grids.Item"], l => l.Code) { Kind = KhColumnKind.Code, Width = "120px", SetText = SetCode, Lookup = LookupAsync },
new("description", T["Pages.Catalog.Grids.Description"], l => l.Description) { SetText = (l, v) => l.Description = v ?? string.Empty },
new("unit", T["Pages.Catalog.Grids.Unit"], l => l.Unit) { Width = "56px", SetText = (l, v) => l.Unit = v?.ToUpperInvariant() },
new("quantity", T["Pages.Catalog.Grids.Quantity"], l => l.Quantity) { Kind = KhColumnKind.Number, Width = "96px", SetNumber = (l, v) => l.Quantity = v },
new("price", T["Pages.Catalog.Grids.Price"], l => l.Price) { Kind = KhColumnKind.Money, Width = "110px", SetNumber = (l, v) => l.Price = v },
new("discount", T["Pages.Catalog.Grids.Discount"], l => l.Discount) { Kind = KhColumnKind.Number, Width = "80px", SetNumber = (l, v) => l.Discount = v },
new("amount", T["Pages.Catalog.Grids.Amount"], l => l.Descriptive || l.IsEmpty ? null : l.Amount) { Kind = KhColumnKind.Money, Width = "120px" },
];
private IReadOnlyList<KhColumn<SampleItem>> ItemColumns =>
[
new("code", T["Pages.Catalog.Grids.Item"], i => i.Code) { Kind = KhColumnKind.Code, Width = "100px" },
new("description", T["Pages.Catalog.Grids.Description"], i => i.Description),
new("unit", T["Pages.Catalog.Grids.Unit"], i => i.Unit) { Width = "56px" },
new("stock", T["Pages.Catalog.Grids.Quantity"], i => i.Stock) { Kind = KhColumnKind.Number, Decimals = 0 },
new("price", T["Pages.Catalog.Grids.Price"], i => i.Price) { Kind = KhColumnKind.Money },
];
// Typing a known code fills description, unit and price, as a real item would.
private static void SetCode(SampleLine line, string? code)
{
line.Code = code?.ToUpperInvariant() ?? string.Empty;
if (CatalogSamples.ItemByCode(code) is { } item)
{
Fill(line, item);
}
}
private static void Fill(SampleLine line, SampleItem item)
{
line.Code = item.Code;
line.Description = item.Description;
line.Unit = item.Unit;
line.Price = item.Price;
line.Quantity ??= 1;
}
private Task LookupAsync(SampleLine line)
{
_lookupLine = line;
_lookupDone = new TaskCompletionSource();
StateHasChanged();
return _lookupDone.Task;
}
private async Task PickItemAsync(SampleItem item)
{
if (_lookupLine is { } line)
{
Fill(line, item);
_lookupLine = null;
_lookupDone?.TrySetResult();
MarkModified();
await Task.Yield();
if (_lines is not null)
{
await _lines.FocusCellAsync(line, "quantity");
}
}
}
private void CreateItem(string text)
{
Toasts.Show(ToastKind.Info, T.Format("Pages.Catalog.Grids.ItemCreated", text));
_lookupLine = null;
_lookupDone?.TrySetResult();
}
/// <inheritdoc />
public override IReadOnlyList<KhTabItem> Tabs => Record is null ? [] : StandardTabs(attachments: 2, links: 1);