Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/ui/text-field/TextField.tsx
iris 94969b552e swarm ui: the create-agent name field says why a name is refused, beside the field
A name with an uppercase letter greyed out next with only the footer to say why. The text field now takes an error: it marks the field invalid, draws the reason under it and points aria-describedby at it. The create form passes it once something is typed, and nameProblem names the cause (uppercase first, then length, then other characters) instead of one catch-all rule. Next stays disabled, with the footer hint as before. The swarm UI doc says so.
2026-11-20 21:42:43 +01:00

104 lines
3.5 KiB
TypeScript

// <TextField> — a labelled single-line text input, the shared control
// every form (`CreateAgentForm` today) reaches for instead
// of hand-rolling its own label/input pair. No textarea/multi-line
// mode — promote that the day a real caller needs one, same "don't
// build ahead of a caller" rule the rest of `ui/` follows.
import { FormField } from "../form-field/FormField.js";
import { Hint } from "../hint/Hint.js";
/** One value the field offers to complete to. */
export interface TextFieldSuggestion {
value: string;
/** Shown beside the value in the browser's list. */
label?: string;
}
export function TextField({
id,
label,
value,
onInput,
type = "text",
pattern,
title,
required,
placeholder,
hideLabel,
suggestions,
onKeyDown,
combobox,
error,
}: {
id: string;
label: string;
value: string;
onInput: (value: string) => void;
type?: "text" | "password" | "number" | "url" | "datetime-local";
pattern?: string;
title?: string;
required?: boolean;
placeholder?: string;
/** The label is for assistive tech only; the placeholder says it on screen. */
hideLabel?: boolean;
/** Values to pick from. The field stays free text: a value that is not in
* the list can still be typed. */
suggestions?: TextFieldSuggestion[];
/** Key presses in the field, for a caller that draws its own menu. */
onKeyDown?: (e: KeyboardEvent) => void;
/** Why the value is not acceptable, shown under the field and marked on it.
* Null or absent while it is fine, so a caller can leave it out until the
* operator has typed something. */
error?: string | null;
/** Marks the field as the text box of a menu the caller draws itself. */
combobox?: {
expanded: boolean;
/** The id of the menu's list. */
controls: string;
/** The id of the selected row, when there is one. */
activeDescendant?: string;
};
}) {
const listId = `${id}-suggestions`;
const errorId = `${id}-error`;
const invalid = error !== undefined && error !== null;
return (
<FormField label={label} htmlFor={id} hideLabel={hideLabel}>
<input
id={id}
class={"ui-form-control" + (invalid ? " ui-form-control-invalid" : "")}
aria-invalid={invalid ? true : undefined}
aria-describedby={invalid ? errorId : undefined}
// preact 11 types `type` as a union that ties each kind to its own
// `list` and `role` attributes; the field takes any of its kinds and
// passes `list` only for suggestions, so the union is asserted here.
type={type as "text"}
value={value}
pattern={pattern}
title={title}
required={required}
placeholder={placeholder}
list={suggestions ? listId : undefined}
autoComplete={combobox ? "off" : undefined}
role={combobox ? "combobox" : undefined}
aria-expanded={combobox ? combobox.expanded : undefined}
aria-controls={combobox ? combobox.controls : undefined}
aria-activedescendant={combobox?.activeDescendant}
aria-autocomplete={combobox ? "list" : undefined}
onKeyDown={onKeyDown}
onInput={(e) => onInput((e.target as HTMLInputElement).value)}
/>
{invalid ? (
<Hint tone="error" id={errorId}>
{error}
</Hint>
) : null}
{suggestions ? (
<datalist id={listId}>
{suggestions.map((s) => (
<option key={s.value} value={s.value} label={s.label} />
))}
</datalist>
) : null}
</FormField>
);
}