'use client'
import {
Input,
Label,
Paragraph,
Span,
VisuallyHidden,
YStack,
type InputProps,
} from '@hanzo/gui'
import type { ColorTokens } from '@hanzogui/web'
import { useId, type ReactNode } from 'react'
import { paint } from '../material/paint'
import { floor, reach } from '../material/reach'
/**
* A labelled control, where the label names the control itself.
*
* `htmlFor` binds to an id, so whoever writes the id has to be whoever renders
* the input. A part that takes a control as a child does not know that id and
* can only put one on the box it draws around it — which binds the label to a
* `
` and leaves the input with no name for a screen reader to read. The
* binding looks the same in the source either way, and the difference is
* audible only to somebody listening.
*
* So this owns the input. There is no arrangement of props that produces a
* label bound to nothing: every field here takes text, and a control that is
* not text gets its own part rather than a hole in this one.
*/
/** What a label and a note read in, in the token layer's own names. */
const { plain, quiet, wrong } = paint
/** The rung a field draws in three places: on a placeholder, on a label that is
* off, and on the text under it. */
const { faint } = paint
/**
* The same rung, for the one prop that is typed against the theme's colour keys
* rather than against a value. `placeholderTextColor` looks the name up and
* falls through to whatever it was handed, which is how every other colour here
* reaches it; the type is narrower than the code, so the value says so.
*/
const ghost = faint as unknown as ColorTokens
export type FieldProps = Omit & {
/**
* What a person typed.
*
* Declared here rather than inherited. The engine's `InputProps` omits both of
* these — it is written for two platforms and a change event is a web thing —
* so a caller inherited nothing and the parameter widened to `any` without a
* word from the compiler. This is a web surface; the event carries the
* element, and the element is what every caller reads.
*/
onChange?: (e: React.ChangeEvent) => void
/** The same thing, already unwrapped, for a caller that wants the text. */
onChangeText?: (text: string) => void
/** What this control is for, read out with it. */
label: ReactNode
/** What a person needs to know before answering. Tied to the control, so a
* screen reader reads it with the field rather than stranding it beside. */
help?: ReactNode
/** What is wrong with the answer. Saying so is also saying it is invalid. */
error?: ReactNode
/**
* The word drawn beside the label for a question that may be left blank.
*
* The word itself and not a flag, because the word belongs to whoever has the
* catalogue: this package ships no copy, and a boolean here would put an
* English string in a component that cannot know the reader's language. It is
* asked for rather than derived from `required`, because the mark is a claim
* about a form — a screen where nothing is required marks nothing, and
* `!required` would mark everything.
*/
optional?: ReactNode
/** Wrong without a sentence about it — a pattern the answer fails, say. */
invalid?: boolean
/**
* The label names the control and is not drawn.
*
* For a control whose place on the screen already says what it does — the
* field on a tab row narrows the list beside it — where a label above would be
* a second heading in a row of headings. It is the label that goes, not the
* naming: the element is still there, still bound, and still what a screen
* reader reads out. There is no prop here that leaves a control nameless.
*/
unseen?: boolean
}
export function Field({
label,
help,
error,
optional,
invalid,
unseen,
id,
disabled,
readOnly,
'aria-describedby': elsewhere,
...rest
}: FieldProps) {
const spare = useId()
const control = id ?? spare
const amiss = invalid ?? Boolean(error)
// Every sentence about this field, in the order it is read.
const about =
[help && `${control}-help`, error && `${control}-error`, elsewhere]
.filter(Boolean)
.join(' ') || undefined
// A block with a stated leading. Clicking a label moves into the field it
// names, so the label is a target, and `floor` is the least a target may be —
// one line stands exactly there and two lines grow past it.
const named = (
)
return (
{unseen ? {named} : named}
{help ? (
{help}
) : null}
{error ? (
{error}
) : null}
)
}