You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have a use-case with our forms where some fields are conditionally rendered e.g. based on the value of another field or because the you can 'Add another'.
Running into an issue however where fields are rendered and touched and then when they are unmounted from the DOM to no longer be shown their state in form state remains as touched being true. This means that if the fields are re-mounted again they will immediately display errors present on the field (because we are determining whether field errors/warnings should be show based on whether it is touched). Do you think it is reasonable to determine whether errors should be shown based on touched state for the field? Can you suggest an alternative from your own usage?
Alternatively, other things I've been considering:
could/should form-container handle this kind of cleanup when the field is unmounted? For this to work we would probably need a HOC to render a field which can be rendered inside a form and does necessary cleanup in its willUnmount
low-effort solution would be to expose a setFieldToUntouched(field: string) or setTouched(field: string, touched: boolean) function in formMethods, which would allow us to do the necessary cleanup in onClick/onChange handlers
The text was updated successfully, but these errors were encountered:
This isn't completely representative, in our own usage we've added some extra logic in the validators to only apply the validation when the field is rendered (e.g. the bankAccountType === 'BUILDING_SOCIETY' condition), but it will give you an idea of what we are working with.
Steps to reproduce:
Select 'Building society' for 'Bank account type'
Touch the 'Roll' number field and enter a value
Clear the value so that an error is shown
Select 'Bank' for 'Bank account type' so that Roll number is hidden
Select 'Building society' for 'Bank account type' so that 'Roll number' is shown again
Expected result:
Field should probably be untouched and should not show an error
We have a use-case with our forms where some fields are conditionally rendered e.g. based on the value of another field or because the you can 'Add another'.
Running into an issue however where fields are rendered and touched and then when they are unmounted from the DOM to no longer be shown their state in form state remains as
touched
beingtrue
. This means that if the fields are re-mounted again they will immediately display errors present on the field (because we are determining whether field errors/warnings should be show based on whether it istouched
). Do you think it is reasonable to determine whether errors should be shown based ontouched
state for the field? Can you suggest an alternative from your own usage?Alternatively, other things I've been considering:
willUnmount
setFieldToUntouched(field: string)
orsetTouched(field: string, touched: boolean)
function informMethods
, which would allow us to do the necessary cleanup in onClick/onChange handlersThe text was updated successfully, but these errors were encountered: