Class ComponentEffect
It ultimately creates a Signal effect, i.e. a call to
Signal.effect(Runnable), that is automatically enabled when a
component is attached and disabled when the component is detached.
Additionally it provides methods to bind signals to component according to a
given value setting function.
- Since:
- 24.8
-
Method Summary
Modifier and TypeMethodDescriptionstatic <C extends Component,T>
Registrationbind(C owner, com.vaadin.signals.Signal<T> signal, SerializableBiConsumer<C, T> setter) Binds asignal's value to a given owner component in a way defined insetterfunction and creates a Signal effect function executing the setter whenever the signal value changes.static <T,PARENT extends Component & HasComponents>
voidbindChildren(PARENT parent, com.vaadin.signals.ListSignal<T> list, SerializableFunction<com.vaadin.signals.ValueSignal<T>, Component> childFactory) Binds aListSignalto a parent component using a child component factory.static <C extends Component>
RegistrationCreates a Signal effect that is owned by a given component.
-
Method Details
-
effect
Creates a Signal effect that is owned by a given component. The effect is enabled when the component is attached and automatically disabled when it is detached.Example of usage:
Registration effect = ComponentEffect.effect(myComponent, () -> { Notification.show("Component is attached and signal value is " + someSignal.value()); }); effect.remove(); // to remove the effect when no longer needed- Type Parameters:
C- the type of the component- Parameters:
owner- the owner component for which the effect is applied, must not benulleffectFunction- the effect function to be executed when any dependency is changed, must not benull- Returns:
- a
Registrationthat can be used to remove the effect function - See Also:
-
bind
public static <C extends Component,T> Registration bind(C owner, com.vaadin.signals.Signal<T> signal, SerializableBiConsumer<C, T> setter) Binds asignal's value to a given owner component in a way defined insetterfunction and creates a Signal effect function executing the setter whenever the signal value changes.Example of usage:
Registration effect = ComponentEffect.bind(mySpan, stringSignal, Span::setText); effect.remove(); // to remove the effect when no longer needed ComponentEffect.bind(mySpan, stringSignal.map(value -> !value.isEmpty()), Span::setVisible);- Type Parameters:
C- the type of the componentT- the type of the signal value- Parameters:
owner- the owner component for which the effect is applied, must not benullsignal- the signal whose value is to be bound to the component, must not benullsetter- the setter function that defines how the signal value is applied to the component, must not benull- Returns:
- a
Registrationthat can be used to remove the effect function - See Also:
-
bindChildren
public static <T,PARENT extends Component & HasComponents> void bindChildren(PARENT parent, com.vaadin.signals.ListSignal<T> list, SerializableFunction<com.vaadin.signals.ValueSignal<T>, Component> childFactory) Binds aListSignalto a parent component using a child component factory. EachValueSignalin the list corresponds to a child component within the parent.The parent component is automatically updated to reflect the structure of the
ListSignal. Changes to the list, such as additions, removals, or reordering, will update the parent's children accordingly.The parent component must not contain any children that are not part of the
ListSignal. If the parent has existing children when this method is called, or if it contains unrelated children after the list changes, anIllegalStateExceptionwill be thrown.New child components are created using the provided
childFactoryfunction. This function takes aValueSignalfrom theListSignaland returns a correspondingComponent. It shouldn't returnnull. TheValueSignalcan be further bound to the returned component as needed. Note thatchildFactoryis run inside aEffect, and thereforeAbstractSignal.value()calls makes effect re-run automatically on signal value change.Example of usage:
ListSignal<String> taskList = new ListSignal<>(String.class); UnorderedList component = new UnorderedList(); ComponentEffect.bindChildren(component, taskList, taskValueSignal -> { var listItem = new ListItem(); ComponentEffect.bind(listItem, taskValueSignal, HasString::setText); return listItem; });- Type Parameters:
T- the value type of theValueSignals in theListSignalPARENT- the type of the parent component- Parameters:
parent- target parent component, must not benulllist- list signal to bind to the parent, must not benullchildFactory- factory to create new component, must not benull- Throws:
IllegalStateException- thrown if parent component isn't empty
-