-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
d532832
commit f97839c
Showing
8 changed files
with
309 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...s/src/main/java/com/vaadin/flow/data/renderer/tests/LitRendererPropertyNamespacePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
...ts/src/main/java/com/vaadin/flow/data/renderer/tests/LitRendererTestComponentWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.