title | order | layout |
---|---|---|
Component Enabled State |
3 |
page |
Components that allow user interaction, such as TextField
or Button
, can have three different enabled states:
-
Enabled: An enabled component allows the user to interact with it. This is the default state.
-
Explicitly disabled: A component is explicitly disabled when
setEnabled(false)
is called directly on it. The user cannot interact with the component, and communication from the client to the server is blocked. -
Implicitly disabled: A component is implicitly disabled when it is a child of a explicitly disabled container. The component behaves exactly like an explicitly disabled component, except it is automatically enabled again as soon as it detaches from the disabled container.
Any component that implements the HasEnabled
interface can be explicitly enabled or disabled.
Example: Disabling a component using the component.setEnabled
method.
TextField name = new TextField("Name");
name.setEnabled(false);
-
This disables the
name
field:-
Users cannot interact with it, and
-
Events from the client to the server are blocked.
-
-
The server does not handle status updates from the component, even if the component is changed manually on the browser, for example by a client-side script or via a developer console.
Example: Disabling all components in a container by using the same API:
FormLayout form = new FormLayout();
TextField name = new TextField("Name");
TextField email = new TextField("E-mail");
Button submit = new Button("Submit");
form.add(name, email, submit);
// all children are implicitly disabled
form.setEnabled(false);
System.out.println(name.isEnabled()); // prints false
Note
|
Server-side components mapped via @Id do not inherit the template element hierarchy.
As a consequence, disabling a mapped component does not disable other mapped components
whose elements are children of the disabled component’s element in the template.
See Mapped Components Limitations for details.
|
Note
|
LitTemplate doesn’t support disabled attribute on @Id mapped components.
See Mapped Components Limitations for details.
|
When an implicitly disabled component is detached from a disabled container, it is automatically enabled again. Similarly, if an enabled component is attached to a disabled container, it is automatically implicitly disabled.
Examples: Implicitly enabled and disabled components
FormLayout form = new FormLayout();
form.setEnabled(false); // the entire form is disabled
TextField name = new TextField("Name");
// prints true, since it is not attached yet
System.out.println(name.isEnabled());
Button submit = new Button("Submit");
// the submit button is explicitly disabled
submit.setEnabled(false);
System.out.println(submit.isEnabled()); // prints false
form.add(name, submit); // attaches children
System.out.println(name.isEnabled()); // prints false
System.out.println(submit.isEnabled()); // prints false
form.remove(name); // the name field gets detached
System.out.println(name.isEnabled()); // prints true
form.remove(submit); // the submit button gets detached
// prints false, since it was explicitly disabled
System.out.println(submit.isEnabled());
By default, disabled components do not allow user interaction from the client side. However, it is sometimes necessary for complex (composite) components to remain partially functional, even in the disabled state. For example, you may want to only fully enable a registration form after a user selects a checkbox to accept a license agreement.
You can override the default disabled behavior by enabling certain RPC-client-side calls that would normally be blocked for disabled components.
The first way to do this is by providing the the property tha should be synchronized to the server in the addPropertyChangeListener
call.
Example: This Polymer template component controls its own enabled state via the checkbox. The checkbox is never disabled, and it enables and disables the component.
@Tag("registration-form")
@JsModule("./src/registration-form.js")
public class RegistrationForm
extends PolymerTemplate<TemplateModel>
implements HasEnabled {
@Id
private TextField name;
@Id
private TextField email;
@Id
private Button submit;
@Id
private Element enable;
public RegistrationForm() {
enable.addPropertyChangeListener("checked", "checked-changed",
this::handleEnabled);
setEnabled(false);
}
private void handleEnabled(
PropertyChangeEvent event) {
setEnabled((Boolean) event.getValue());
}
@EventHandler
private void register() {
String userName = name.getValue();
String userEmail = email.getValue();
System.out.println("Register user with name='"
+ userName
+ "' and email='" + userEmail + "'");
}
}
Here is its template file:
class RegistrationForm extends PolymerElement {
static get template() {
return html`
<vaadin-text-field id='name'>
{{name}}
</vaadin-text-field>
<vaadin-text-field id='email'>
{{email}}
</vaadin-text-field>
<vaadin-button id='submit'
on-click='register'>
Register
</vaadin-button>
<vaadin-checkbox id='enable'>
Accept License Agreement
</vaadin-checkbox>`;
}
static get is() {
return 'registration-form';
}
}
customElements.define(RegistrationForm.is,
RegistrationForm);
-
The checkbox is implicitly disabled if the template (which is its parent) is disabled. As a result, no RPC is allowed for the checkbox.
-
The
addPropertyChangeListener
method (with the extra "checked-changed" argument) is used to synchronize thechecked
property. -
The folowing RPC communications are blocked for the disabled element:
-
Property changes.
-
DOM events.
-
Event handler methods (annotated with
@EventHandler
). For example, theregister()
method is an event handler method that is blocked when the component is disabled. -
Client delegate methods (annotated with
@ClientCallable
).
-
As an alternative, you can use the @Synchronize
annotation with the DisabledUpdateMode.ALWAYS
argument value.
Example: Using the @Synchronize
annotation for the property getter in your component.
@Synchronize(property = "prop", value = "prop-changed",
allowUpdates = DisabledUpdateMode.ALWAYS)
public String getProp() {
return getElement().getProperty("prop");
}
There are two ways to enable DOM events. You can use:
-
An
addEventListener
overload method in theElement
API, or -
The
@DomEvent
annotation.
Example: Unblocking a DOM event for a disabled element using the addEventListener
overload method that accepts the DisabledUpdateMode.ALWAYS
parameter.
public Notification() {
getElement().addEventListener("opened-changed",
event -> System.out.println("Opened"))
.setDisabledUpdateMode(DisabledUpdateMode.ALWAYS);
}
Example: Unblocking a DOM event for a disabled component using the @DomEvent
annotation with the parameter value allowUpdates = DisabledUpdateMode.ALWAYS )
:
@DomEvent(value = "click",
allowUpdates = DisabledUpdateMode.ALWAYS)
public class CustomEvent
extends ComponentEvent<Component> {
}
If there are server-handler methods annotated with @ClientCallable
or @EventHandler
, you can unblock them for disabled components by specifying DisabledUpdateMode.ALWAYS
as a value.
Example: Specifying DisabledUpdateMode.ALWAYS
@EventHandler(DisabledUpdateMode.ALWAYS)
private void eventHandler() {
}
@ClientCallable(DisabledUpdateMode.ALWAYS)
private void clientRequest() {
}