Hook for a text input tracking touched, dirty, and baseline
A profile form only shows a validation error after the user has left a field (touched), enables “Save” only when something changed (dirty), and warns before navigating away with unsaved edits. The profile itself arrives from the server after the form has already rendered. Implement a React hook, useField, that manages one controlled text input and tracks this state.
function useField(initialValue: string): {
value: string;
dirty: boolean;
touched: boolean;
bind: {
value: string;
onChange: (event: { target: { value: string } }) => void;
onBlur: () => void;
};
setValue: (next: string) => void;
reset: () => void;
commit: () => void;
}
The baseline is the value the field is compared against. It starts as initialValue.
value starts as initialValue. bind is spread onto an input: <input {...field.bind} />. Its value is the current value, its onChange stores event.target.value, and its onBlur marks the field as touched.setValue(next) changes the value from code, exactly like typing.dirty is true when value differs from the baseline. It is compared every time, so typing the original text back makes the field not dirty again.touched starts false and becomes true on the first blur. Typing alone does not touch the field. Once touched, it stays touched until reset().reset() sets the value back to the baseline and touched back to false.commit() makes the current value the new baseline (call it after a successful save). The field is no longer dirty. touched does not change.initialValue (for example, the server data arrived), the baseline becomes the new initialValue. If the field is not dirty, the value also becomes the new initialValue. If it is dirty, keep the user’s text; dirty is then recomputed against the new baseline.initialValue arrives in (no flash of the old value).function NameField({ savedName, onSave }) {
const name = useField(savedName);
return (
<form onSubmit={(e) => { e.preventDefault(); onSave(name.value); name.commit(); }}>
<input aria-label="Display name" {...name.bind} />
{name.touched && name.value === '' && <p>Name is required</p>}
<button disabled={!name.dirty}>Save</button>
<button type="button" onClick={name.reset}>Discard</button>
</form>
);
}
// useField('Ada') value 'Ada', dirty false, touched false
// type 'Ada L' value 'Ada L', dirty true, touched false
// blur touched true
// type 'Ada' value 'Ada', dirty false (same as the baseline again)
// type 'Grace'; commit() baseline 'Grace', dirty false
// reset() value 'Grace', touched false
// Not dirty, initialValue '' -> 'Ada': value becomes 'Ada'
// Dirty with 'Al', initialValue 'Ada' -> 'Alan': value stays 'Al', dirty true
// Dirty with 'Al', initialValue 'Ada' -> 'Al': value stays 'Al', dirty false
<input aria-label="Display name" {...field.bind} /> and reads the object your hook returns.focusout) event on it, and call setValue, reset and commit inside ReactDOM.flushSync. They also check the input’s displayed value.Object.assign(window, { useField }); below “Do not edit below this line”. The tests use it to call your hook.