diff --git a/gallery/Gallery.Client/Pages/Segmented.razor b/gallery/Gallery.Client/Pages/Segmented.razor index 8cbd422..630a303 100644 --- a/gallery/Gallery.Client/Pages/Segmented.razor +++ b/gallery/Gallery.Client/Pages/Segmented.razor @@ -92,7 +92,7 @@
-

The bound value reaches .NET when a choice changes; the arrow keys skip the disabled Week. A host that sets the value moves the selection; the ids stay.

+

The bound value reaches .NET when a choice changes; the arrow keys skip the disabled Week, and at either end they wrap or stop as the browser does for any radios. A host that sets the value moves the selection; the ids stay.

@@ -139,7 +139,7 @@
A div with role="radiogroup", or in a field the field takes the role. For each option a label around a native radio, an optional icon, the label and an optional count. - The native radio group: Tab enters on the chosen item, or the first when none is chosen; the arrow keys move and choose, skipping disabled items. Required puts required on the first enabled radio and aria-required on the group. + The native radio group: Tab enters on the chosen item, or the first when none is chosen; the arrow keys move and choose, skipping disabled items. At the first or the last item the browser decides whether they wrap round, as it does for any radios: Chromium and Firefox wrap, WebKit stops. Required puts required on the first enabled radio and aria-required on the group. The radios post under the framework's name or yours. Where the framework gives none (WebAssembly, outside a form that asks for field names) they share one made from the group id, so they stay exclusive. Each radio's id is the group id with __choice- and its value, so it survives a reorder. Static. Posts as a form field; showing another part of the page on a change needs an interactive mode, or a form that is sent. It never navigates. fl-segmented with --s, --l, --icon, --block; parts fl-segmented__item, __control, __label, __count. diff --git a/tests/FluentCMS.Components.Browser/Forms/SegmentedTests.cs b/tests/FluentCMS.Components.Browser/Forms/SegmentedTests.cs index 79d6f68..90c33f9 100644 --- a/tests/FluentCMS.Components.Browser/Forms/SegmentedTests.cs +++ b/tests/FluentCMS.Components.Browser/Forms/SegmentedTests.cs @@ -50,23 +50,89 @@ public async Task TabEntersOnTheChoiceAndTheArrowsSkipDisabledChoices(RenderMode await keys.AssertFocusAsync("#segmented-bound input[value='day']:checked"); await keys.AssertFocusNameAsync("Day"); + async Task ExpectChoiceAsync(string value) + { + await keys.AssertFocusAsync($"#segmented-bound input[value='{value}']:checked"); + await Expect(page.Locator("#segmented-bound input:checked")).ToHaveCountAsync(1); + await Expect(output).ToHaveTextAsync(mode.IsInteractive() ? value : "day"); + } + await keys.PressAsync("ArrowDown"); - await keys.AssertFocusAsync("#segmented-bound input[value='month']:checked"); await Expect(page.Locator("#segmented-bound input[value='week']")).ToBeDisabledAsync(); - await Expect(output).ToHaveTextAsync(mode.IsInteractive() ? "month" : "day"); + await ExpectChoiceAsync("month"); await keys.PressAsync("ArrowDown"); - await keys.AssertFocusAsync("#segmented-bound input[value='year']:checked"); + await ExpectChoiceAsync("year"); + + // At either end the arrow keys do what the same browser does for plain radios: some wrap, some stop. + (string afterLast, string afterFirst) = await PlainRadioBoundariesAsync(page); await keys.PressAsync("ArrowDown"); - await keys.AssertFocusAsync("#segmented-bound input[value='day']:checked"); + await ExpectChoiceAsync(afterLast); + if (afterLast == "year") + { + await keys.PressAsync("ArrowUp"); + await ExpectChoiceAsync("month"); + await keys.PressAsync("ArrowUp"); + await ExpectChoiceAsync("day"); + } await keys.PressAsync("ArrowUp"); - await keys.AssertFocusAsync("#segmented-bound input[value='year']:checked"); - await Expect(output).ToHaveTextAsync(mode.IsInteractive() ? "year" : "day"); - await Expect(page.Locator("#segmented-bound input:checked")).ToHaveCountAsync(1); + await ExpectChoiceAsync(afterFirst); await keys.TabAsync(); await keys.AssertFocusAsync("#segmented-bound-month"); } + // The same choices as the bound example, as plain HTML radios in the same document and direction, outside + // the Blazor root. Returns the value the browser moves to with ArrowDown from the last enabled radio and with + // ArrowUp from the first, and checks that it skips the disabled one and keeps exactly one checked. + private static readonly string[] EnabledChoices = ["day", "month", "year"]; + private static readonly string[] Ends = ["day", "year"]; + + private static async Task<(string AfterLast, string AfterFirst)> PlainRadioBoundariesAsync(IPage page) + { + IElementHandle? previous = await page.EvaluateHandleAsync("() => document.activeElement") as IElementHandle; + await page.EvaluateAsync(""" + () => { + const form = document.createElement('form'); + form.id = 'plain-radios'; + for (const [value, disabled] of [['day', false], ['week', true], ['month', false], ['year', false]]) { + const input = Object.assign(document.createElement('input'), { type: 'radio', name: 'plain-period', value, disabled }); + form.append(input); + } + document.body.append(form); + } + """); + async Task FromAsync(string value, string key) + { + await page.Locator($"#plain-radios input[value='{value}']").EvaluateAsync("el => { el.checked = true; el.focus(); }"); + await page.Keyboard.PressAsync(key); + string[] state = await page.EvaluateAsync(""" + () => [document.activeElement.value, ...[...document.querySelectorAll('#plain-radios :checked')].map(el => el.value)] + """); + Assert.Equal(2, state.Length); + Assert.Equal(state[0], state[1]); + Assert.Contains(state[0], EnabledChoices); + return state[0]; + } + + try + { + Assert.Equal("month", await FromAsync("day", "ArrowDown")); + string afterLast = await FromAsync("year", "ArrowDown"); + string afterFirst = await FromAsync("day", "ArrowUp"); + Assert.Contains(afterLast, Ends); + Assert.Contains(afterFirst, Ends); + return (afterLast, afterFirst); + } + finally + { + await page.EvaluateAsync("() => document.querySelector('#plain-radios').remove()"); + if (previous is not null) + { + await previous.EvaluateAsync("el => el.focus()"); + } + } + } + [Theory] [ModeData(RenderMode.Server, RenderMode.Wasm)] public async Task AHostChangeMovesTheSelectionAndKeepsTheIds(RenderMode mode)