Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controlled input validation on blur #812

Open
juanpmarin opened this issue Oct 30, 2024 · 1 comment
Open

Controlled input validation on blur #812

juanpmarin opened this issue Oct 30, 2024 · 1 comment

Comments

@juanpmarin
Copy link

Describe the bug and the expected behavior

I'm setting up a shadcn select, like this:
https://github.com/juanpmarin/conform-issue-repro/blob/b3324dbcddfa201b4f1165b7aeb95cfd980cf2be/components/buggy-form.tsx#L57-L114

And I'm setting shouldValidate to onBlur, because control.blur is invoked when the select is closed, validation should be triggered and error message should hide, it does not work as expected.

Conform version

v1.2.2

Steps to Reproduce the Bug or Issue

  • Clone and run this project
  • Submit the form without selecting a value from the dropdown
  • An error message will appear
  • Select a city from the dropdown menu
  • The error message does not disappear

What browsers are you seeing the problem on?

No response

Screenshots or Videos

No response

Additional context

No response

@AMEH64
Copy link
Contributor

AMEH64 commented Nov 14, 2024

I did some initial investigation. Below are some notes with my findings.

The useControl hook uses the useInputEvent hook internally, which adds a focusout event listener to manage its internal state for the blur event logic.

https://github.com/edmundhung/conform/blob/cb984945e9cf3a5448bafeef490d8a2dcc845226/packages/conform-react/integrations.ts#L184C1-L207C12

Conform will focus the first invalid field when the form is submitted. Since the native select element is focused on submission, the aforementioned focusout event listener will be triggered after another element gains focus, either programmatically via focus delegation of the custom control or if a user interacts with the page.

https://github.com/edmundhung/conform/blob/cb984945e9cf3a5448bafeef490d8a2dcc845226/docs/api/react/useInputControl.md?plain=1#L65C1-L66C1

As a result, the blur event is not dispatched for the native select when the controls blur method is called after the options menu closes.

https://github.com/edmundhung/conform/blob/cb984945e9cf3a5448bafeef490d8a2dcc845226/packages/conform-react/integrations.ts#L247C1-L264C6

If you add a second select field to the form, the issue is only replicable for the first field since only the first field is focused on submission after being invalidated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants