Unmounting inputs doesn't reset/clear them.
What you were expecting:
Expect field inputs to be unmounted and the input value to be reset/clear with this code:
<FormDataConsumer>
{({ formData, ...rest }) => formData.hasEmail &&
<TextInput source="email" {...rest} />
}
</FormDataConsumer>
What happened instead: Form values are persisted and submitted with the form.
Steps to reproduce: You can copy paste the form from the official documentation and test it.
React admin v4.3.1
I suggest watching for changes to the field you want and resetting the field manually.
import {useFormContext} from "react-hook-form";
...
const {watch, resetField} = useFormContext();
const inputValue = watch("hasEmail");
useEffect(() => {
if (!inputValue) resetField("email");
}, [inputValue]);
@KonkretneKosteczki Thank you! Worked with unregister in my case, but reset works too. Closing.
This behavior is the responsibility of react-hook-form.
You can solve it by passing shouldUnregister in your Form.
<SimpleForm shouldUnregister>
<BooleanInput source="hasEmail" />
<FormDataConsumer>
{({ formData, ...rest }) => formData.hasEmail &&
<TextInput source="email" {...rest} />
}
</FormDataConsumer>
</SimpleForm>
@fzaninotto,
Plus, maybe we should add this to the docs.
And maybe consider setting shouldUnregister=true as default value for react-admin Forms.