Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gallery/Gallery.Client/Pages/Segmented.razor
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
</Section>

<Section Title="Binding" Id="binding">
<p>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.</p>
<p>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.</p>
<Demo Column Narrow>
<FlField Label="Period">
<FlSegmented id="segmented-bound" @bind-Value="_bound" Options="BoundPeriods" name="bound" />
Expand Down Expand Up @@ -139,7 +139,7 @@
<Section Title="Spec">
<SpecTable>
<SpecRow Name="Element">A <code>div</code> with <code>role="radiogroup"</code>, or in a field the field takes the role. For each option a <code>label</code> around a native radio, an optional icon, the label and an optional count.</SpecRow>
<SpecRow Name="ARIA and keys">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 <code>required</code> on the first enabled radio and <code>aria-required</code> on the group.</SpecRow>
<SpecRow Name="ARIA and keys">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 <code>required</code> on the first enabled radio and <code>aria-required</code> on the group.</SpecRow>
<SpecRow Name="Names and ids">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 <code>__choice-</code> and its value, so it survives a reorder.</SpecRow>
<SpecRow Name="Level">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.</SpecRow>
<SpecRow Name="Classes"><code>fl-segmented</code> with <code>--s</code>, <code>--l</code>, <code>--icon</code>, <code>--block</code>; parts <code>fl-segmented__item</code>, <code>__control</code>, <code>__label</code>, <code>__count</code>.</SpecRow>
Expand Down
80 changes: 73 additions & 7 deletions tests/FluentCMS.Components.Browser/Forms/SegmentedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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<string[]>("""
() => [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)
Expand Down