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

USelectMenu doesn't emit UForm.change as expected #3660

Open
twinkelmann opened this issue Mar 24, 2025 · 3 comments
Open

USelectMenu doesn't emit UForm.change as expected #3660

twinkelmann opened this issue Mar 24, 2025 · 3 comments
Assignees
Labels
bug Something isn't working v3 #1289

Comments

@twinkelmann
Copy link

Environment

Is this bug related to Nuxt or Vue?

Nuxt

Version

v3.16.1

Reproduction

https://codesandbox.io/p/devbox/dry-sun-8ddkqr

Description

The events of the USelectMenu do not work as intended. As you can see in the reproduction, the change event of the UForm is expected to be emitted every time you select/deselect/create an item, but instead it's emitted only a few seemingly random times.

Switching to the input event on the form has the same random behavior.

I am also surprised programmatically changing the list of values doesn't trigger the update event on the USelectMenu, but that could be by design and is not really the blocking issue here.

My reproduction code is a simplification of my real code. It is very similar to the documentation example for creating items, main difference is the multi selection. However the issue also occurs without multi selection.

Additional context

No response

Logs

@twinkelmann twinkelmann added bug Something isn't working triage v3 #1289 labels Mar 24, 2025
@twinkelmann
Copy link
Author

A few concrete examples to try on the reproduction yourself:

If the dropdown has 2 options and you alternate toggling both (first off, first on, second off, second on), the form change event will consistently get triggered on the unchecking of the option. It will not trigger on the checking of the option, and it will not trigger on the unchecking if you are toggling the same option multiple times in a row.

If you disable the first, then the second, then enable the second, then enable the first (or the exact opposite), you can repeat this loop and the event is never triggered.

@twinkelmann
Copy link
Author

I suspect an issue in the code checking if the form's state changed before generating events (SelectMenu.vue:257,

function onUpdate(value: any) {
  if (toRaw(props.modelValue) === value) {  // <---- this condition ?
    return
  }
  // @ts-expect-error - 'target' does not exist in type 'EventInit'
  const event = new Event('change', { target: { value } })
  emits('change', event)
  emitFormChange()
  emitFormInput()
}

?), because even if I call form.value?.submit() manually when the field changed, the submit event does not fire more often than the update/input events.

Anyways I'm finding no workaround so I will have to implement something custom and dirty for now...

@romhml
Copy link
Member

romhml commented Mar 24, 2025

The form doesn't implement a @change event, it only support @error and @submit. Not sure why the event handler is even called in your example, I'll try to look into it.

The emitFormChange in the SelectMenu's code is here to trigger the Form's validation when its value changes.

To fix your issue, you should bind the @change event handler on your inputs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v3 #1289
Projects
None yet
Development

No branches or pull requests

2 participants