com.vaadin.flow.component.

Class ComponentEffect

java.lang.Object
com.vaadin.flow.component.ComponentEffect
public final class ComponentEffect extends Object

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 Details

    • effect

      public static <C extends Component> Registration effect(C owner, Runnable effectFunction)

      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 component

      Parameters:

      owner - the owner component for which the effect is applied, must not be null

      effectFunction - the effect function to be executed when any dependency is changed, must not be null

      Returns:

      a Registration that can be used to remove the effect function

      See 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 in setter 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 component

      T - the type of the signal value

      Parameters:

      owner - the owner component for which the effect is applied, must not be null

      signal - the signal whose value is to be bound to the component, must not be null

      setter - the setter function that defines how the signal value is applied to the component, must not be null

      Returns:

      a Registration that can be used to remove the effect function

      See 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 component

      Parameters:

      owner - the owner component for which the effect is applied, must not be null

      setter - the setter function that defines how the formatted string is applied to the component, must not be null

      locale - the locale to be used for formatting the string, if null, then no localization is applied

      format - the format string to be used for formatting the signal values, must not be null

      signals - the signals whose values are to be used for formatting the string, must not be null

      Returns:

      a Registration that can be used to remove the effect function

      See 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 component

      Parameters:

      owner - the owner component for which the effect is applied, must not be null

      setter - the setter function that defines how the formatted string is applied to the component, must not be null

      format - the format string to be used for formatting the signal values, must not be null

      signals - the signals whose values are to be used for formatting the string, must not be null

      Returns:

      a Registration that can be used to remove the effect function

      See Also: