Refusing or rewriting an edit moves from onChange to transformChange
Detect
- An
onChangein your code that returns early without storing the event, to refuse an edit. - An
onChangethat stores a rewritten copy offormDatathat can undo the edit just made, such as a trimmed string, which drops the space just typed. - A form passed
initialFormDatawhoseonChangerewritesformDataor computes a field from other fields and passes the result back, which the form doesn't read after mount.
Change
By the time onChange runs, the form has already applied the edit, so not storing the event no longer refuses it, under either prop. onChange reports an edit and no longer decides it. That decision goes in transformChange. A parent passing formData can still store a rewritten value, which the form then shows, unless the rewrite gives back the value passed before (see A form passed formData shows each new value you pass), but a refusal goes here. It is a pure function that receives the data the edit produced (next) and the data before it (previous), and returns the data the form should keep. The form calls it for every edit, from a field or from setFieldValue(), and for those alone, before it stores, validates and reports the result. What the form derives on its own (its defaults, the live omit a blur runs and a reset()) is stored without it, and only the last two are reported through onChange. The defaults are filled in on mount and again when the schema, a ui:emptyValue, defaultFormStateBehavior or additionalPropertiesWhenAbsent changes.
-const onChange = ({ formData }) => {
- if ((formData.title ?? '').length > 80) return;
- setData({ ...formData, code: formData.code?.toUpperCase() });
-};
+const transformChange = (next, previous) => {
+ if ((next?.title ?? '').length > 80) return previous;
+ return next?.code === undefined ? next : { ...next, code: next.code.toUpperCase() };
+};
+const onChange = ({ formData }) => setData(formData);
Return next to take the edit, previous to refuse it, or another value. Keep these rules in mind:
- It runs for every keystroke, so a rewrite has to hold for a value the user is still typing and for a field they cleared, which is
undefined. Upper-casing holds. Trimming would remove a space as soon as it is typed. - Return
next's unchanged subtrees as they are, so their fields don't render again. - Keep the function pure. The form may call it more than once for the same edit, the way React may call a component more than once.
- A decision that has to wait, such as a server check, can still undo an edit afterwards with
setFieldValue().
A form passed formData can keep computing a field from other fields in onChange and passing the result back. Under initialFormData, compute it in transformChange, and into initialFormData too, since the seed doesn't go through transformChange.
A transformChange that drops keys holding undefined loses the user's clears, see A transformChange that drops a cleared key loses the clear.
Verify
- A refused edit no longer shows in the form, and a rewritten value shows as rewritten while the user types.
- When an edit switches a
oneOf/anyOfoption, the selector stays on the option the user picked unless you refuse the switch or rewrite that field's data. A rewrite elsewhere in the form keeps the choice. A rewrite at the field keeps it only while the rewritten data still fits the picked option, and otherwise moves the selector to the option that data fits.
Also read: