epicweb-dev / web-forms

Learn the primary mechanism for interactivity on the web.
https://www.epicweb.dev/workshops/professional-web-forms
Other
199 stars 61 forks source link

fix: add condition to re-trigger focus upon new submissions #36

Closed gustavoalvesleao closed 10 months ago

gustavoalvesleao commented 10 months ago

When using the hook useFocusInvalid, introduced in Schema Validation lesson 01. Zod schema validation, we have a problem: The actionData.status is always equals to 'error', which means the useEffect only runs once. This leads us not having the focus being set properly after the second "invalid" submission attempt:

https://github.com/epicweb-dev/web-forms/assets/46943072/91426e67-f44f-4536-8049-67e1410357d4

* Just a note that this was not occurring in the previous lessons, as actionData was used as a dependency and therefore was new after every submission

The proposed solution is simply to add is to add isSubmitting in the boolean that forms hasErrors, so we can have the useEffect running after every submission:

https://github.com/epicweb-dev/web-forms/assets/46943072/e4b46352-4c6b-461a-bd9f-25539203d13c