com.vaadin.flow.component.
Class ComponentEffect
The utility class that provides helper methods for using Signal effects in a context of a given component's life-cycle.
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 settng function and format strings based on signal values.
Since:
24.8
-
Method Summary
Modifier and TypeMethodDescriptionstatic <C extends Component,
T>
Registrationbind
(C owner, Signal<T> signal, SerializableBiConsumer<C, T> setter) Binds a
signal
's value to a given owner component in a way defined insetter
function and creates a Signal effect function executing the setter whenever the signal value changes.static <C extends Component>
RegistrationCreates a Signal effect that is owned by a given component.
static <C extends Component>
Registrationformat
(C owner, SerializableBiConsumer<C, String> setter, String format, Signal<?>... signals) Formats a string using the values of the provided signals and sets it on the owner component using the provided setter function.
static <C extends Component>
Registrationformat
(C owner, SerializableBiConsumer<C, String> setter, Locale locale, String format, Signal<?>... signals) Formats a string using the values of the provided signals and the given locale, sets the formatted string on the owner component using the provided setter function.
-
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.
Examle 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 componentParameters:
owner
- the owner component for which the effect is applied, must not benull
effectFunction
- the effect function to be executed when any dependency is changed, must not benull
Returns:
a
Registration
that can be used to remove the effect functionSee Also:
-
bind
public static <C extends Component,T> Registration bind(C owner, Signal<T> signal, SerializableBiConsumer<C, T> setter) Binds a
signal
's value to a given owner component in a way defined insetter
function 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 valueParameters:
owner
- the owner component for which the effect is applied, must not benull
signal
- the signal whose value is to be bound to the component, must not benull
setter
- the setter function that defines how the signal value is applied to the component, must not benull
Returns:
a
Registration
that can be used to remove the effect functionSee Also:
-
format
public static <C extends Component> Registration format(C owner, SerializableBiConsumer<C, String> setter, Locale locale, String format, Signal<?>... signals) Formats a string using the values of the provided signals and the given locale, sets the formatted string on the owner component using the provided setter function.
Binds a formatted string using the values of the provided signals to a given owner component in a way defined in
setter
function and creates a Signal effect function executing the setter whenever the signal value changes.Example of usage:
ComponentEffect.format(mySpan, Span::setText, Locale.US, "The price of %s is %.2f", nameSignal, priceSignal);
Type Parameters:
C
- the type of the componentParameters:
owner
- the owner component for which the effect is applied, must not benull
setter
- the setter function that defines how the formatted string is applied to the component, must not benull
locale
- the locale to be used for formatting the string, ifnull
, then no localization is appliedformat
- the format string to be used for formatting the signal values, must not benull
signals
- the signals whose values are to be used for formatting the string, must not benull
Returns:
a
Registration
that can be used to remove the effect functionSee Also:
-
format
public static <C extends Component> Registration format(C owner, SerializableBiConsumer<C, String> setter, String format, Signal<?>... signals) Formats a string using the values of the provided signals and sets it on the owner component using the provided setter function.
Binds a formatted string using the values of the provided signals to a given owner component in a way defined in
setter
function and creates a Signal effect function executing the setter whenever the signal value changes.Formats using locale from the current UI, I18NProvider or default locale depending on what is available.
Example of usage:
ComponentEffect.format(mySpan, Span::setText, "The price of %s is %.2f", nameSignal, priceSignal);
Type Parameters:
C
- the type of the componentParameters:
owner
- the owner component for which the effect is applied, must not benull
setter
- the setter function that defines how the formatted string is applied to the component, must not benull
format
- the format string to be used for formatting the signal values, must not benull
signals
- the signals whose values are to be used for formatting the string, must not benull
Returns:
a
Registration
that can be used to remove the effect functionSee Also:
-