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

"Change" event is not triggered #18

Closed
Manukam opened this issue Feb 10, 2023 · 11 comments
Closed

"Change" event is not triggered #18

Manukam opened this issue Feb 10, 2023 · 11 comments

Comments

@Manukam
Copy link

Manukam commented Feb 10, 2023

Hi,

Just linking the previous issue I opened so that the code samples and the context are available.

#17

Issue is, the handle_info function is triggered when the user inputs characters, but once an option is clicked, the phx_change event is not triggered. Any idea why? Is this because I am using live_select inside a component?

Also as mentioned in the other issues, live_select's handle_info is received by the parent, so, I included the handle_event also in the parent

@maxmarcon
Copy link
Owner

Also as mentioned in the other issues, live_select's handle_info is received by the parent, so, I included the handle_event also in the parent

That's wrong. handle_event should be placed in the Live view or component that receives your phx-change form event. So if your form is implemented in a live component, the handle_event callback should be defined in that live component (you also need to set phx-target={@myself} in the form).
The phx-change event has nothing to do with live select specifically, it's the standard phx-change event triggered by all LV forms.

@maxmarcon
Copy link
Owner

where you able to solve the issue @Manukam ?

@Manukam
Copy link
Author

Manukam commented Feb 13, 2023

No, I put the handle_event("validate" ,...) (my form's phx_change event is named as validate) method in both the parent liveview, and in the component as well, still it's not getting triggered. If the event was getting triggered and was going to the wrong place, I should get an error function undefined error, but don't get that as well.

Interesting thing is, I have other form fields in this form, and for them the event is getting fired, only for live_select it's not getting fired.

@maxmarcon
Copy link
Owner

Did you add the JS hooks? Are you seeing errors in your browser's console?

@Manukam
Copy link
Author

Manukam commented Feb 13, 2023

Hooks are working fine, I see whenever I click an option, an update msg from utils.js is printed. My component is not a live component, could that be the reason?

@maxmarcon
Copy link
Owner

I'm confused. I thought your form was in a live component? You said you put handle_event in your component. How can you do this if it's not a live component? Stateless component can't receive events.

I think it will be easier if you share your code

@Manukam
Copy link
Author

Manukam commented Feb 13, 2023

Pretty new to Phoenix and trying to figure this out. Yes you are correct, my form element is in a live component, i.e
use TestWeb, :live_component

I use another custom component inside the live component, this is where the live_select is defined, this sub component is defined as
use Phoenix.Component

@maxmarcon
Copy link
Owner

ok that makes sense. It should work. But it's hard for me to help without seeing any code

Interesting thing is, I have other form fields in this form, and for them the event is getting fired, only for live_select it's not getting fired.

This part is weird. if you're seeing your "validate" event triggered for all fields but not for live_select, the only reason I can think of is that the hooks are not working. But if the hooks aren't working, then you shouldn't be able to select anything. Are you sure you can select something from the dropdown and then it appears in the live select input as selected element, just as it does here?

Absolutely positive that you added the live select hook as explained here?

Again, sharing some code would be the best way to make progress here.

@Manukam
Copy link
Author

Manukam commented Feb 13, 2023

I can select something from the dropdown, but what I selected does not show up as a selected element. The dropdown simply disappears when I click on an option. When I select an option it should trigger the "change" event right?

@maxmarcon
Copy link
Owner

That's a sign that your hooks don't work. I reiterate: have you set up the hooks correctly? Can you show me what you're doing?

@Manukam
Copy link
Author

Manukam commented Feb 13, 2023

Oh! You were right, it was a problem with the hooks. Thank you for this! Sorry for taking this long to figure out this silly mistake!

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