Class LitRenderer<SOURCE>

java.lang.Object
com.vaadin.flow.data.renderer.Renderer<SOURCE>
com.vaadin.flow.data.renderer.LitRenderer<SOURCE>
Type Parameters:
SOURCE - the type of the model object used inside the template expression
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
ComponentRenderer

@JsModule("./lit-renderer.ts") public class LitRenderer<SOURCE> extends Renderer<SOURCE>
LitRenderer is a Renderer that uses a Lit-based template literal to render given model objects in the components that support the JS renderer functions API. Mainly it's intended for use with Grid, ComboBox and VirtualList, but is not limited to these.
Since:
22.0.
Author:
Vaadin Ltd
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the declared template used to render items, or null to render with the string of getTemplateExpression().
    protected String
    Returns the Lit template expression used to render items.
    Map<String,com.vaadin.flow.function.ValueProvider<SOURCE,?>>
    Gets the property mapped to ValueProviders in this renderer.
    static <SOURCE> LitRenderer<SOURCE>
    Creates a new LitRenderer based on a template declared by a method of a JsSnippets interface.
    static <SOURCE> LitRenderer<SOURCE>
    of(String templateExpression)
    Creates a new LitRenderer based on the provided template expression.
    render(com.vaadin.flow.dom.Element container, com.vaadin.flow.data.provider.DataKeyMapper<SOURCE> keyMapper, String rendererName)
    Registers a renderer function with the given name to the given container element.
    withClientAction(String name, com.vaadin.flow.component.trigger.internal.Action action)
    Adds a client-side action that can be called from within the template expression, for affordances that must run inside the browser's own event handler — copying to the clipboard, sharing, entering fullscreen — where a function cannot work because it round-trips to the server and the user gesture is gone by the time the server responds.
    withFunction(String functionName, com.vaadin.flow.function.SerializableBiConsumer<SOURCE,tools.jackson.databind.node.ArrayNode> handler)
    Adds a function that can be called from within the template expression.
    withFunction(String functionName, com.vaadin.flow.function.SerializableConsumer<SOURCE> handler)
    Adds a function that can be called from within the template expression.
    withProperty(String property, com.vaadin.flow.function.ValueProvider<SOURCE,?> provider)
    Makes a property available to the template expression.

    Methods inherited from class com.vaadin.flow.data.renderer.Renderer

    render

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • of

      public static <SOURCE> LitRenderer<SOURCE> of(String templateExpression)
      Creates a new LitRenderer based on the provided template expression. The expression accepts content that is allowed inside JS template literals, and works with the Lit data binding syntax.

      The template expression has access to:

      Examples:

       
       // Prints the `name` property of a person
       LitRenderer.<Person> of("<div>Name: ${item.name}</div>")
               .withProperty("name", Person::getName);
      
       // Prints the index of the item inside a repeating list
       LitRenderer.of("${index}");
       
       
      Type Parameters:
      SOURCE - the type of the input object used inside the template
      Parameters:
      templateExpression - the template expression used to render items, not null
      Returns:
      an initialized LitRenderer
      See Also:
    • of

      public static <SOURCE> LitRenderer<SOURCE> of(CompiledLitTemplate template)
      Creates a new LitRenderer based on a template declared by a method of a JsSnippets interface. The build collects the template into the bundle, so it works with a content security policy that does not allow unsafe-eval.

      The template is the body of a function that returns a Lit template. See CompiledLitTemplate for the variables it has access to.

      Example:

       @JsDefinition
       interface PersonTemplates extends JsSnippets {
      
           @JsExpression("return html`<div>Name: ${item.name}</div>`;")
           CompiledLitTemplate name();
       }
      
       LitRenderer.<Person> of(JsSnippets.of(PersonTemplates.class).name())
               .withProperty("name", Person::getName);
       
      Type Parameters:
      SOURCE - the type of the input object used inside the template
      Parameters:
      template - the template, returned by a method of a JsSnippets interface called on the object that JsSnippets.of(Class) returns, not null
      Returns:
      an initialized LitRenderer
      Since:
      25.4
      See Also:
    • render

      public Rendering<SOURCE> render(com.vaadin.flow.dom.Element container, com.vaadin.flow.data.provider.DataKeyMapper<SOURCE> keyMapper, String rendererName)
      Description copied from class: Renderer
      Registers a renderer function with the given name to the given container element. Creates the setup to handle rendering of individual data items as requested by the renderer function invocation.
      Specified by:
      render in class Renderer<SOURCE>
      Parameters:
      container - the element which accepts the renderer function on the client.
      keyMapper - mapper used internally to fetch items by key and to provide keys for given items.
      rendererName - name of the renderer function the container element accepts
      Returns:
      the context of the rendering, that can be used by the components to provide extra customization
    • getTemplateExpression

      protected String getTemplateExpression()
      Returns the Lit template expression used to render items.
      Returns:
      the template expression
      Since:
      24.0
    • getTemplate

      protected CompiledLitTemplate getTemplate()
      Returns the declared template used to render items, or null to render with the string of getTemplateExpression().

      A subclass that overrides getTemplateExpression() without overriding this method renders with that string, so that existing subclasses of the built-in renderers keep working.

      Returns:
      the declared template, or null if there is none
      Since:
      25.4
    • withProperty

      public LitRenderer<SOURCE> withProperty(String property, com.vaadin.flow.function.ValueProvider<SOURCE,?> provider)
      Makes a property available to the template expression. Each property is referenced inside the template by using the ${item.property} syntax.

      Examples:

       
       // Regular property
       LitRenderer.<Person> of("<div>Name: ${item.name}</div>")
               .withProperty("name", Person::getName);
      
       // Property that uses a bean. Note that in this case the entire "Address"
       // object will be sent to the template.
       // Note that even properties of the bean which are not used in the
       // template are sent to the client, so use
       // this feature with caution.
       LitRenderer.<Person> of("<span>Street: ${item.address.street}</span>")
               .withProperty("address", Person::getAddress);
      
       // In this case only the street field inside the Address object is sent
       LitRenderer.<Person> of("<span>Street: ${item.street}</span>")
               .withProperty("street",
                       person -> person.getAddress().getStreet());
       
       
      Any Jackson types supported are valid for LitRenderer.
      Parameters:
      property - the name of the property used inside the template expression, not null
      provider - a ValueProvider that provides the actual value for the property, not null
      Returns:
      this instance for method chaining
    • withFunction

      public LitRenderer<SOURCE> withFunction(String functionName, com.vaadin.flow.function.SerializableConsumer<SOURCE> handler)
      Adds a function that can be called from within the template expression.

      Examples:

       
       // Standard event
       LitRenderer.of("<button @click=${handleClick}>Click me</button>")
               .withFunction("handleClick", object -> doSomething());
       
       
      The name of the function used in the template expression should be the name used at the functionName parameter. This name must be a valid JavaScript function name.
      Parameters:
      functionName - the name of the function used inside the template expression, must be alphanumeric and not null, must not be one of the JavaScript reserved words (https://www.w3schools.com/js/js_reserved.asp)
      handler - the handler executed when the function is called, not null
      Returns:
      this instance for method chaining
      See Also:
    • withFunction

      public LitRenderer<SOURCE> withFunction(String functionName, com.vaadin.flow.function.SerializableBiConsumer<SOURCE,tools.jackson.databind.node.ArrayNode> handler)
      Adds a function that can be called from within the template expression. The function accepts arguments that can be consumed by the given handler.

      Examples:

       
       // Standard event
       LitRenderer.of("<button @click=${handleClick}>Click me</button>")
               .withFunction("handleClick", item -> doSomething());
      
       // Function invocation with arguments
       LitRenderer.of("<input @keypress=${(e) => handleKeyPress(e.key)}>")
               .withFunction("handleKeyPress", (item, args) -> {
                   System.out.println("Pressed key: " + args.getString(0));
               });
       
       
      The name of the function used in the template expression should be the name used at the functionName parameter. This name must be a valid Javascript function name.
      Parameters:
      functionName - the name of the function used inside the template expression, must be alphanumeric and not null, must not be one of the JavaScript reserved words (https://www.w3schools.com/js/js_reserved.asp)
      handler - the handler executed when the function is called, not null
      Returns:
      this instance for method chaining
      See Also:
    • withClientAction

      public LitRenderer<SOURCE> withClientAction(String name, com.vaadin.flow.component.trigger.internal.Action action)
      Adds a client-side action that can be called from within the template expression, for affordances that must run inside the browser's own event handler — copying to the clipboard, sharing, entering fullscreen — where a function cannot work because it round-trips to the server and the user gesture is gone by the time the server responds.

      The action is bound once for the whole renderer, however many items it renders; the template passes it the values it works on:

       
       LitRenderer.<Person> of(
               "<span>${item.email}</span><button @click=${() => copy(item.email)}>Copy</button>")
               .withProperty("email", Person::getEmail)
               .withClientAction("copy", Clipboard.writeText());
       
       
      Parameters:
      name - the name of the action used inside the template expression, must be alphanumeric and not null
      action - the action to run on the client, not null
      Returns:
      this instance for method chaining
    • getValueProviders

      public Map<String,com.vaadin.flow.function.ValueProvider<SOURCE,?>> getValueProviders()
      Gets the property mapped to ValueProviders in this renderer. The returned map is immutable.
      Returns:
      the mapped properties, never null
      Since:
      23.0.10