Skip to content

Commit

Permalink
Create new mixin interface HasLabel(#3241) (#8424)
Browse files Browse the repository at this point in the history
Add new HasLabel interface to be used by components that supports label
definitions

Fixes #3241

Co-authored-by: Pekka Hyvönen <pekka@vaadin.com>
  • Loading branch information
ovidiupopa07 and pleku committed Jun 18, 2020
1 parent 0c73352 commit 1621b61
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
51 changes: 51 additions & 0 deletions flow-server/src/main/java/com/vaadin/flow/component/HasLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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;

import com.vaadin.flow.dom.ElementConstants;

/**
* A component that supports label definition.
* <p>
* The default implementations set the label of the component to the given text for
* {@link #getElement()}. Override all methods in this interface if the text
* should be added to some other element.
*
*
* @author Vaadin Ltd
* @since
*/
public interface HasLabel extends HasElement{
/**
* Set the label of the component to the given text.
*
* @param label
* the label text to set or {@code null} to clear
*/
default void setLabel(String label) {
getElement().setProperty(ElementConstants.LABEL_PROPERTY_NAME, label);
}

/**
* Gets the label of the component.
*
* @return the label of the component or {@code null} if no label has
* been set
*/
default String getLabel() {
return getElement().getProperty(ElementConstants.LABEL_PROPERTY_NAME, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class ElementConstants {
* The style property for color.
*/
public static final String STYLE_COLOR = "color";
/**
* The label property.
*/
public static final String LABEL_PROPERTY_NAME = "label";

private ElementConstants() {
// Constants only
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Test;


public class HasLabelTest {

@Tag(Tag.DIV)
private static class TestComponent extends Component
implements HasLabel {

}

@Test
public void withoutLabelComponent_getLabelReturnsNull() {
TestComponent component = new TestComponent();

assertNull(component.getLabel());
}

@Test
public void withNullLabel_getLabelReturnsNull() {
TestComponent component = new TestComponent();
component.setLabel(null);
assertNull(component.getLabel());
}

@Test
public void withEmptyLabel_getLabelReturnsEmptyString() {
TestComponent component = new TestComponent();
component.setLabel("");
assertEquals("",component.getLabel());
}


@Test
public void setLabel() {
TestComponent component = new TestComponent();
component.setLabel("test label");

assertEquals("test label", component.getLabel());
}

}

0 comments on commit 1621b61

Please sign in to comment.