refactor(ui): make ChoiceMenu the one flat menu, with typed selection

ChoiceMenu takes selectionMode single or multiple, or onAction alone for an action menu. The node join menu, the connections column menu and the arrange tray's add menu move onto it; MenuButton content stays for kit internals and NodeMenu. The join menu was a single-selection menu that never showed a choice, so its items become menuitem instead of menuitemradio. Items can be a function, read when the menu opens, so a node row still builds its group list only then.
This commit is contained in:
Zakk committed 2026-09-26 05:22:47 +10:00
1 parent 87778df73c
commit 1e55dc4436
6 files changed
+81 -76

No files matched your search

+1 -1
View File
@@ -527,7 +527,7 @@ test('new group validation refusal retains the dialog and its name without a suc
});
await page.goto('/#/nodes?provider=inline');
await page.getByRole('button', {name: 'Add hk-01 to a group', exact: true}).click();
await page.getByRole('menuitemradio', {name: 'New group…', exact: true}).click();
await page.getByRole('menuitem', {name: 'New group…', exact: true}).click();
const dialog = page.getByRole('dialog', {name: 'New group…', exact: true});
await dialog.getByLabel('Name', {exact: true}).fill('retained-group');
const validating = page.waitForRequest('**/api/v1/config/validate');
+3 -3
View File
@@ -4,11 +4,11 @@ test('a node joins an existing group or a new one through the name filter', asyn
await page.goto('/#/nodes?provider=inline');
await page.getByRole('button', {name: 'Add jp-01 to a group', exact: true}).click();
const menu = page.getByRole('menu');
await expect(menu.getByRole('menuitemradio', {name: /^gaming/})).toHaveCount(0);
await menu.getByRole('menuitemradio', {name: /^resilient/}).click();
await expect(menu.getByRole('menuitem', {name: /^gaming/})).toHaveCount(0);
await menu.getByRole('menuitem', {name: /^resilient/}).click();
await expect(page.locator('.rp-toast.positive')).toContainText('jp-01 added to resilient');
await page.getByRole('button', {name: 'Add sg-01 to a group', exact: true}).click();
await page.getByRole('menuitemradio', {name: 'New group…', exact: true}).click();
await page.getByRole('menuitem', {name: 'New group…', exact: true}).click();
const dialog = page.getByRole('dialog');
await dialog.getByLabel('Name').fill('backup');
await expect(dialog.getByRole('button', {name: /Selection policy/})).toContainText('Fastest on average');
+2 -20
View File
@@ -1,4 +1,3 @@
import {Menu} from 'react-aria-components';
import {
Badge,
Button,
@@ -8,8 +7,6 @@ import {
LabeledSelect,
Light,
ChoiceMenu,
MenuButton,
MenuChoice,
RuleRef,
Segmented,
TextField,
@@ -51,24 +48,9 @@ export function Connections(props: PageProps) {
{id: 'none', label: t('conn.ungrouped')}
]}
/>
<MenuButton
label={t('conn.columns')}
content={
<Menu
aria-label={t('conn.columns')}
selectionMode="multiple"
shouldCloseOnSelect={false}
selectedKeys={vm.visibleColumns}
onAction={vm.toggleColumn}
>
{vm.columns.map(column => (
<MenuChoice key={column.id} item={column} />
))}
</Menu>
}
>
<ChoiceMenu label={t('conn.columns')} selectionMode="multiple" items={vm.columns} value={vm.visibleColumns} onAction={vm.toggleColumn}>
{t('conn.columns')}
</MenuButton>
</ChoiceMenu>
{vm.filtered && (
<Button quiet onPress={vm.clear}>
{t('ui.clearFilters')}
+3 -13
View File
@@ -1,21 +1,11 @@
import {createContext, useContext, useMemo} from 'react';
import {Menu} from 'react-aria-components';
import {useT} from '../../i18n';
import {Button, DataTable, LabeledSelect, MenuButton, MenuChoice, TextField, TextTooltip, pickMenuKey, type TableColumn} from '../../ui/ui';
import {Button, ChoiceMenu, DataTable, LabeledSelect, TextField, TextTooltip, type TableColumn} from '../../ui/ui';
import Close from '../../ui/icons/Close';
import AddCircle from '../../ui/icons/AddCircle';
import SpeedFast from '../../ui/icons/SpeedFast';
import type {NodeTableView} from './useNodeTable';
function JoinOptions({row}: {row: NodeTableView['rows'][number]}) {
return (
<Menu aria-label={row.joinLabel} selectionMode="single" selectedKeys={[]} onSelectionChange={pickMenuKey(row.join)}>
{row.menu().map(item => (
<MenuChoice key={item.id} item={item} />
))}
</Menu>
);
}
// The node whose probe is running, read by the probe buttons alone, so a probe starting or ending re-renders the
// buttons on screen rather than the rows or columns.
const ProbeBusy = createContext<string | null>(null);
@@ -65,9 +55,9 @@ export function NodeTable({model: m}: {model: NodeTableView}) {
<span className="rp-chain">
{row.canProbe && <ProbeButton row={row} />}
{writable && (
<MenuButton quiet chevron={false} label={row.joinLabel} isDisabled={sourceBusy} content={<JoinOptions row={row} />}>
<ChoiceMenu quiet chevron={false} label={row.joinLabel} isDisabled={sourceBusy} items={row.menu} onAction={row.join}>
<AddCircle />
</MenuButton>
</ChoiceMenu>
)}
{row.removable && (
<Button small quiet icon isDisabled={busy} label={row.removeLabel} onPress={row.remove}>
+11 -21
View File
@@ -6,7 +6,6 @@ import {
GridList,
GridListItem,
ListLayout,
Menu,
Virtualizer,
isTextDropItem,
useDragAndDrop,
@@ -22,6 +21,7 @@ import {
Button,
buttonClass,
Check,
ChoiceMenu,
cx,
Disclosure,
Empty,
@@ -30,8 +30,6 @@ import {
Light,
Link,
Loading,
MenuButton,
MenuChoice,
ModalDialog,
Segmented,
Tag,
@@ -312,28 +310,20 @@ function Tray({m}: {m: Model}) {
{t('arrange.clear')}
</Button>
)}
<MenuButton
<ChoiceMenu
label={t('arrange.addSelected')}
isDisabled={locked || !targets.length}
content={
<Menu
aria-label={t('arrange.addSelected')}
onAction={group => {
m.place(
String(group),
chosen.map(row => row.item)
);
setSelected(new Set());
}}
>
{targets.map(group => (
<MenuChoice key={group.name} item={{id: group.name, label: group.name}} />
))}
</Menu>
}
items={targets.map(group => ({id: group.name, label: group.name}))}
onAction={group => {
m.place(
group,
chosen.map(row => row.item)
);
setSelected(new Set());
}}
>
{t('arrange.addSelected')}
</MenuButton>
</ChoiceMenu>
</div>
</div>
);
+61 -18
View File
@@ -271,11 +271,49 @@ function SubmenuMenu({label, submenus}: {label: string; submenus: ChoiceSubmenu[
);
}
// The flat menu's items, read only once it opens: a table row's choices can cost a pass over the whole table.
type Items = Item[] | (() => Item[]);
function FlatMenu({
label,
items,
selectionMode,
value,
onChange,
onAction
}: {
label: string;
items: Items;
selectionMode: 'none' | 'single' | 'multiple';
value?: string | string[];
onChange?: (key: string) => void;
onAction?: (key: string) => void;
}) {
return (
<Menu
aria-label={label}
selectionMode={selectionMode}
selectedKeys={value == null ? undefined : typeof value === 'string' ? [value] : value}
onSelectionChange={onChange && pickMenuKey(onChange)}
// Several choices are picked one after another, so the menu stays open, as S2's does.
shouldCloseOnSelect={selectionMode === 'multiple' ? false : undefined}
onAction={onAction && (key => onAction(String(key)))}
>
{(typeof items === 'function' ? items() : items).map(item => (
<MenuChoice key={item.id} item={item} />
))}
</Menu>
);
}
type NotFlat = {items?: never; selectionMode?: never; value?: never; onChange?: never};
// A menu of choices: flat, in titled sections each with its own selection, or as rows that each open a submenu of
// sections. `onAction` hears every pick, including one of the already chosen item, for menus where picking it again
// sections. A flat menu marks one choice (`selectionMode` single, the default) or several (`multiple`, each pick
// toggling one through `onAction`); with neither `value` nor `selectionMode` it is an action menu that only runs
// `onAction`. `onAction` hears every pick, including one of the already chosen item, for menus where picking it again
// clears it.
export function ChoiceMenu({
items,
selectionMode,
value,
onChange,
sections,
@@ -284,12 +322,20 @@ export function ChoiceMenu({
...props
}: Omit<MenuButtonProps, 'content'> &
(
| {items: Item[]; value: string; onChange: (key: string) => void; sections?: never; submenus?: never}
| {sections: ChoiceSection[]; items?: never; value?: never; onChange?: never; submenus?: never}
| {submenus: ChoiceSubmenu[]; items?: never; value?: never; onChange?: never; sections?: never}
) & {
onAction?: (key: string) => void;
}) {
| {
items: Items;
selectionMode?: 'single';
value: string;
onChange: (key: string) => void;
onAction?: (key: string) => void;
sections?: never;
submenus?: never;
}
| {items: Items; selectionMode: 'multiple'; value: string[]; onAction: (key: string) => void; onChange?: never; sections?: never; submenus?: never}
| {items: Items; onAction: (key: string) => void; selectionMode?: never; value?: never; onChange?: never; sections?: never; submenus?: never}
| (NotFlat & {sections: ChoiceSection[]; onAction?: (key: string) => void; submenus?: never})
| (NotFlat & {submenus: ChoiceSubmenu[]; onAction?: never; sections?: never})
)) {
return (
<MenuButton
{...props}
@@ -299,17 +345,14 @@ export function ChoiceMenu({
) : sections ? (
<SectionMenu label={props.label} sections={sections} onAction={onAction} />
) : (
<Menu
aria-label={props.label}
selectionMode="single"
selectedKeys={[value]}
onSelectionChange={pickMenuKey(onChange)}
onAction={onAction && (key => onAction(String(key)))}
>
{items.map(item => (
<MenuChoice key={item.id} item={item} />
))}
</Menu>
<FlatMenu
label={props.label}
items={items}
selectionMode={selectionMode ?? (value == null ? 'none' : 'single')}
value={value}
onChange={onChange}
onAction={onAction}
/>
)
}
/>