Files
ersatztv/web/src/components/forms.tsx
T
timothyandClaude Opus 5 4d33462732
review-verdict/h10 Review-verdict: MERGEABLE @ 4d33462 (base: main)
PR Gates / CI image pin matches docker/ci (pull_request) Successful in 20s
Build ErsatzTV Image / Delimiter ban (release path) (pull_request) Successful in 47s
PR Gates / Docs update reminder (pull_request) Successful in 48s
Build ErsatzTV Image / CI toolchain image resolves (pull_request) Successful in 49s
PR Gates / Fix proofs (Proves trailers) (pull_request) Successful in 16s
Review verdict / Set review-verdict status (pull_request_target) Successful in 10s
PR Gates / decisions lifecycle (pull_request) Successful in 35s
PR Gates / Script lint and tests (ruff + pytest) (pull_request) Successful in 5m12s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m43s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 6m55s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Skipped
Build ErsatzTV Image / Functional E2E (curl + UI contracts) (pull_request) Successful in 5m54s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 13s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 5s
fix(721,740): align Auto-Tune proposal rows on a grid; guard AddItemsDialog's async searches
#721 — the Auto-Tune proposal list was hard to scan because the channel number started
wherever the preceding text ended: the row was a flex line whose first child was a
`<Checkbox label={displayName}>`, so `minWidth: 42` reserved width for the number without
being able to align it. The same name then rendered a second time in the detail column, and
the group header above the rows had the same duplicate (`Select all TV Shows` immediately
followed by `TV Shows`).

The row is now a grid. What makes the column line up is that everything to the LEFT of the
number is fixed-width — a label-less Checkbox renders one fixed-size control — and the number
track is fixed rather than content-sized, because each row is its own grid container and an
`auto`/`max-content` track would resolve per row.

No fixed track is a true upper bound on the value: `AutoTuneNumberAllocator.Allocate` emits
`int.ToString()` from a `StartingNumber` that `PreviewAutoTuneChannelsHandler` only floors at
1, and a per-proposal override is free text checked against `Channel.NumberValidator`
(`^[0-9]+(\.[0-9]{1,2})?$`) — unbounded integer part plus up to two decimals. So the cell
clips: an extreme value degrades to an ellipsis with the full value on hover instead of
overflowing into the checkbox beside it. That is a real trade — the flex box it replaces grew
instead of truncating — so the `title` fallback is part of the fix, not decoration, and is
pinned. The number is right-aligned and tabular so 2-, 3- and 4-digit numbers agree on their
last digit.

`Checkbox` gains `hideLabel`, mirroring the prop `Switch` in the same file already has: the
name lives in its own column and the control keeps `label` as its accessible name, so the
existing `getByRole('checkbox', { name })` assertions are untouched.

#740 — `AddItemsDialog` had neither of the two guards spa-conventions.md §3b mandates for
asynchronous pickers, and their triggers differ. `runSearch` is reachable twice concurrently
(a form submit fanning out over DEFAULT_SEARCH_KINDS, a kind-chip click issuing one request),
so a superseded response could settle after a newer one — repainting the list with every kind
under an active "Movies" chip, wiping a newer successful result with a superseded search's
error, or clearing `searching` while the other request was still in flight. Separately, the
dialog is unmount-reachable mid-request through Escape, a backdrop click and the header close
button, none of which consult the in-flight flag — so `submit`'s post-await writes to its own
state need the is-mounted guard even though its Add button is disabled and nothing can
supersede it. `onAdded`/`onClose` are deliberately NOT guarded: they belong to the parent,
which owns its own guarding, and dropping the list reload after a write that succeeded would
leave the collection stale.

Every clause listed below ships with a test that reddens when THAT clause alone is removed.
Verified by applying each mutation to the pristine source, asserting it matched exactly once,
reading it back, and re-running — 14/14 red:

  runSearch success guard   -> repaint test        submit catch guard    -> failed-add test
  runSearch catch guard     -> failed-search test  submit finally guard  -> failed-add test
  runSearch finally guard   -> spinner test        row grid track list   -> #721 test
  isCurrent mounted half    -> unmount test        textAlign / overflow  -> #721 test
  isCurrent seq half        -> 3 search tests      whiteSpace / title    -> #721 test
  row + group-header hideLabel -> #721 test

Two of those needed the test strengthened before they were pinned at all, and both are the
kind of green that means nothing:

- The two `submit` guards MASKED each other — removing either alone left the suite green,
  because the survivor still read the ref. That test now counts the reads rather than
  asserting merely that one of them saw `false`.
