When form.reset() leaves the UI out of sync
A Contour UI bug: field values reset, but character counts and textarea height stayed behind. What fixed it, and what to test.
Contour’s text fields can show a character count, and its textareas can grow with their content. An earlier version handled typing correctly but missed another way a field can change: the browser’s native form reset.
After a reset, the text returned to its default value while the counter could still describe the edited text. A textarea could also keep the height of a much longer note. The field and the interface around it no longer agreed.
The browser reset its state. The component had more.
An uncontrolled field lets the browser own its current value. A character count or an auto-sized textarea adds derived UI state. Listening only for edits is not enough to keep those pieces synchronized.
Calling form.reset() restores the controls’ default values. It is not an instruction to reset every React state variable associated with the form. In this case, the counter and height needed their own synchronization.
The reset event is too early to read the result.
The HTML reset algorithm dispatches a cancelable reset event before resetting the controls. Reading a field’s value inside that listener can therefore read the value that is about to be replaced.
Contour schedules the synchronization in a later task and checks whether the reset was canceled. The important timing decision looks like this:
// Core timing decision; not the complete lifecycle hook.
const onReset = (event: Event) => {
setTimeout(() => {
if (!event.defaultPrevented) syncFromControl();
}, 0);
};syncFromControl() stands for reading the field’s resulting value and updating the derived UI. The complete hook also removes its event listener and clears pending work on unmount. It listens on the owner document and matches the field’s actual form owner, so a field associated through a form attribute is covered too.
Controlled fields have a different owner.
For a controlled field, the parent’s value remains authoritative. Resetting that form’s application state belongs to the parent. The library must keep the DOM and its derived UI aligned with the supplied value; it should not quietly replace the parent’s state with a guessed default.
That distinction matters when extracting a reusable component: native browser behavior and application state both need an explicit owner.
What is worth testing
Checking only the input’s text would have missed this defect. A useful regression check edits the field, grows the textarea, triggers the real reset action, and checks all three results: value, count and height.
- Reset through both a reset button and
form.reset(). - Cancel a reset and verify the edited state remains.
- Associate a field with a form outside its DOM parent.
- After resetting, type the same value used before the reset and check that the counter still updates.
- For controlled fields, verify that the parent’s value remains authoritative.
The uncontrolled counter and height correction shipped in Contour UI 0.2.0. The current portfolio uses 0.6.3. The sample in the case study runs the installed package; it demonstrates the corrected behavior rather than recreating the old bug.
Try the reset example