@k8ordo/form

Custom inputs

Put the value of a component that renders no <input name>, such as a rich text editor or a custom picker, into the submission with HiddenValue.

On this page

Submitting the value

post-form.tsx
const [body, setBody] = useState(post.body);

<Editor onChange={setBody} value={body} />
<HiddenValue name="body" value={body} />

Keep the component’s value in state and pass the same value to HiddenValue. Its name is the field’s path in the schema.

HiddenValue renders an <input type="hidden">. The value is submitted with the other inputs, so it needs no submit-time code to gather it.

Information

Note

@k8ordo/ui components put their own values into the submission and need no HiddenValue (see Forms).

Change detection

When React updates value, no DOM event fires. So with an <input type="hidden"> placed directly, cross-field rules and isDirty are not updated.

HiddenValue fires an input event on every change, so the rules and isDirty update as they do for a keystroke.

Reset and browser validation

post-form.tsx
<form
  {...form.props}
  onReset={() => {
    form.props.onReset();
    setBody(post.body);
  }}
>

The value lives in the caller’s state, so resetting the form does not restore a HiddenValue, and isDirty stays true until that state is reset. In your own onReset, call form.props.onReset() and then reset the state.

Warning

Pitfall

A hidden input is excluded from the browser’s constraint validation. Constraints such as required or min are not checked in the browser; the server checks them after the submission.

Playground

Star rating demo

The star buttons are a custom control with no <input>. The chosen value goes into the submission through HiddenValue.

Rating
isDirtyfalse

Try it

  1. Choose a star. HiddenValue fires an input event and isDirty turns true.
  2. Press “Submit”. The value that would be sent is shown, as in rating=3.
  3. Press “Revert”. The stars and isDirty both go back.
k8ordo

React libraries that use Baseline features without holding back.