@page "/" Coffee order

Coffee order

When the value is copied

@* Default @bind: the field changes on the change event, when you leave the box. *@ @* @bind:event="oninput": the field changes on every keystroke. *@

Name: @name

Note: @note (@note.Length characters)

Select and checkbox

@* A select binds its value. @bind:after runs after the new value is stored in size. *@

@stockMessage

@* A checkbox binds its checked state to a bool. *@

Numbers and dates

@* type="number" and type="date" always use the invariant culture. *@ @* A text box bound to an int: text that does not parse is replaced by the last good value. An empty box is different: Blazor stores default(int), which is 0. *@ @* @bind:format controls how the date is written and the exact pattern it must match. *@

Your own get and set

@* @bind:get supplies the value, @bind:set receives what the user typed. The setter cleans it, and Blazor writes the cleaned value back into the box. *@

@(VoucherApplied ? "COFFEE10 applied: 10% off." : "Letters and digits only, up to 8.")

Order

@cups × @size@(oatMilk ? " with oat milk" : "") for @(string.IsNullOrWhiteSpace(name) ? "a guest" : name)

Pickup on @pickup.ToString("dddd, d MMMM yyyy", CultureInfo.InvariantCulture)

Total: @Total.ToString("0.00", CultureInfo.InvariantCulture)

Current culture: @(CultureInfo.CurrentCulture.Name == "" ? "invariant" : CultureInfo.CurrentCulture.Name)

@code { private string name = ""; private string note = ""; private CupSize size = CupSize.Medium; private bool oatMilk; private int cups = 2; private DateOnly pickup = DateOnly.FromDateTime(DateTime.Today.AddDays(1)); private string voucher = ""; private string stockMessage = "Change the size to run @bind:after."; private bool VoucherApplied => voucher == "COFFEE10"; private decimal PricePerCup => size switch { CupSize.Small => 2.80m, CupSize.Medium => 3.20m, _ => 3.60m, } + (oatMilk ? 0.40m : 0m); private decimal Total => Math.Round(cups * PricePerCup * (VoucherApplied ? 0.9m : 1m), 2); // Runs after @bind has stored the new size. It may be async. private async Task CheckStockAsync() { stockMessage = $"Checking {size} cups…"; await Task.Delay(600); stockMessage = $"{size} cups are in stock. {PricePerCup.ToString("0.00", CultureInfo.InvariantCulture)} each."; } // Called with the raw text on every keystroke. Keep letters and digits, upper-case, at most 8. private void SetVoucher(string? value) { var kept = new string((value ?? "").Where(char.IsLetterOrDigit).ToArray()).ToUpperInvariant(); voucher = kept.Length > 8 ? kept[..8] : kept; } public enum CupSize { Small, Medium, Large } }