Skip to content

Commit

Permalink
feat: allow using live directive in LitRenderer (#6068)
Browse files Browse the repository at this point in the history
* feat: allow using `live` directive in LitRenderer

Inject the `live` directive into the closure where the render is executed.
It's beneficial for cases where components can have their state changed by users to reset their value when the render is called.
  • Loading branch information
DiegoCardoso committed Feb 20, 2024
1 parent f05d103 commit deb0639
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public LitRendererPage() {
setSimpleLitRendererButton.setId("setSimpleLitRendererButton");
add(setSimpleLitRendererButton);

NativeButton setCheckboxRenderer = new NativeButton(
"Set LitRenderer with checkbox", e -> {
component.setRenderer(LitRenderer.<String> of(
"<input type='checkbox' .checked='${live(item.checked)}'>")
.withProperty("checked", "2"::equals));
});
setCheckboxRenderer.setId("setCheckboxRenderer");
add(setCheckboxRenderer);

NativeButton removeRendererButton = new NativeButton("Remove renderer",
e -> component.setRenderer(null));
removeRendererButton.setId("removeRendererButton");
Expand Down Expand Up @@ -86,7 +95,6 @@ public LitRendererPage() {
});
toggleAttachedButton.setId("toggleAttachedButton");
add(toggleAttachedButton);

}

private void setLitRenderer(LitRendererTestComponent component) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ public void shouldSupportRendererInstanceSpecificProperties() {
Assert.assertEquals("Details: 0 (details)", details.getText());
}

@Test
public void shouldBeAbleToUseLiveDirective() {
clickElementWithJs("setCheckboxRenderer");
WebElement checkbox = findElement(By.cssSelector("#item-0 input"));
checkbox.click();
clickElementWithJs("longRefreshButton");
checkbox = findElement(By.cssSelector("#item-0 input"));
Assert.assertFalse(checkbox.isSelected());
}

private String getFirstClientCallableLogMessage() {
return getLogEntries(Level.WARNING).stream()
// Discard all but event messages
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-restricted-syntax */
/* eslint-disable max-params */
import { html, render } from 'lit';
import { live } from 'lit/directives/live.js';

type RenderRoot = HTMLElement & { __litRenderer?: Renderer; _$litPart$?: any };

Expand Down Expand Up @@ -39,7 +40,7 @@ _window.Vaadin.setLitRenderer = (
const renderFunction = Function(`
"use strict";
const [render, html, returnChannel] = arguments;
const [render, html, live, returnChannel] = arguments;
return (root, model, itemKey) => {
const { item, index } = model;
Expand All @@ -57,7 +58,7 @@ _window.Vaadin.setLitRenderer = (
render(html\`${templateExpression}\`, root)
}
`)(render, html, returnChannel);
`)(render, html, live, returnChannel);

const renderer: Renderer = (root, _, model) => {
const { item } = model;
Expand Down

0 comments on commit deb0639

Please sign in to comment.