A cleared field takes the default of the field that replaces it
Detect
- In your schema, a field whose
type,const,enum(or the constants of itsanyOf/oneOf),maxItemsordefaultchanges through anif/then, an option switch or a newschemaprop. - In your schema, an
anyOf/oneOfof constants with adefault, used undermergeDefaultsIntoFormData: 'useDefaultIfFormDataUndefined'. - On the rendered page, a field the user cleared that now fills in, or stays empty, after one of those changes.
Change
Two cases of a field the user cleared behave differently now, and neither can be turned off:
- When a field's
type,const,enum(or the constants of itsanyOf/oneOf),maxItemsordefaultchanges, a clear of it gives way to the new field's default under everymergeDefaultsIntoFormData. The field used to stay empty. Adisabledorreadonlyform keeps and gives way to its clears the same way, and so does any other key holdingundefined. Every value stays as it is, including anullitem the user didn't clear. - Under
mergeDefaultsIntoFormData: 'useDefaultIfFormDataUndefined', ananyOf/oneOffield of constants with adefaultstays cleared, as any other field does. Its default used to come back.
If your form relied on the old result, restore it from transformChange, which gets the data an edit settled on. Write undefined back for a field that has to stay empty, or the default for one that has to take it again:
const transformChange = (next) => (next?.kind === 'other' ? { ...next, detail: undefined } : next);
A new schema prop runs no transformChange, so a field that has to stay empty through one needs its data set after the change, with setFieldValue().
Verify
- After an
if/thenswitch, an option switch or a newschemaprop, cleared fields show the result your form expects. - A
transformChangethat writesundefinedback keeps the key, see A transformChange that drops a cleared key loses the clear.
If you can't preserve it
Neither case has an option to turn it off. The only way back to the old result is to set the data yourself, from transformChange for edits or with setFieldValue() after a new schema prop.
Also read: