Files
chatkit/dist/react/DictationField.d.ts
CatoandClaude Opus 5.5 f82952e06c feat(field): DictationField with no state in the app — native forms and react-hook-form (0.45.0) (#114)
* feat(field): DictationField with no state in the app — native forms and react-hook-form (0.44.0)

- Uncontrolled mode: value/onValueChange optional, new defaultValue; the real
  text box carries name, so a native form or server action posts typed and
  said words, and a form reset restores defaultValue.
- react-hook-form: forwardRef (ref is the text box), onChange(event) for
  every change including microphone/Improve/Undo/setText, onBlur(event).
- Browser test drives react-hook-form itself (register, Controller, reset)
  and a native form; docs/roadmap.md records the remaining typing gaps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtoej4Vu1mYafzzcLc

* docs(field): say the ref works across the peer React range, not a version

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtoej4Vu1mYafzzcLc

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-15 19:13:22 +01:00

197 lines
9.6 KiB
TypeScript

import { type ChangeEvent, type HTMLAttributes, type KeyboardEvent as ReactKeyboardEvent, type ReactNode, type Ref } from "react";
import type { DictationProblem } from "../dictation.js";
import type { ComposerVoice } from "./Composer.js";
import type { AttachOptions } from "../attachments.js";
import type { AttachmentsController } from "./use-attachments.js";
import type { AttachExtra } from "./AttachMenu.js";
import { type TypingAttachLabelOverrides, type TypingAttachLabels, type TypingHandle } from "./typing.js";
export type DictationFieldLabels = TypingAttachLabels & {
voice: string;
voiceStop: string;
cancelRecording: string;
confirmRecording: string;
transcribing: string;
listening: string;
retryTake: string;
dismiss: string;
dictation: Record<DictationProblem, string>;
/** The button that opens the improve panel. */
improve: string;
/** Placeholder of the "what should change?" box. */
improveWhat: string;
improveApply: string;
improveWorking: string;
improveFailed: string;
/** Shown after a change, beside Undo. */
improved: string;
undo: string;
};
export declare const DEFAULT_DICTATION_FIELD_LABELS: DictationFieldLabels;
/**
* Writing help for a field: given the text and what the person asked for,
* resolve to the new text, or to `null` when it could not be done. Chatkit
* ships no model call and no prompt — the app (or `@bitbaum/writingkit`)
* owns those — only the way a person asks, sees and takes it back.
*/
/**
* What a page can do to a field it does not type into (`handleRef`, 0.43):
* `setText` replaces the words (kept within `maxLength`), `addFiles` stages
* as the paperclip would, `focus`, and `submit` runs `onSubmit` with the same
* checks as the submit key. The same shape as `ComposerHandle`.
*/
export type DictationFieldHandle = TypingHandle;
export type ImproveRun = (text: string, instruction: string) => Promise<string | null | undefined>;
/**
* Props the component sets itself. `inputProps` cannot replace them: the
* field's value, its label wiring, its error state, its 16px text and its
* keys belong to the package.
*/
type ManagedInputKey = "id" | "className" | "style" | "value" | "defaultValue" | "placeholder" | "children" | "dangerouslySetInnerHTML" | "onChange" | "onKeyDown" | "onFocus" | "onBlur" | "aria-label" | "aria-invalid";
/**
* What an app may add to the text box itself: `spellCheck`, `autoComplete`,
* `autoCapitalize`, `inputMode`, `enterKeyHint`, `minLength`, `aria-*` and
* `data-*`. Spread before the managed props, so the managed ones win;
* `aria-describedby` is joined with the hint and error ids, not replaced.
*/
export type DictationFieldInputProps = Omit<HTMLAttributes<HTMLElement>, ManagedInputKey> & {
autoComplete?: string;
minLength?: number;
} & {
[data: `data-${string}`]: string | number | boolean | undefined;
};
/** The text box itself: a `<textarea>`, or an `<input>` when one line. */
export type DictationFieldElement = HTMLTextAreaElement | HTMLInputElement;
/**
* What `onChange` and `onBlur` receive: a real React event when the person
* types or leaves, or the same shape (`type`, `target`, `currentTarget`, the
* text box with its new value already in it) when the words came from the
* microphone, Improve, Undo, a form reset or `handleRef.setText`. Enough for
* `react-hook-form`'s `register()` and `Controller`, and for any handler that
* reads `event.target.value` or `event.target.name`.
*/
export type DictationFieldEvent = Pick<ChangeEvent<DictationFieldElement>, "target" | "currentTarget"> & {
type: string;
};
export type DictationFieldProps = {
/**
* The words, held by the app (controlled). Leave it out, and the field
* holds its own words from `defaultValue` (uncontrolled): give it a `name`
* and a native `<form>` or a server action posts them, and a form reset
* puts `defaultValue` back.
*/
value?: string;
/** The first words of an uncontrolled field. Ignored when `value` is given. */
defaultValue?: string;
/** Every change, as text: typed, said, improved, undone. */
onValueChange?: (next: string) => void;
/**
* Every change, as an event (see `DictationFieldEvent`) — the shape a
* form library reads. `{...register("note")}` from react-hook-form and a
* `Controller`'s `{...field}` both spread straight onto the field.
*/
onChange?: (event: DictationFieldEvent) => void;
/** Visible label. Without one, `ariaLabel` is required for assistive tech. */
label?: ReactNode;
ariaLabel?: string;
id?: string;
placeholder?: string;
/** `false`: a one-line field (a title, a summary). Default: a growing text area. */
multiline?: boolean;
/** Fewest rows a multiline field shows. Default 3. */
rows?: number;
maxLength?: number;
disabled?: boolean;
required?: boolean;
/** The field's name in a native `<form>` post (the real text box carries it). */
name?: string;
autoFocus?: boolean;
/** Shown, selectable, not editable: no microphone, no Improve, no Undo. */
readOnly?: boolean;
/** The error state: `aria-invalid`, the frame in the danger colour. */
invalid?: boolean;
/** The error, in words, under the field (`role="alert"`). Implies `invalid`. */
error?: ReactNode;
/** Runs first; `event.preventDefault()` here stops `onSubmit`. */
onKeyDown?: (event: ReactKeyboardEvent<HTMLTextAreaElement | HTMLInputElement>) => void;
/**
* The submit keys, the same in every app: Cmd+Enter or Ctrl+Enter in a
* multi-line field, Enter in a one-line one. Never during an IME
* composition, never while disabled or read-only, never while a take is
* being recorded or transcribed.
*/
onSubmit?: () => void;
/** Focus entered the FIELD (text box, microphone, Improve and its panel). */
onFocus?: () => void;
/**
* Focus left the whole field — not when it moves between its parts, so
* pressing the microphone is not a blur. Leaving while a take is recorded
* or transcribed is reported once the take has ended and its words have
* been delivered through `onValueChange`. Receives a blur-shaped
* `DictationFieldEvent` whose `target` is the text box (react-hook-form
* reads its `name`).
*/
onBlur?: (event: DictationFieldEvent) => void;
/** Extra attributes for the text box; see `DictationFieldInputProps`. */
inputProps?: DictationFieldInputProps;
/** The microphone. Pass `{ transcribeUrl }` for the server leg; `false` hides it. */
voice?: ComposerVoice | false;
/** Give it, and a non-empty field offers "Improve". */
improve?: {
run: ImproveRun;
suggestions?: readonly string[];
};
/**
* The paperclip: the same as `Composer`'s (the Camera · Photos · Files
* sheet, a pasted screenshot staged, a dropped file, previews with Remove,
* the same limits and notes). Off by default. Pass `AttachOptions` for the
* app's limits, `sources`, `accept`, `readFile` or `take`. A recording
* picked as a file is transcribed into the words, as in the composer.
*/
attach?: boolean | AttachOptions;
/**
* The staged files, held by the APP: `const files = useAttachments(opts)`,
* passed here, read at the app's own submit (`files.toWire()`) and cleared
* after (`files.clear()`). Without it the field stages into its own, which
* the app cannot read — so an app that attaches passes one. Give
* `useAttachments` the same options as `attach` (its limits and `readFile`
* decide what stages; `attach` decides what the paperclip offers).
*/
attachments?: AttachmentsController;
/**
* The app's own rows in the paperclip's sheet, under Camera · Photos ·
* Files (one of the person's things, a saved note) — `Composer`'s
* `attachExtras`, the same prop. With `attach: false` the paperclip opens a
* sheet of only these rows. Never on a read-only field.
*/
attachExtras?: readonly AttachExtra[];
/** Fill, stage, focus or submit from outside the field (see `DictationFieldHandle`). */
handleRef?: Ref<DictationFieldHandle>;
/** Under the field: a count, a rule, an example. */
hint?: ReactNode;
/** The text box. The component's own `ref` reaches the same element. */
inputRef?: Ref<DictationFieldElement>;
labels?: Partial<Omit<DictationFieldLabels, "dictation" | "attachNotes" | "attachMenu">> & {
dictation?: Partial<DictationFieldLabels["dictation"]>;
} & TypingAttachLabelOverrides;
className?: string;
};
/**
* ANY text box a person types words into: the microphone is part of it.
*
* This is the rule from AGENTS.md ("The experience belongs to the package")
* made a component: a field made by hand in an app has no microphone, 14px
* text that makes iOS zoom the page, and no way to take a failed take back —
* so there is one, and it is this. `Composer` is the same thing for a
* conversation (it also sends); this is for everything else: a note, a title,
* a bio, a message to a seller.
*
* Controlled (`value`) or uncontrolled (`defaultValue` + `name`, posted by a
* native form). `ref`, `name`, `onChange(event)` and `onBlur(event)` make it
* a drop-in for react-hook-form's `register()` and `Controller`.
* Dictation appends to what is there. With `improve`, a field that
* has text offers one more control: pick a suggestion or say or type what to
* change, the text is replaced, and Undo puts it back until the next edit.
*/
export declare const DictationField: import("react").ForwardRefExoticComponent<DictationFieldProps & import("react").RefAttributes<DictationFieldElement>>;
export {};
//# sourceMappingURL=DictationField.d.ts.map