Class LiteRenderer<SOURCE>

java.lang.Object
com.vaadin.flow.data.renderer.Renderer<SOURCE>
com.flowingcode.vaadin.addons.litetemplate.LiteRenderer<SOURCE>
Type Parameters:
SOURCE - the type of the model object used in the template
All Implemented Interfaces:
Serializable

public class LiteRenderer<SOURCE> extends Renderer<SOURCE>
Renderer that uses a component instance as template. Intended as an alternative of LitRenderer.
Author:
Javier Godoy
See Also:
  • Method Details

    • toString

      public String toString()
      Returns a string representation of the object.
      Overrides:
      toString in class Object
    • render

      public Rendering<SOURCE> render(Element container, DataKeyMapper<SOURCE> keyMapper, String rendererName)
      Specified by:
      render in class Renderer<SOURCE>
    • of

      public static <SOURCE> LiteRenderer<SOURCE> of(Component templateComponent, Component... moreTemplateComponents)
      Creates a new LiteRenderer using the specified template components. The renderer will automatically reflect any updates made to the components, up until the first render.

      ${placeholder} expressions within attribute and property values will be interpolated, allowing them to dynamically reflect the same properties accessible to LitRenderer. Additionally, dynamic attributes and properties will be replaced with generated placeholders. Example:

       
       LiteRenderer.<Person>of(new Div("Name: ${item.name}")).withProperty("name", Person::getName);
       
       
      Type Parameters:
      SOURCE - the type of the input object used inside the template
      Parameters:
      templateComponent - the first template components used to render items. Must not be null.
      moreTemplateComponents - additional template components used to render items. Must not be null.
      Returns:
      a LiteRenderer configured with the given template component.
      Throws:
      NullPointerException - if any template component is null
      See Also:
    • disableInterpolation

      public LiteRenderer<SOURCE> disableInterpolation()
      Disable interpolation of template properties within the attributes and properties of the template components and their children.

      To enable interpolation on a particular child, use enableInterpolation(Component) on that child.

      Returns:
      this instance for method chaining
      See Also:
    • disableInterpolation

      public LiteRenderer<SOURCE> disableInterpolation(Component component)
      Disable interpolation of template properties within the attributes and properties of the specified component and its children.

      To enable interpolation on a particular child, use enableInterpolation(Component) on that child.

      See Also:
    • enableInterpolation

      public LiteRenderer<SOURCE> enableInterpolation(Component component)
      Enable interpolation of template properties within the attributes and properties of the specified component and its children.

      To enable interpolation on a particular child, use disableInterpolation(Component) on that child.

      See Also:
    • withProperty

      public LiteRenderer<SOURCE> withProperty(@NonNull @NonNull String property, @NonNull @NonNull ValueProvider<SOURCE,?> provider)
      Makes a Lit property available to the template component. Each property is referenced inside attributes or properties of the template by using the ${item.property} syntax.

      Example:

       
       LiteRenderer.<Person>of(new Div("Name: ${item.name}"))
                   .withProperty("name", Person::getName);
       
       
      Any types supported by LitRenderer are valid types for LiteRenderer.
      Parameters:
      property - the name of the property used inside the template expression. Must not be null.
      provider - a ValueProvider that provides the actual value for the property. Must not be null.
      Returns:
      this instance for method chaining
      See Also:
    • withProperty

      public LiteRenderer<SOURCE> withProperty(@NonNull @NonNull Component component, @NonNull @NonNull String name, @NonNull @NonNull ValueProvider<SOURCE,?> valueProvider)
      Binds a dynamic property with a specified component inside the template. The value of the property is provided by a ValueProvider and will be updated based on the input object.

      This method invalidates any previous renderer to reflect the new property.

      Parameters:
      component - the component within the template to which the property is being bound. Must not be null.
      name - the name of the property being bound. Must not be null.
      valueProvider - a ValueProvider that supplies the value of the property from the input object. Must not be null.
      Returns:
      this instance for method chaining
      Throws:
      IllegalArgumentException - if the provided component is not part of the template.
      See Also:
    • withAttribute

      public LiteRenderer<SOURCE> withAttribute(@NonNull @NonNull Component component, @NonNull @NonNull String attribute, @NonNull @NonNull ValueProvider<SOURCE,?> valueProvider)
      Binds a dynamic attribute to a specified component inside the template.. The value of the property is provided by a ValueProvider and will be updated based on the input object.

      This method invalidates any previous renderer to reflect the new attribute.

      Parameters:
      component - the component within the template to which the attribute is being bound. Must not be null.
      attribute - the name of the attribute being bound. Must not be null.
      valueProvider - a ValueProvider that supplies the value of the attribute from the input object. Must not be null.
      Returns:
      this instance for method chaining
      Throws:
      IllegalArgumentException - if the provided component is not part of the template.
      See Also:
    • withAttribute

      public LiteRenderer<SOURCE> withAttribute(@NonNull @NonNull Component component, @NonNull @NonNull String attribute, String value)
      Assigns a static attribute to a specified component in the template. This attribute will have a fixed value and will not dynamically update based on the input object, but it can contain ${placeholder} expressions allowing dynamic content to be inserted when rendered. Example:
      
       Div div = new Div("${item.firstName} ${item.lastName}");
       grid.addColumn(LiteRenderer.<Person>of(div)
          .withProperty("firstName", Person::firstName)
          .withProperty("lastName", Person::lastName)
          .withProperty("age", Person::age)
          .withAttribute(div, "title", "Age: ${item.age}")
       
      Parameters:
      component - the component within the template to which the attribute is being assigned. Must not be null.
      attribute - the name of the attribute being assigned. Must not be null.
      value - the static value of the attribute. Must not be null.
      Returns:
      this instance for method chaining
      Throws:
      IllegalArgumentException - if the provided component is not part of the template.
      See Also:
    • withListener

      public LiteRenderer<SOURCE> withListener(@NonNull @NonNull Component component, @NonNull @NonNull String eventType, @NonNull @NonNull SerializableBiConsumer<SOURCE,elemental.json.JsonArray> handler, String... eventArguments)
      Registers an event listener for a DOM event on one of the child elements within the template component. Each DOM event can only be registered once per component. The function accepts arguments that can be consumed by the given handler.

      Examples:

       
       LiteRenderer.of(new TextField())
                   .withListener(child, "keypress", , (item, args) -> {
             System.out.println("Pressed key: " + args.getString(0));
           }, "e.key");
       
       
      Parameters:
      component - The component whose child element will receive the event listener.
      eventType - The type of DOM event to listen for (e.g., "click", "change").
      handler - A callback function to handle the event when triggered
      eventArguments - One or more expressions for extracting event data. When an event is fired in the browser, the expression is evaluated and its value is sentback to the server. The expression is evaluated in a context where element refers to this element and event refersto the fired event. If multiple expressions are defined for the sameevent, their order of execution is undefined.
      Returns:
      this instance for method chaining
      See Also:
    • withListener

      public LiteRenderer<SOURCE> withListener(@NonNull @NonNull Component component, @NonNull @NonNull String eventType, @NonNull @NonNull SerializableConsumer<SOURCE> handler)
      Registers an event listener for a DOM event on one of the child elements within the template component. Each DOM event can only be registered once per component.
      Parameters:
      component - The component whose child element will receive the event listener.
      eventType - The type of DOM event to listen for (e.g., "click", "change").
      handler - A callback function to handle the event when triggered
      Returns:
      this instance for method chaining
      See Also:
    • getItem

      public static <T> T getItem(ComponentEvent<?> event, Class<T> type)
      Returns the item of an event fired on the template.
      Parameters:
      event - the event fired on the template component.
      type - the type of the item.
      Returns:
      the item of an event fired on the template
      Throws:
      ClassCastException - if the captured item is not assignable to type.
      IllegalStateException - if no item was captured.