A transformChange that drops a cleared key loses the clear
Detect
mergeDefaultsIntoFormData: 'useDefaultIfFormDataUndefined'in yourdefaultFormStateBehavior.- A
transformChangein your code that removes keys holdingundefined, for example by destructuring them away. - A
transformChangethat copies withJSON.parse(JSON.stringify(next)). - On the rendered page, a field the user cleared that fills its default back in.
Change
A clear holds only while the key is there holding undefined. A missing key used to count as cleared too. Pass the undefined through to keep it:
-const transformChange = ({ code, ...rest }) => (code ? { ...rest, code: code.toUpperCase() } : rest);
+const transformChange = (next) => (next.code === undefined ? next : { ...next, code: next.code.toUpperCase() });
JSON.parse(JSON.stringify(next)) drops every undefined. Copy with structuredClone() instead:
-const copy = JSON.parse(JSON.stringify(next));
+const copy = structuredClone(next);
Verify
- A field the user cleared stays empty after the next edit elsewhere in the form.
- See Refusing or rewriting an edit moves from onChange to transformChange for the other rules
transformChangehas to follow.
Also read: