2

As the LoginForm is no longer present in Vaadin 7, what is the best approach to handle a basic login page consisting of a single username/password input and a "login" button.

How can I make sure that when hitting the return key the form is submitted? I can think of ways of doing this, but figured there must be a more common approach.

mgrstnr
  • 490
  • 1
  • 5
  • 23

2 Answers2

9

One simple way would be to use the LoginForm add on. https://vaadin.com/directory#addon/loginform

The other way would be to just display a form with login&password fields and set the Login button as the default action.

In Vaadin 7:

// Have an Login button and set it as the default button
Button login = new Button("Login");
login.setClickShortcut(ShortcutAction.KeyCode.ENTER);  // Bind ENTER key to this button.
login.addStyleName(Reindeer.BUTTON_DEFAULT);           // Add styling as visual cue this button is the default button (has Enter key bound to it). 

Vaadin 8 uses a different theme by default, Valo instead of Reindeer, using a different style name:

// Have an Login button and set it as the default button
Button login = new Button("Login");
login.setClickShortcut(ShortcutAction.KeyCode.ENTER); // Bind ENTER key to this button.
login.addStyleName(ValoTheme.BUTTON_PRIMARY);         // Add styling as visual cue this button is the default button (has Enter key bound to it). 

See documentation on Shortcut Keys for Vaadin 7 and for Vaadin 8. Note that some web browsers may not support some keyboard shortcuts.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
André Schild
  • 4,592
  • 5
  • 28
  • 42
3

Vaadin 13+

In Vaadin Flow versions 13 and later, use Button::addClickShortcut. The method in defined in the interface com.vaadin.flow.component.ClickNotifier.

This addClickShortcut method replaces the setClickShortcut (add vs set) in Vaadin 7 & 8 as seen in other correct Answer.

Button login = new Button("Login");  
login.addClickListener(click -> {
    //do your things here 
});       
login.addClickShortcut(Key.ENTER);  //  Bind keyboard shortcut.

This code binds your ENTER key to the event listener.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Sox -
  • 592
  • 1
  • 9
  • 28