From 7d06fcdc05fa891f874716665430524bb39cd2c3 Mon Sep 17 00:00:00 2001 From: Yichen Jiang Date: Sat, 19 Sep 2026 16:04:34 +0800 Subject: [PATCH] fix(settings-models): derive the add panels, lock the switch in flight, pair tabs with panels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The add card's panels are now derived from the shown mode and the modes visited since it opened, and each renders only while its mode is still offered, so a refresh that changes which modes are offered — every configurable row gone, the pi-ai namespace unmounted, a route freed after the catalog was exhausted — can neither leave the card without a panel nor keep a form whose write the Host would refuse. The catalog draft resolves to the chosen row while its namespace can take the write, else to the first addable row; closing either panel forgets it, so a later refresh cannot open a row editor unasked. Both cards report a write or an endpoint interrogation in flight, and the switch and the catalog select lock while one is, since a switch underneath it would orphan the answer or the picker it opens. The SegmentedControl takes the owner's id and pairs each tab with its panel through `aria-controls` and `aria-labelledby`; a card offering one mode carries that mode as its title instead of an orphan tabpanel, and a locked segment names its reason as hover text. `fullWidth` goes: no consumer asked for it. --- .../client/ui-primitives/README.i18n.yaml | 4 +- packages/client/ui-primitives/README.md | 2 +- packages/client/ui-primitives/README.zh.md | 2 +- .../src/SegmentedControl.module.css | 7 +- .../ui-primitives/src/SegmentedControl.tsx | 19 +- .../tests/segmented-control.client.spec.tsx | 31 +++- .../ui-settings-models/README.i18n.yaml | 4 +- packages/client/ui-settings-models/README.md | 2 +- .../client/ui-settings-models/README.zh.md | 2 +- .../src/client/CustomProviderCard.tsx | 12 +- .../src/client/ModelListEditor.tsx | 10 +- .../src/client/ModelsSection.tsx | 166 +++++++++++++----- .../src/client/ProviderEditor.tsx | 10 ++ .../ui-settings-models/src/client/locales.ts | 4 + .../tests/components.client.spec.tsx | 165 +++++++++++++++++ .../tests/model-list-catalog.client.spec.tsx | 8 +- 16 files changed, 368 insertions(+), 80 deletions(-) diff --git a/packages/client/ui-primitives/README.i18n.yaml b/packages/client/ui-primitives/README.i18n.yaml index eb89a4eb3f..f031a6b178 100644 --- a/packages/client/ui-primitives/README.i18n.yaml +++ b/packages/client/ui-primitives/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/client/ui-primitives/README.md -README.md: 61db832d764d127d8e0c050a10da384b34287316 -README.zh.md: b9c1013d8280202359682dbb59988b1824fbe8ab +README.md: adfdb1cac3136553bb1e45001089fa2fe19159f2 +README.zh.md: 85666ec13b623b180600ba3311424b97468ae348 diff --git a/packages/client/ui-primitives/README.md b/packages/client/ui-primitives/README.md index 61db832d76..adfdb1cac3 100644 --- a/packages/client/ui-primitives/README.md +++ b/packages/client/ui-primitives/README.md @@ -38,7 +38,7 @@ Check this table before writing a control in a feature package. A plugin cannot |---|---| | `Button` | Clickable action; `variant` selects `primary`, `ghost`, `outline`, or `toolbar`. | | `Switch` | Two-state toggle, 36×20. `label` is required, so the control cannot ship unnamed. | -| `SegmentedControl` | Tablist of two or more equal-width segments with one sliding indicator, for switching a card or panel between a few modes; the owner holds the selection, `label` names the list, and a segment may be `disabled` with a `title`. | +| `SegmentedControl` | Tablist of two or more equal-width segments with one sliding indicator, for switching a card or panel between a few modes; the owner holds the selection and `label` names the list. `id` seeds each tab's id (`-`) and the panel it controls (`--panel`), which the owner renders and points back at the tab with `aria-labelledby`; a segment may be `disabled` with a `title`, and `disabled` on the control locks every segment while the shown panel has work in flight. | | `Checkbox` | Labeled native checkbox with controlled state, keyboard interaction, and disabled styling; the caller supplies localized `label` text. | | `Input` | Single-line text entry for search boxes and inline forms. | | `Menu` | Dropdown of items, separators, and group labels, with nested submenus. While open, ↑/↓ (with Home and End) walk the list, Tab settles the focused row, and Escape or Shift+Tab close back to the anchor; selecting a row also returns the keyboard to the anchor unless the owner moved it itself. Only a keyboard on the anchor or inside the list is intercepted, and `autoFocus` decides solely whether opening focuses the first row. | diff --git a/packages/client/ui-primitives/README.zh.md b/packages/client/ui-primitives/README.zh.md index b9c1013d82..85666ec13b 100644 --- a/packages/client/ui-primitives/README.zh.md +++ b/packages/client/ui-primitives/README.zh.md @@ -38,7 +38,7 @@ kind: "package-library" |---|---| | `Button` | 可点击操作;`variant` 选择 `primary`、`ghost`、`outline` 或 `toolbar`。 | | `Switch` | 36×20 的双态开关。`label` 必填,控件不可能在没有名称的情况下发布。 | -| `SegmentedControl` | 两段或更多等宽分段加一个滑动指示块的 tablist,用于在几种模式间切换一张卡片或面板;选中项由调用方持有,`label` 为列表命名,分段可 `disabled` 并带 `title`。 | +| `SegmentedControl` | 两段或更多等宽分段加一个滑动指示块的 tablist,用于在几种模式间切换一张卡片或面板;选中项由调用方持有,`label` 为列表命名。`id` 派生每个 tab 的 id(`-`)及其控制的面板 id(`--panel`),面板由调用方渲染并用 `aria-labelledby` 指回 tab;分段可 `disabled` 并带 `title`,控件级 `disabled` 在当前面板有进行中的操作时锁住全部分段。 | | `Checkbox` | 带标签的原生复选框,支持受控状态、键盘交互和禁用样式;调用方提供本地化的 `label` 文本。 | | `Input` | 单行文本输入,用于搜索框与行内表单。 | | `Menu` | 由条目、分隔线与分组标题构成的下拉菜单,支持嵌套子菜单。打开期间 `↑`/`↓`(以及 Home、End)在列表中走位,Tab 选定聚焦行,Escape 或 Shift+Tab 关闭并把焦点还给锚点;选定一行同样把键盘还给锚点——除非拥有者自己移动了焦点。只拦截位于锚点或列表内的键盘,`autoFocus` 仅决定打开时是否聚焦首行。 | diff --git a/packages/client/ui-primitives/src/SegmentedControl.module.css b/packages/client/ui-primitives/src/SegmentedControl.module.css index 4795c8f0e6..a35b25edd4 100644 --- a/packages/client/ui-primitives/src/SegmentedControl.module.css +++ b/packages/client/ui-primitives/src/SegmentedControl.module.css @@ -4,8 +4,7 @@ * the soft elevation shadow (border: 0), the way every elevated surface draws * its edge, so the elevation gate keeps it off the alias-border set. * - * Segments are equal-width grid tracks — sized to the widest label when the - * control hugs its content, sharing the container when it spans it — which is + * Segments are equal-width grid tracks sized to the widest label, which is * what lets one indicator be placed arithmetically: the tablist carries the * segment count and the selected index as custom properties, and the indicator * derives its width and offset from them instead of measuring the DOM. */ @@ -20,10 +19,6 @@ background: var(--dsw-alias-interactive-bg-hover); } -.control[data-full-width='true'] { - display: grid; -} - .indicator { position: absolute; top: 3px; diff --git a/packages/client/ui-primitives/src/SegmentedControl.tsx b/packages/client/ui-primitives/src/SegmentedControl.tsx index b260f0a22f..ffbe2a819e 100644 --- a/packages/client/ui-primitives/src/SegmentedControl.tsx +++ b/packages/client/ui-primitives/src/SegmentedControl.tsx @@ -2,6 +2,9 @@ // sliding indicator, for switching a card or panel between a few modes. The // owner holds the selection; `label` is required so the tablist never ships // without an accessible name, and every segment label is owner-localized. +// Each tab is `-` and controls the panel `--panel`, the +// element the owner renders for that mode and points back at the tab with +// `aria-labelledby`. import { useEffect, useRef } from 'react' import type { CSSProperties, KeyboardEvent } from 'react' @@ -52,24 +55,27 @@ function walk( /** * Render a segmented control. + * @param props.id - the owner's base id: each tab is `-` and names + * `--panel` as the panel it controls. * @param props.value - the selected option's value; the control is fully controlled. * @param props.options - the segments in display order; at least two. * @param props.onChange - called with the value a click or a walk key asks for, * never with the value already selected. * @param props.label - localized accessible name of the tablist. - * @param props.fullWidth - stretch across the container instead of hugging the - * segment labels; the segments stay equal-width either way. + * @param props.disabled - lock every segment, typically while the shown panel + * has a write or a fetch in flight that switching would orphan. * @param props.className - extra class for layout placement. * @returns the tablist element. */ export function SegmentedControl({ - value, options, onChange, label, fullWidth = false, className, + id, value, options, onChange, label, disabled = false, className, }: { + id: string value: Value options: readonly SegmentedControlOption[] onChange: (next: Value) => void label: string - fullWidth?: boolean + disabled?: boolean // `| undefined` so a caller can forward an optional class straight through // under exactOptionalPropertyTypes (a CSS-module lookup is string|undefined). className?: string | undefined @@ -104,7 +110,6 @@ export function SegmentedControl({ ref={list} role="tablist" aria-label={label} - data-full-width={fullWidth} className={clsx(css.control, className)} style={indicator} > @@ -114,11 +119,13 @@ export function SegmentedControl({ return (