Skip to content

Commit

Permalink
Set enable state based on template element attribute (#9007)
Browse files Browse the repository at this point in the history
Set element disabled if template has disabled property
Fixes #8973
  • Loading branch information
Denis authored and pleku committed Feb 2, 2021
1 parent 0991728 commit 8b37d2a
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ private static Map<String, ElementInitializationStrategy> createStategies() {
result.put("spellcheck", attributeStrategy);
result.put("tabindex", attributeStrategy);
result.put("translate", attributeStrategy);

result.put("disabled", new DisabledInitializationStrategy());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2000-2020 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.component.template.internal;

import java.io.Serializable;

import com.vaadin.flow.dom.Element;

/**
* Initializes Element via enabling/disabling it.
*
* @author Vaadin Ltd
* @since
*
*/
class DisabledInitializationStrategy
implements ElementInitializationStrategy, Serializable {

@Override
public void initialize(Element element, String name, String value) {
assert "disabled".equals(name);
// this is a boolean attribute
assert value == null || value.isEmpty()
|| value.equals(Boolean.TRUE.toString());
element.setEnabled(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public void initializeElement_setStyle_styleIsSetAsAttribute() {
Assert.assertEquals("50px", comp.getStyle().get("height"));
}

@Test
public void initializeElement_disabled_elementIsDisabled() {
initializer.accept(
Collections.singletonMap("disabled", Boolean.TRUE.toString()));

Assert.assertFalse(element.isEnabled());
}

@Tag(Tag.DIV)
public static class TestComponent extends Component implements HasStyle {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ public TestLitTemplate(VaadinService service) {

}

@Tag("foo-bar")
private static class DisabledElementTemplate extends LitTemplate {

@Id("labelId")
private com.vaadin.flow.dom.Element label;

public DisabledElementTemplate(VaadinService service) {
this((clazz, tag, svc) -> new LitTemplateParser.TemplateData("",
Jsoup.parse("<foo-bar id='" + tag
+ "'><label id='labelId' disabled></foo-bar>")),
service);
}

DisabledElementTemplate(LitTemplateParser parser,
VaadinService service) {
super(parser, service);
}

}

@Before
public void setUp() {
DeploymentConfiguration configuration = Mockito
Expand Down Expand Up @@ -91,4 +111,12 @@ public void attachExistingElementWithAttributeValue_elementHasAttribute() {
template.label.getAttribute("hidden"));
}

@Test
public void attachExistingElementWithAttributeValue_elementIsDisabled() {
DisabledElementTemplate template = new DisabledElementTemplate(service);

Assert.assertTrue(template.label.hasAttribute("id"));
Assert.assertFalse(template.label.isEnabled());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public void initializeElement_setStyle_styleIsSetAsAttribute() {
Assert.assertEquals("50px", comp.getStyle().get("height"));
}

@Test
public void initializeElement_disabled_elementIsDisabled() {
initializer.accept(
Collections.singletonMap("disabled", Boolean.TRUE.toString()));

Assert.assertFalse(element.isEnabled());
}

@Tag(Tag.DIV)
public static class TestComponent extends Component implements HasStyle {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,25 @@ public IdElementTemplate() {

}

@Tag(TAG)
private static class DisabledElementTemplate
extends PolymerTemplate<ModelClass> {

@Id("labelId")
private com.vaadin.flow.dom.Element label;

public DisabledElementTemplate() {
this((clazz, tag, service) -> new TemplateData("",
Jsoup.parse("<dom-module id='" + tag
+ "'><label id='labelId' disabled></dom-module>")));
}

DisabledElementTemplate(TemplateParser parser) {
super(parser);
}

}

private static class IdWrongElementTemplate extends IdElementTemplate {

public IdWrongElementTemplate() {
Expand Down Expand Up @@ -798,6 +817,14 @@ public void attachExistingElementWithAttributeValue_elementHasPropertyAndAttribu
template.label.getAttribute("hidden"));
}

@Test
public void attachExistingElementWithAttributeValue_elementIsDisabled() {
DisabledElementTemplate template = new DisabledElementTemplate();

Assert.assertTrue(template.label.hasAttribute("id"));
Assert.assertFalse(template.label.isEnabled());
}

@Test
public void attachExistingElement_injectedByIDdChild_onlyOneElementIsCreated() {
TemplateInjectTemplate template = new TemplateInjectTemplate();
Expand Down
1 change: 1 addition & 0 deletions flow-tests/test-root-context/frontend/AttributeTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class AttributeTemplate extends PolymerElement {
return html`
<div style="padding: 10px; border: 1px solid black">
<div id="div" title="foo" foo="bar" baz></div>
<div id="disabled" disabled></div>
</div>
<slot>
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class AttributeLitTemplate extends LitElement {
return html`
<div style="padding: 10px; border: 1px solid black">
<div id="div" title="foo" foo="bar" baz></div>
<div id="disabled" disabled></div>
</div>
<slot>
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class LitTemplateAttributeView extends LitTemplate
@Id("div")
private Div injectedDiv;

@Id("disabled")
private Div disabledDiv;

@Override
protected void onAttach(AttachEvent attachEvent) {
setId("template");
Expand All @@ -28,5 +31,10 @@ protected void onAttach(AttachEvent attachEvent) {
+ injectedDiv.getElement().getProperty("baz"));
div.setId("info");
add(div);

div = new Div();
div.setId("disabledInfo");
div.setText("Enabled: " + disabledDiv.isEnabled());
add(div);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class TemplateAttributeView extends PolymerTemplate<TemplateModel>
@Id("div")
private Div injectedDiv;

@Id("disabled")
private Div disabledDiv;

@Override
protected void onAttach(AttachEvent attachEvent) {
setId("template");
Expand All @@ -29,5 +32,10 @@ protected void onAttach(AttachEvent attachEvent) {
+ injectedDiv.getElement().getProperty("baz"));
div.setId("info");
add(div);

div = new Div();
div.setId("disabledInfo");
div.setText("Enabled: " + disabledDiv.isEnabled());
add(div);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ public void readTemplateAttribiute() {
TestBenchElement template = $(TestBenchElement.class).id("template");
TestBenchElement info = template.$(TestBenchElement.class).id("info");
Assert.assertEquals("foo bar true", info.getText());

TestBenchElement isDisabled = template.$(TestBenchElement.class)
.id("disabledInfo");
Assert.assertEquals("Enabled: false", isDisabled.getText());
}
}

0 comments on commit 8b37d2a

Please sign in to comment.