-
Notifications
You must be signed in to change notification settings - Fork 164
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
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
1 parent
0c73352
commit 1621b61
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
flow-server/src/main/java/com/vaadin/flow/component/HasLabel.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,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); | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
flow-server/src/test/java/com/vaadin/flow/component/HasLabelTest.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,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()); | ||
} | ||
|
||
} |