Skip to content

Undo, redo & long lists

A stock list of five hundred lines, shown twenty at a time, with Undo and Redo beside the Save button. Paged rather than virtualised, so every line stays reachable: the error summary, focus-the-first-error and the pager's own "show the first" turn to the page a problem is on.

Change a few quantities, remove a line, move one - then press Undo, again and again: each answer comes back with what it set off, a removed line as itself. Clear a line's product on page 7, go back to page 1 and press Save: the summary lists it, and its link turns to that page and puts the caret in the box. Two changes to the same field within a second are one undo.
Live form
Stock

Lines500

500 lines

View source
@page "/undo-long-lists"

<BormView State="_state" ShowUndo="true" ShowErrorSummary="true" OnValidSubmit="HandleSubmit" SubmitText="Save stock" />

@code {
    private readonly Invoice _model = new();
    private BormState _state = default!;

    protected override void OnInitialized()
    {
        for (var i = 1; i <= 500; i++)
            _model.Lines.Add(new InvoiceLine { Description = $"Item {i:000}", Quantity = 1 + i % 9, UnitPrice = 1 });

        var schema = BormBuilder.For<Invoice>()
            .Title("Stock")
            .Array(x => x.Lines, line => line
                    .Field(l => l.Description, f => f.Label("Product").Required())
                    .Field(l => l.Quantity, f => f.Range(0, 9999)),
                f => f.PageSize(20).Attr("itemNoun", "line"))
            .Computed(x => x.Total, m => m.Lines.Sum(l => (decimal)l.Quantity), dependsOn: [nameof(Invoice.Lines)],
                f => f.Label("Items in stock"))   // the model is an invoice; this list is a stock
            .Build();

        _state = new BormState(schema, new BormModelDataAccessor(_model));
    }

    public void Dispose() => _state.Dispose();
}

What one undo takes back

  • Everything one answer set off: the value, a field a condition then hid and cleared, a sibling a change handler filled in. They come back together, and the handler is not run again. Computed values are not recorded; they follow.
  • Adding, removing, moving, copying and clearing rows are changes like any other, and undoing a removal puts back the same row - with whatever it held - rather than a copy.
  • Two writes to one field within a second are one change, so a field that writes as the user types is not undone a character at a time; the tidying a field does as it is left (Trim(), a mask) belongs to that answer.
  • A value put back is judged again, so an error the user was shown comes back with the value that caused it. Reset() and restoring a draft start the history again.

The buttons are named for the field they act on ("Undo the change to Product"), keep the focus so they can be pressed again, and announce what they did. Ctrl+Z inside a text box still undoes the typing in that box, as the browser always has. For buttons of your own: state.UndoAsync(), RedoAsync(), CanUndo, CanRedo and NextUndoPath; HistoryLimit is how many changes are kept - none by default, 100 once ShowUndo is on.

Paged, not virtualised

PageSize(20) shows twenty rows at a time, with Previous and Next, a page picker and the rows on screen ("21–40 of 500"), and says how many rows on other pages need attention, with a button to the first of them. Every way of reaching a row still reaches it: the error summary's links and focus-the-first-error turn to the row's page first, and adding, copying or moving a row turns to the page it lands on. A virtualised list would leave the rows out of view out of the page, where neither the keyboard, the error summary nor a screen reader could reach them; a page is always whole. x-pageSize carries it through JSON Schema.

An unhandled error has occurred. Reload 🗙