Skip to content

Commit

Permalink
feature: Implement HasPlaceholder. Fixes #4068
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Nov 21, 2023
1 parent 156854e commit 717f30d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
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");
}
}
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());
}

}

0 comments on commit 717f30d

Please sign in to comment.