-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
✨ amp-autocomplete: Add support for making object items available in autocomplete select events #30677
✨ amp-autocomplete: Add support for making object items available in autocomplete select events #30677
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @joaopsrleal, thanks for your contribution!
Just to be clear, this change is only applicable for client-side filtering right?
Thank you! :) That's a good point. I hadn't considered the server-side filtering case. I think we should support it as well. To do that, we'd only have to do the same massaging of the data here. Any concerns? Also, I was thinking: right now users have to call data-json and pass the correct name in {{...}} as the value. Something we could consider is to somehow move some of this logic to the template service and auto-expand the template. For instance, this way users would only have to specify data-json (or we can choose another name for the attribute) and then we'd auto expand it to data-json={{objToJson}} in the element's DOM. It would definitely make it easier to use this feature. Let me know whether you think this would be worth it and I'm happy to explore it a bit more. |
Hi @joaopsrleal, thanks for making this contribution! I wanted to ask if it is necessary to add an attribute to expose the new value. It sounds like the proposed approach is to use the attribute to expose the full event object on @micajuine-ho What do you think? |
I like this approach better as it better aligns with the existing API.
I just wanted to make sure that there was no overlap with the changed code and the SSR case. As for adding this as a feature here, I think I would prefer if we continue with the client-side filtering case only, and then if SSR case is requested, then we can add in that feature. I think it's also safer for bugfixes/rollbacks. |
That's an interesting idea. Regarding whether it is needed to add an attribute to expose the new value: we need a way of correlating the element on the DOM (which in this case is the rendered autocomplete suggestion) with the original data item, since we currently use what is on the DOM for determining the event.value. The safest way of doing that is by embedding this information on the elements' DOM. Assuming the elements returned here respect the order of filteredData (may be a brittle assumption), we could modify the element by adding either the json element like I'm doing now or a number representing the index of the data element it represents. This wouldn't require users to add the element themselves. Maybe there's another way of doing it, but I couldn't come up with any other ideas :/. I'd love to hear your thoughts on this. In any case, regardless of whether we use an attribute, I believe we could extend this method to pass a valueAsObject attribute to the created event in the cases it makes sense. I'll make that change. |
I've just made the change to make .valueAsObject available in the event next to .value. They should both be available now. Let me know whether you have any thoughts regarding the need for additing the data attribute. If we think it's safe to assume that the order of the rendered elements is the same as the provided data elements, we could hide that from users. However, I feel this part of the code may become too brittle. Other ideas are welcome. |
The original issue #26525 doesn't specifically request this for client-side rendering only. I imagine that many autocomplete use cases rely on SSR, which means this feature won't initially be available to them. If we decide to go with just client-side rendering, I'll update the documentation to mention that this is currently only supported for client-side rendering to properly set expectations. @caroqliu you did the original triaging of the issue, so you may have additional thoughts here. |
Sorry for closing the PR. I mispressed a button :( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, the changes look great!
Looks like the validator is having some issues; can you please run |
Since I'm currently working on a Windows machine running |
Thanks for your review! :) |
/cc @ampproject/wg-caching for validator test approval. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validation changes look fine
…autocomplete select events (ampproject#30677) * Introduces data-json attribute for making object items available in select events * Fix validator output file * Fix validator output file a second time * Make object available in the valueAsObject field instead of the value field * Remove extra param * Fix JSDoc * Fix JSDoc annotations * Disable lint issue with declared type * Update validator output file * Fix validator output
Introduces a new data-json attribute to support sending objects as the event.value of select & change events. This requires adding a objToJson attribute to the objects passed to the mustache templates used to render the autocomplete suggestions.
Resolves #26525.