Skip to content

feat: add support for @EventData of type Component - #12334

Merged
joheriks merged 4 commits into
masterfrom
fix/dom-event
Nov 18, 2021
Merged

joheriks merged 4 commits into
masterfrom
fix/dom-event

Conversation

@pleku

@pleku pleku commented Nov 11, 2021 •

Copy link
Copy Markdown

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

public MappedDomEventWithRouterLinkData(Component source,
boolean fromClient, @EventData("component") Component component,
@EventData("router.link") RouterLink routerLink) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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..)

@pleku pleku Nov 11, 2021 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

@pleku pleku Nov 11, 2021 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@joheriks
joheriks self-requested a review November 12, 2021 12:37
Comment on lines +1537 to +1544
do {
stateNodeForDomNode = stateTree
.getStateNodeForDomNode(targetNode);
targetNode = DomApi.wrap(targetNode.getParentNode());
} while (stateNodeForDomNode == null && targetNode != null);
if (stateNodeForDomNode != null) {
return stateNodeForDomNode.getId();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@joheriks

Copy link
Copy Markdown
Contributor

Two two Sonar critical issues about refactoring getClosestStateNodeIdToTarget to have less nesting and the unused import in ComponentEventBus could be addressed.

Comment thread flow-tests/test-root-context/pom.xml Resolved
@pleku
pleku requested review from caalador and joheriks November 18, 2021 09:38
@pleku

pleku commented Nov 18, 2021

Copy link
Copy Markdown
Author

Two two Sonar critical issues about refactoring getClosestStateNodeIdToTarget to have less nesting and the unused import in ComponentEventBus could be addressed.

Done

@vaadin-bot

Copy link
Copy Markdown
Collaborator

SonarQube analysis reported 7 issues

  1. CRITICAL SimpleElementBindingStrategy.java#L1507: Either log or rethrow this exception. rule
  2. CRITICAL SimpleElementBindingStrategy.java#L1547: Either log or rethrow this exception. rule
  3. MAJOR SimpleElementBindingStrategy.java#L1270: A "NullPointerException" could be thrown; "eventData" is nullable here. rule
  4. MAJOR SimpleElementBindingStrategy.java#L1284: A "NullPointerException" could be thrown; "eventData" is nullable here. rule
  5. MAJOR SimpleElementBindingStrategy.java#L1292: A "NullPointerException" could be thrown; "eventData" is nullable here. rule
  6. MAJOR SimpleElementBindingStrategy.java#L1514: This block of commented-out lines of code should be removed. rule
  7. MAJOR SimpleElementBindingStrategy.java#L1556: This block of commented-out lines of code should be removed. rule

@joheriks
joheriks enabled auto-merge (squash) November 18, 2021 10:02
@joheriks
joheriks merged commit 29d99a8 into master Nov 18, 2021
@joheriks
joheriks deleted the fix/dom-event branch November 18, 2021 10:34
pleku pushed a commit that referenced this pull request Nov 19, 2021
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
pleku pushed a commit that referenced this pull request Nov 19, 2021
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
@pleku

pleku commented Nov 19, 2021

Copy link
Copy Markdown
Author

Cherry-picked to 2.8

@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with platform 22.0.0.rc1 and is also targeting the upcoming stable 22.0.0 version.

pleku pushed a commit that referenced this pull request Nov 29, 2021
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
@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with Vaadin 14.9.0.alpha1 and is also targeting the upcoming stable 14.9.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants