Repository navigation
feat: add support for @EventData of type Component - #12334
Conversation
Makes it possible to use event data expressions that map to any Component / Element from server side state tree.
For example it will be possible to use:
- `@EventData("event.target") Component eventTarget` for `@DomEvent`
- `@EventData("event.target") Element eventTarget` for `@DomEvent`
- `DomEventRegistration::addEventDataElement(String expression)` to get any element mapped,
- and fetch it with `DomEvent::getEventDataElement(String expression)
Follow up for #11992, #3356
|
|
||
| public MappedDomEventWithRouterLinkData(Component source, | ||
| boolean fromClient, @EventData("component") Component component, | ||
| @EventData("router.link") RouterLink routerLink) { |
There was a problem hiding this comment.
This looks like "user generated" components should be able to be returned, like e.g. "MyCustomComponent extends Component" - is this intended? If yes, would be awesome to have a test for this; because this could be really awesome for type-safe component usage (probably problematic with generics..)
There was a problem hiding this comment.
Do mean like https://github.com/vaadin/flow/pull/12334/files#diff-c6d2fb2b77355f6aa7c20d042332d2563c7e9313aff1a02809a5ee3d7857e342R94 where the H3 component is mapped if it is there, or an error thrown when the type doesn't match. The implementation is in: https://github.com/vaadin/flow/pull/12334/files#diff-488cb30a1687bdf305e93b812187b637e6a3b01fce4f0c900ec73e49a7c79721R348 and it uses existing ComponentMapping server side feature, which says:
/**
* Gets the component this node has been mapped to, if any.
*
* @return an optional component, or an empty optional if no component has
* been mapped to this node
*/
public Optional<Component> getComponent() {
return Optional.ofNullable(component);
}So in case you're creating things via the element API, but not as a component mapping, it will return null when you try to map it to a component. We do have code which will "map the component to an existing element", do you mean that I should also take this into account and make it possible ?
There was a problem hiding this comment.
Maybe I misinterpreted your note/question 😅
Yes, any component instance type is supported and there is an integration test at least with H3 which should not be any different from any other component (in the eyes of the framework code).
But if you want to be able to map to things that are created with new Element("foobar") to @EventData("event.target.getFoobarElement()") Foobar foobar where Foobar is @Tag("foobar") public class Foobar extends Component, it will not work currently as there is no existing component mapping. But, if the element instance has a component mapping as it has been created with new Foobar(), then it works.
There was a problem hiding this comment.
That make sense, as long as there is currently the target "in memory" / already instantiated it can be used. Sounds good! Totally forgot that e.g. RouterLink is already a "user generated" component for flow, but you are right - that should work :)
Also refactors the IT code slightly.
| do { | ||
| stateNodeForDomNode = stateTree | ||
| .getStateNodeForDomNode(targetNode); | ||
| targetNode = DomApi.wrap(targetNode.getParentNode()); | ||
| } while (stateNodeForDomNode == null && targetNode != null); | ||
| if (stateNodeForDomNode != null) { | ||
| return stateNodeForDomNode.getId(); | ||
| } |
There was a problem hiding this comment.
| do { | |
| stateNodeForDomNode = stateTree | |
| .getStateNodeForDomNode(targetNode); | |
| targetNode = DomApi.wrap(targetNode.getParentNode()); | |
| } while (stateNodeForDomNode == null && targetNode != null); | |
| if (stateNodeForDomNode != null) { | |
| return stateNodeForDomNode.getId(); | |
| } | |
| while (targetNode != null) { | |
| StateNode stateNodeForDomNode = stateTree | |
| .getStateNodeForDomNode(targetNode); | |
| if (stateNodeForDomNode != null) { | |
| return stateNodeForDomNode.getId(); | |
| } | |
| targetNode = DomApi.wrap(targetNode.getParentNode()); | |
| } |
As do-whiles are always a bit harder to read, especially with the two exit conditions.
|
Two two Sonar critical issues about refactoring |
Done |
|
SonarQube analysis reported 7 issues
|
Makes it possible to use event data expressions that map to any Component / Element from server side state tree.
For example it will be possible to use:
- `@EventData("event.target") Component eventTarget` for `@DomEvent`
- `@EventData("event.target") Element eventTarget` for `@DomEvent`
- `DomEventRegistration::addEventDataElement(String expression)` to get any element mapped,
- and fetch it with `DomEvent::getEventDataElement(String expression)
Follow up for #11992, #3356
Makes it possible to use event data expressions that map to any Component / Element from server side state tree.
For example it will be possible to use:
- `@EventData("event.target") Component eventTarget` for `@DomEvent`
- `@EventData("event.target") Element eventTarget` for `@DomEvent`
- `DomEventRegistration::addEventDataElement(String expression)` to get any element mapped,
- and fetch it with `DomEvent::getEventDataElement(String expression)
Follow up for #11992, #3356
|
Cherry-picked to 2.8 |
|
This ticket/PR has been released with platform 22.0.0.rc1 and is also targeting the upcoming stable 22.0.0 version. |
Makes it possible to use event data expressions that map to any Component / Element from server side state tree.
For example it will be possible to use:
- `@EventData("event.target") Component eventTarget` for `@DomEvent`
- `@EventData("event.target") Element eventTarget` for `@DomEvent`
- `DomEventRegistration::addEventDataElement(String expression)` to get any element mapped,
- and fetch it with `DomEvent::getEventDataElement(String expression)
Follow up for #11992, #3356
|
This ticket/PR has been released with Vaadin 14.9.0.alpha1 and is also targeting the upcoming stable 14.9.0 version. |



Makes it possible to use event data expressions that map to any Component / Element from server side state tree.
For example it will be possible to use:
@EventData("event.target") Component eventTargetfor@DomEvent@EventData("event.target") Element eventTargetfor@DomEventDomEventRegistration::addEventDataElement(String expression)to get any element mapped,DomEvent::getEventDataElement(String expression)Follow up for #11992, #3356