- The failed-search test asserted `queryByText('Unable to search library')`, which
  `messageFromCollectionError` only produces for a non-`Error` throw — so it was null-vs-null
  and passed with the guard removed. It matches `role="alert"` instead, verified to fail on
  its own with the other assertions in that test removed.

The unmount tests record what the component read out of the is-mounted ref because an
unmounted tree renders nothing either way and no DOM assertion can tell "the guard stopped it"
from "React discarded it"; same construction as schedules/pickers.test.tsx.

The design-system prototypes already had a label-less proposal checkbox — the SPA had drifted
from them — so the mirrored delta is the number's right-align. The prototypes keep their flex
row; converting them to the grid is a layout change to a mockup, not part of this fix.

Verification: web suite 115 files / 1206 tests green; `npm run typecheck` and `npm run lint`
clean; repo guard suite `scripts/tests` 956 passed / 2 skipped. Live-E2E not required —
`docs/e2e-local.md` scopes it to API write-path handler changes and this is pure-SPA and
read-only. No .cs files touched.

fixes #721
fixes #740

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-25 23:23:38 +02:00

342 lines
8.4 KiB
TypeScript

import type { ChangeEvent, CSSProperties, FocusEvent, MouseEvent, ReactNode } from 'react';
import { Check, ChevronDown, Minus } from 'lucide-react';
import { Spinner } from './feedback';
type ControlSize = 'sm' | 'md';
function classNames(...values: Array<string | false | null | undefined>): string {
return values.filter(Boolean).join(' ');
}
export interface ButtonProps {
children?: ReactNode;
variant?: 'primary' | 'secondary' | 'ghost' | 'danger';
size?: ControlSize;
startIcon?: ReactNode;
endIcon?: ReactNode;
disabled?: boolean;
loading?: boolean;
fullWidth?: boolean;
type?: 'button' | 'submit' | 'reset';
onClick?: (e: MouseEvent<HTMLButtonElement>) => void;
style?: CSSProperties;
title?: string;
}
export function Button({
children,
variant = 'primary',
size = 'md',
startIcon,
endIcon,
disabled = false,
loading = false,
fullWidth = false,
type = 'button',
onClick,
style,
title
}: ButtonProps) {
return (
<button
type={type}
disabled={disabled || loading}
onClick={onClick}
title={title}
className={classNames(
'ctv-button',
`ctv-button-${variant}`,
size === 'sm' && 'ctv-button-sm',
fullWidth && 'ctv-button-full'
)}
style={style}
>
{loading ? <Spinner size={14} stroke={3} /> : startIcon}
<span>{children}</span>
{!loading && endIcon}
</button>
);
}
export interface IconButtonProps {
children?: ReactNode;
size?: ControlSize;
variant?: 'ghost' | 'solid';
active?: boolean;
disabled?: boolean;
title?: string;
onClick?: (e: MouseEvent<HTMLButtonElement>) => void;
style?: CSSProperties;
}
export function IconButton({
children,
size = 'md',
variant = 'ghost',
active = false,
disabled = false,
title,
onClick,
style
}: IconButtonProps) {
return (
<button
type="button"
title={title}
aria-label={title}
disabled={disabled}
onClick={onClick}
className={classNames(
'ctv-icon-button',
size === 'sm' && 'ctv-icon-button-sm',
variant === 'solid' && 'ctv-icon-button-solid',
active && 'ctv-icon-button-active'
)}
style={style}
>
{children}
</button>
);
}
export interface InputProps {
label?: string;
value?: string;
onChange?: (e: ChangeEvent<HTMLInputElement>) => void;
placeholder?: string;
leadingIcon?: ReactNode;
trailing?: ReactNode;
error?: string | null;
disabled?: boolean;
size?: ControlSize;
type?: string;
fullWidth?: boolean;
style?: CSSProperties;
// Native <input> passthroughs — added for numeric fields (e.g. the multi-collection weight input,
// #404): min/max bound the stepper, inputMode hints the mobile keyboard, onBlur normalizes on commit.
min?: number;
max?: number;
inputMode?: 'numeric' | 'decimal' | 'text';
onBlur?: (e: FocusEvent<HTMLInputElement>) => void;
ariaLabel?: string;
}
export function Input({
label,
value,
onChange,
placeholder,
leadingIcon,
trailing,
error = null,
disabled = false,
size = 'md',
type = 'text',
fullWidth = true,
style,
min,
max,
inputMode,
onBlur,
ariaLabel
}: InputProps) {
return (
<label className={classNames('ctv-field', fullWidth && 'ctv-field-full')}>
{label && <span className="ctv-field-label">{label}</span>}
<span
className={classNames(
'ctv-field-frame',
size === 'sm' && 'ctv-field-frame-sm',
error && 'ctv-field-frame-error',
disabled && 'ctv-field-frame-disabled'
)}
style={style}
>
{leadingIcon && <span className="ctv-field-icon">{leadingIcon}</span>}
<input
className="ctv-input"
type={type}
value={value}
onChange={onChange}
onBlur={onBlur}
placeholder={placeholder}
disabled={disabled}
min={min}
max={max}
inputMode={inputMode}
aria-label={ariaLabel}
/>
{trailing && <span className="ctv-field-trailing">{trailing}</span>}
</span>
{error && <span className="ctv-field-error">{error}</span>}
</label>
);
}
export interface SelectOption {
value: string;
label: string;
}
export interface SelectProps {
label?: string;
value?: string;
onChange?: (e: ChangeEvent<HTMLSelectElement>) => void;
options?: Array<string | SelectOption>;
disabled?: boolean;
size?: ControlSize;
fullWidth?: boolean;
style?: CSSProperties;
// Associates an out-of-band hint (e.g. a truncation message rendered as a sibling) with this
// control for screen readers, matching how `error`/`help` text is otherwise co-located with a
// field. Pass the id of the element carrying the hint text.
ariaDescribedBy?: string;
}
export function Select({
label,
value,
onChange,
options = [],
disabled = false,
size = 'md',
fullWidth = true,
style,
ariaDescribedBy
}: SelectProps) {
const normalizedOptions = options.map((option) =>
typeof option === 'string' ? { value: option, label: option } : option
);
return (
<label className={classNames('ctv-field', fullWidth && 'ctv-field-full')}>
{label && <span className="ctv-field-label">{label}</span>}
<span
className={classNames(
'ctv-field-frame',
'ctv-select-frame',
size === 'sm' && 'ctv-field-frame-sm',
disabled && 'ctv-field-frame-disabled'
)}
style={style}
>
<select
aria-describedby={ariaDescribedBy}
className="ctv-select"
value={value}
onChange={onChange}
disabled={disabled}
>
{normalizedOptions.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
<ChevronDown className="ctv-select-chevron" aria-hidden="true" size={14} />
</span>
</label>
);
}
export interface SwitchProps {
checked?: boolean;
onChange?: (next: boolean) => void;
label?: string;
// When true, `label` still becomes the accessible name (aria-label) but is not rendered as
// visible text — for callers that already show the same caption elsewhere (e.g. a settings
// Row label) and would otherwise duplicate it.
hideLabel?: boolean;
disabled?: boolean;
size?: ControlSize;
style?: CSSProperties;
}
export function Switch({
checked = false,
onChange,
label,
hideLabel = false,
disabled = false,
size = 'md',
style
}: SwitchProps) {
return (
<span
className={classNames(
'ctv-switch',
size === 'sm' && 'ctv-switch-sm',
disabled && 'ctv-switch-disabled'
)}
style={style}
>
<button
type="button"
role="switch"
aria-label={label}
aria-checked={checked}
aria-disabled={disabled}
className="ctv-switch-control"
onClick={() => {
if (!disabled) {
onChange?.(!checked);
}
}}
>
<span className="ctv-switch-knob" />
</button>
{label && !hideLabel && <span>{label}</span>}
</span>
);
}
export interface CheckboxProps {
checked?: boolean;
onChange?: (next: boolean) => void;
label?: string;
/**
* Keep `label` as the accessible name but do not render it visibly — same contract as `Switch`
* above. A caller that renders the name itself (a grid row with its own name column, #721) needs
* the control to stay named without emitting the text a second time.
*/
hideLabel?: boolean;
indeterminate?: boolean;
disabled?: boolean;
style?: CSSProperties;
}
export function Checkbox({
checked = false,
onChange,
label,
hideLabel = false,
indeterminate = false,
disabled = false,
style
}: CheckboxProps) {
return (
<span className={classNames('ctv-checkbox', disabled && 'ctv-checkbox-disabled')} style={style}>
<button
type="button"
role="checkbox"
aria-label={label}
aria-checked={indeterminate ? 'mixed' : checked}
aria-disabled={disabled}
className="ctv-checkbox-control"
onClick={() => {
if (!disabled) {
onChange?.(!checked);
}
}}
>
{indeterminate ? (
<Minus aria-hidden="true" size={11} strokeWidth={3} />
) : checked ? (
<Check aria-hidden="true" size={12} strokeWidth={3} />
) : null}
</button>
{label && !hideLabel && <span>{label}</span>}
</span>
);
}