I have a view containing a TextArea, which when a user presses ‘Enter’ will submit the contents to the server.
My server is hosted in the US and European users see the Loading Indicator at the top of the screen as they type - which is not the best user experience.
I debugged using Chrome Dev Tools and on each key press, the client is sending a Sync RPC call to the server. The round-trip of each of these calls can be 300ms+ from Europe, resulting in the loading indicator.
These sync calls happen when using TextArea addKeyPressListener, addKeyUpListener and addKeyDownListener.
They do not happen when using Shortcuts.addShortcutListener( textArea, this::onSubmit, Key.ENTER ).listenOn( textField )
However, onSubmit then does not have the latest value of the TextArea, unless setValueChangeMode( ValueChangeMode.EAGER ) is set.
If this happens - every key press results in the sync event being sent to the server and the loading indicator issue.
I noted the MessageInput component has the desired behavior - no sync on key press, Enter submits the latest value. However, that component is not suitable for my requirements.
Looking at the Message Input source, it looks like this behavior is handled in JavaScript:
this._textAreaController = new SlotController(this, 'textarea', 'vaadin-text-area', {
initializer: (textarea) => {
textarea.addEventListener('value-changed', (event) => {
this.value = event.detail.value;
});
textarea.addEventListener('keydown', (event) => {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
event.stopImmediatePropagation();
this.__submit();
}
});
textarea.minRows = 1;
this._textArea = textarea;
},
});
Is there a way to achieve this desired behavior with TextArea (and TextField also, as this is used elsewhere in the application)