Skip to content

Commit

Permalink
fix: do not require UI to generate property namespace (#6761) (#6776)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergey Vinogradov <[email protected]>
Co-authored-by: Zhe Sun <[email protected]>
  • Loading branch information
3 people authored Nov 4, 2024
1 parent d532832 commit f97839c
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ComponentRendererInNewThreadPage extends Div {
public ComponentRendererInNewThreadPage() {
UI.getCurrent().getPushConfiguration().setPushMode(PushMode.AUTOMATIC);

LitRendererTestComponent component = new LitRendererTestComponent();
LitRendererTestComponentWrapper component = new LitRendererTestComponentWrapper();
component.setItems("Item");

ComponentRenderer<Span, String> componentRenderer = new ComponentRenderer<Span, String>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class LitRendererPage extends Div {

public LitRendererPage() {

LitRendererTestComponent component = new LitRendererTestComponent();
LitRendererTestComponentWrapper component = new LitRendererTestComponentWrapper();
component.setItems(Arrays.asList("0", "1", "2", "3", "4"));
setLitRenderer(component);

Expand Down Expand Up @@ -97,7 +97,7 @@ public LitRendererPage() {
add(toggleAttachedButton);
}

private void setLitRenderer(LitRendererTestComponent component) {
private void setLitRenderer(LitRendererTestComponentWrapper component) {
component.setRenderer(LitRenderer.<String> of(new StringBuilder()
.append("<div tabindex=\"0\"")
.append(" id=\"content-${index}\"")
Expand All @@ -122,7 +122,8 @@ private void setLitRenderer(LitRendererTestComponent component) {
}));
}

private void setDetailsLitRenderer(LitRendererTestComponent component) {
private void setDetailsLitRenderer(
LitRendererTestComponentWrapper component) {
LitRenderer<String> renderer = LitRenderer.<String> of(
"<div id=\"details-${index}\">Details: ${item.value}</div>");
component.setDetailsRenderer(renderer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.data.renderer.tests;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.data.renderer.LitRenderer;
import com.vaadin.flow.router.Route;

@Route("vaadin-renderer-flow/lit-renderer-property-namespace")
public class LitRendererPropertyNamespacePage extends Div {
public LitRendererPropertyNamespacePage() {
// Create a wrapper component with two LitRendererTestComponents
LitRendererTestComponentWrapper wrapper = new LitRendererTestComponentWrapper(
2);
wrapper.setItems("Item 0");

NativeButton setLitRenderersButton = new NativeButton(
"Set LitRenderers", e -> {
for (int i = 0; i < wrapper.getComponentCount(); i++) {
wrapper.setRenderer(i, createLitRenderer(
"Component " + i + " : Default Renderer"));
wrapper.setDetailsRenderer(i, createLitRenderer(
"Component " + i + " : Details Renderer"));
}
});
setLitRenderersButton.setId("set-lit-renderers");

NativeButton setComponentRenderersButton = new NativeButton(
"Set ComponentRenderers", e -> {
for (int i = 0; i < wrapper.getComponentCount(); i++) {
wrapper.setRenderer(i, createComponentRenderer(
"Component " + i + " : Default Renderer"));
wrapper.setDetailsRenderer(i, createComponentRenderer(
"Component " + i + " : Details Renderer"));
}
});
setComponentRenderersButton.setId("set-component-renderers");

NativeButton attachButton = new NativeButton("Attach", e -> {
add(wrapper);
});
attachButton.setId("attach");

add(setLitRenderersButton, setComponentRenderersButton, attachButton);
}

private LitRenderer<String> createLitRenderer(String prefix) {
LitRenderer<String> renderer = LitRenderer
.of("<span>${item.content}</span>");
renderer.withProperty("content", item -> prefix + " : " + item);
return renderer;
}

private ComponentRenderer<Span, String> createComponentRenderer(
String prefix) {
return new ComponentRenderer<>(item -> new Span(prefix + " : " + item));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,67 +16,30 @@
package com.vaadin.flow.data.renderer.tests;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.data.binder.HasDataProvider;
import com.vaadin.flow.data.provider.ArrayUpdater;
import com.vaadin.flow.data.provider.CompositeDataGenerator;
import com.vaadin.flow.data.provider.DataCommunicator;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.renderer.LitRenderer;
import com.vaadin.flow.data.renderer.Rendering;
import com.vaadin.flow.internal.JsonUtils;
import com.vaadin.flow.shared.Registration;

import elemental.json.JsonValue;

@Tag("lit-renderer-test-component")
@JsModule("lit-renderer-test-component.js")
public class LitRendererTestComponent extends Div
implements HasDataProvider<String> {

public class LitRendererTestComponent extends Div {
private final DataCommunicator<String> dataCommunicator;
private final CompositeDataGenerator<String> dataGenerator = new CompositeDataGenerator<>();
private final CompositeDataGenerator<String> dataGenerator;

private final List<Registration> renderingRegistrations = new ArrayList<>();
private final List<Registration> detailsRenderingRegistrations = new ArrayList<>();

private final ArrayUpdater arrayUpdater = new ArrayUpdater() {
@Override
public Update startUpdate(int sizeChange) {
return new Update() {
@Override
public void clear(int start, int length) {
// not essential for this test
}

@Override
public void set(int start, List<JsonValue> items) {
getElement().executeJs("this.items = $0",
items.stream().collect(JsonUtils.asArray()));
}

@Override
public void commit(int updateId) {
// not essential for this test
}
};
}

@Override
public void initialize() {
// not essential for this test
}
};

public LitRendererTestComponent() {
dataCommunicator = new DataCommunicator<>(dataGenerator, arrayUpdater,
data -> {
}, getElement().getNode());
public LitRendererTestComponent(DataCommunicator<String> dataCommunicator,
CompositeDataGenerator<String> dataGenerator) {
this.dataCommunicator = dataCommunicator;
this.dataGenerator = dataGenerator;
}

public void setRenderer(LitRenderer<String> renderer) {
Expand Down Expand Up @@ -108,16 +71,4 @@ public void setDetailsRenderer(LitRenderer<String> renderer) {
dataCommunicator.reset();
}
}

@Override
public void setItems(Collection<String> items) {
HasDataProvider.super.setItems(items);
dataCommunicator.setRequestedRange(0, items.size());
}

@Override
public void setDataProvider(DataProvider<String, ?> dataProvider) {
dataCommunicator.setDataProvider(dataProvider, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.data.renderer.tests;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.data.binder.HasDataProvider;
import com.vaadin.flow.data.provider.ArrayUpdater;
import com.vaadin.flow.data.provider.CompositeDataGenerator;
import com.vaadin.flow.data.provider.DataCommunicator;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.renderer.LitRenderer;
import com.vaadin.flow.internal.JsonUtils;

import elemental.json.JsonValue;

public class LitRendererTestComponentWrapper extends Div
implements HasDataProvider<String> {
private final DataCommunicator<String> dataCommunicator;
private final CompositeDataGenerator<String> dataGenerator = new CompositeDataGenerator<>();

private final ArrayUpdater arrayUpdater = new ArrayUpdater() {
@Override
public Update startUpdate(int sizeChange) {
return new Update() {
@Override
public void clear(int start, int length) {
// not essential for this test
}

@Override
public void set(int start, List<JsonValue> items) {
getChildren().forEach((component) -> {
component.getElement().executeJs("this.items = $0",
items.stream().collect(JsonUtils.asArray()));
});
}

@Override
public void commit(int updateId) {
// not essential for this test
}
};
}

@Override
public void initialize() {
// not essential for this test
}
};

/**
* Creates a wrapper with one LitRendererTestComponent.
*/
public LitRendererTestComponentWrapper() {
this(1);
}

/**
* Creates a wrapper with the given number of LitRendererTestComponent.
*
* @param childCount
* the number of children to add
*/
public LitRendererTestComponentWrapper(int childCount) {
dataCommunicator = new DataCommunicator<>(dataGenerator, arrayUpdater,
data -> {
}, getElement().getNode());

for (int i = 0; i < childCount; i++) {
add(new LitRendererTestComponent(dataCommunicator, dataGenerator));
}
}

/**
* Sets the renderer for all children.
*/
public void setRenderer(LitRenderer<String> renderer) {
getComponents().forEach(component -> {
component.setRenderer(renderer);
});
}

/**
* Sets the renderer for the component at the given index.
*/
public void setRenderer(int componentIndex, LitRenderer<String> renderer) {
getComponents().get(componentIndex).setRenderer(renderer);
}

/**
* Sets the details renderer for all children.
*/
public void setDetailsRenderer(LitRenderer<String> renderer) {
getComponents().forEach(component -> {
component.setDetailsRenderer(renderer);
});
}

/**
* Sets the details renderer for the component at the given index.
*/
public void setDetailsRenderer(int componentIndex,
LitRenderer<String> renderer) {
getComponents().get(componentIndex).setDetailsRenderer(renderer);
}

private List<LitRendererTestComponent> getComponents() {
return getChildren().map(LitRendererTestComponent.class::cast)
.collect(Collectors.toList());
}

@Override
public void setItems(Collection<String> items) {
HasDataProvider.super.setItems(items);
dataCommunicator.setRequestedRange(0, items.size());
}

@Override
public void setDataProvider(DataProvider<String, ?> dataProvider) {
dataCommunicator.setDataProvider(dataProvider, null);
}
}
Loading

0 comments on commit f97839c

Please sign in to comment.