-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Implement HasPlaceholder. Fixes #4068
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
flow-server/src/main/java/com/vaadin/flow/component/HasPlaceholder.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,35 @@ | ||
package com.vaadin.flow.component; | ||
|
||
/** | ||
* A component which supports a placeholder. | ||
* <p> | ||
* A placeholder is a text that should be displayed in the input element, | ||
* when the user has not entered a value. | ||
* <p> | ||
* The default implementations sets the <code>placeholder</code> property for this element. | ||
* Override all methods in this interface if the placeholder | ||
* should be set in some other way. | ||
*/ | ||
public interface HasPlaceholder extends HasElement { | ||
/** | ||
* Sets the placeholder text that should be displayed in the input element, | ||
* when the user has not entered a value | ||
* | ||
* @param placeholder the placeholder text, may be null. | ||
*/ | ||
default void setPlaceholder(String placeholder) { | ||
getElement().setProperty("placeholder", | ||
placeholder == null ? "" : placeholder); | ||
} | ||
|
||
/** | ||
* The placeholder text that should be displayed in the input element, when | ||
* the user has not entered a value | ||
* | ||
* @return the {@code placeholder} property from the web component. May be | ||
* null if not yet set. | ||
*/ | ||
default String getPlaceholder() { | ||
return getElement().getProperty("placeholder"); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
flow-server/src/test/java/com/vaadin/flow/component/HasPlaceholderTest.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,59 @@ | ||
/* | ||
* Copyright 2000-2023 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 org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
public class HasPlaceholderTest { | ||
|
||
@Tag(Tag.DIV) | ||
private static class TestComponent extends Component implements HasPlaceholder { | ||
|
||
} | ||
|
||
@Test | ||
public void withoutPlaceholderComponent_getPlaceholderReturnsNull() { | ||
TestComponent component = new TestComponent(); | ||
|
||
assertNull(component.getPlaceholder()); | ||
} | ||
|
||
@Test | ||
public void withNullPlaceholder_getPlaceholderReturnsEmptyString() { | ||
TestComponent component = new TestComponent(); | ||
component.setPlaceholder(null); | ||
assertEquals("", component.getPlaceholder()); | ||
} | ||
|
||
@Test | ||
public void withEmptyPlaceholder_getPlaceholderReturnsEmptyString() { | ||
TestComponent component = new TestComponent(); | ||
component.setPlaceholder(""); | ||
assertEquals("", component.getPlaceholder()); | ||
} | ||
|
||
@Test | ||
public void setPlaceholder() { | ||
TestComponent component = new TestComponent(); | ||
component.setPlaceholder("test Placeholder"); | ||
|
||
assertEquals("test Placeholder", component.getPlaceholder()); | ||
} | ||
|
||
} |