react.form.validated-input@1.0.0
impl/typescript.tsx
2,915 bytes · the TypeScript implementation · view raw
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
"use client";
import { useId, useState, type JSX, type ReactNode } from "react";
import { validateInput } from "./form_validate_input.ts"; ← from form.validate-input ^1.0.0 · built alongside by fune
import { FormField, fieldIds } from "./react_form_form_field.ts"; ← from react.form.form-field ^1.0.0 · built alongside by fune
import type { ValidatedInputProps } from "./react_form_validated_input_types.ts";
function present(node: ReactNode): boolean {
return node !== null && node !== undefined && node !== false && node !== "";
}
/**
* A GOV.UK text input whose rules are data. The same InputRules go to the
* browser as attributes (required, minLength, maxLength, pattern) and to
* validateInput for the message, so the server can check a submission with
* the very same rules in TypeScript, Python or Rust. A number is a text input
* with a numeric keyboard, not type="number", which GOV.UK advises against;
* its min and max are checked by validateInput, since a text input has no
* min or max attribute.
*/
export function ValidatedInput(props: ValidatedInputProps): JSX.Element {
const auto = useId();
const { label, rules, hint, showError, error, value, defaultValue, onChange, onBlur, autoComplete, spellCheck, disabled, className } = props;
const id = props.id ?? auto;
// The text as it stands, so an uncontrolled input can still be checked.
const [own, setOwn] = useState<string>(defaultValue ?? "");
const current = value ?? own;
// The rules are checked on every render, so a bad rule fails loudly even
// before the field is touched.
const check = validateInput(current, rules);
const shown: ReactNode = present(error) ? error : showError && !check.valid ? check.message : null;
const ids = fieldIds(id, present(hint), present(shown));
const inputMode = props.inputMode ?? (rules.kind === "integer" ? "numeric" : rules.kind === "number" ? "decimal" : undefined);
return (
<FormField id={id} label={label} hint={hint} error={shown} className={className}>
<input
className={ids.error ? "fune-input fune-input--error" : "fune-input"}
id={ids.control}
name={props.name ?? id}
type={rules.kind === "email" ? "email" : "text"}
value={value}
defaultValue={value === undefined ? defaultValue : undefined}
autoComplete={autoComplete}
inputMode={inputMode as JSX.IntrinsicElements["input"]["inputMode"]}
spellCheck={spellCheck ?? (rules.kind === "email" ? false : undefined)}
required={rules.required || undefined}
minLength={rules.minLength ?? undefined}
maxLength={rules.maxLength ?? undefined}
pattern={rules.pattern ?? undefined}
disabled={disabled}
aria-describedby={ids.describedBy ?? undefined}
aria-invalid={ids.error ? true : undefined}
onChange={(event) => {
setOwn(event.target.value);
onChange?.(event.target.value);
}}
onBlur={onBlur ? () => onBlur() : undefined}
/>
</FormField>
);
}