Components / Foundations
HiddenValue
Lets a custom widget take part in a native form. It renders a visually and semantically hidden input that carries the widget's value, so the form sees it like any other field. It is a foundation: it has no visual output.
Who uses itLink to "Who uses it"
When to use it directlyLink to "When to use it directly"
When you build a custom widget (a listbox, a picker) that has no native input of its own but must submit a value, validate and reset with its form.
Use a component instead when a ready-made control fits: Select for choosing from a list, TextField for text.
UsageLink to "Usage"
tsx
import { HiddenValue } from "@arshad-shah/cynosure";
<HiddenValue
name="plan"
value={plan}
required
requiredMessage="Choose a plan."
onReset={() => setPlan(defaultPlan)}
onInvalid={(event) => {
event.preventDefault();
triggerRef.current?.focus();
}}
/>PropsLink to "Props"
| Prop | What it does | Type | Default |
|---|---|---|---|
valueRequired | The value to submit. | string | |
name | Form field name the value is submitted under. | string | |
form | id of the form to submit with, when rendered outside it. | string | |
required | Fails form validation while value is empty. | boolean | |
requiredMessage | Message used when required and empty. | string | |
onReset | Called when the owning form is reset, so the widget can restore its default. | (() => void) | |
disabled | Disabled values are neither submitted nor validated (like native disabled controls). | boolean | |
onInvalid | Called when validation fails. Call event.preventDefault() to stop the browser reporting on this hidden input, then focus and explain on the widget's visible control instead. | ((event: Event) => void) |
Generated from the type definitions in @arshad-shah/cynosure 0.5.0.
GuaranteesLink to "Guarantees"
- Submits:
nameandvalueappear in FormData. Useformto associate it with a form elsewhere in the page. - Validates: while
requiredand the value is empty, the form is invalid withrequiredMessage(default "Please choose a value."). - Resets:
onResetis called when the owning form is reset, so the widget can restore its default. - Disabled: a
disabledvalue is neither submitted nor validated, like a native disabled control. - Custom reporting: call
event.preventDefault()inonInvalidto stop the browser reporting on the hidden input, then focus and explain on the widget's visible control. - It is a text input rather than
type="hidden"so constraint validation applies, and it is out of the tab order and hidden from assistive technology.
RelatedLink to "Related"
These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.