vaadin / flow

Vaadin Flow is a Java framework binding Vaadin web components to Java. This is part of Vaadin 10+.
Apache License 2.0
618 stars 167 forks source link

Synchronizing values along with a shortcutlistener #7046

Open OlliTietavainenVaadin opened 4 years ago

OlliTietavainenVaadin commented 4 years ago

Vaadin 14.0

In some components, the value of the component might not get updated when a shortcut listener is triggered. For example, there could be a shortcut Ctrl+S for saving a form. If you have a TextField focused with some text entered and the shortcut is pressed, the TextField's new value is not available on the server, while this would be the expected behavior in many cases.

Legioth commented 4 years ago

Most affected components are probably using a synchronized element property for propagating the value back to the server. We could in theory force a sync of such properties (regardless of debounce or throttle settings) before the key event is sent to the server.

Some open questions for that approach:

  1. Should this forced sync be done for all elements or only the focused one? It would be necessary to also sync properties for all elements in the shadow root chain of activeElement instances so that a sync would be triggered for e.g. <vaadin-text-field> even though that element is inside a shadow root and the actually focused element is the <input> inside the text field's shadow root.
  2. Should this forced sync be done for any round trip, or only for keyboard shortcuts? If it would be selective, then we'd need an API for opting in. This could either be a JS method to call (e.g. by abusing an event data expression) or a toggle on DomListenerRegistration.

There may also be components that sync their value e.g. as event details for a DOM event or through @ClientCallable. Such cases would most likely require some coordination with the component's connector logic. One potential approach would be that the connector can register a client-side listener that is run when this kind of synchronization should be triggered and then do its magic in that one.

mehdi-vaadin commented 4 years ago

It can be simply reproduced with the following piece of code.

TextField textField = new TextField();
add(textField);
Button button = new Button("Click", event -> System.out.println(textField.getValue()));
button.addClickShortcut(Key.KEY_S, KeyModifier.CONTROL);
add(button);

When the text field is focused, pressing Ctrl+S doesn't print its current value.

mshabarov commented 1 year ago

Cross-posting: same issue with a workaround https://github.com/vaadin/flow/issues/17484.