From 717f30d2e1bc8b03935fb4c2494f37fa4fc3433a Mon Sep 17 00:00:00 2001 From: Martin Vysny Date: Tue, 21 Nov 2023 06:10:56 +0200 Subject: [PATCH] feature: Implement HasPlaceholder. Fixes #4068 --- .../vaadin/flow/component/HasPlaceholder.java | 35 +++++++++++ .../flow/component/HasPlaceholderTest.java | 59 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 flow-server/src/main/java/com/vaadin/flow/component/HasPlaceholder.java create mode 100644 flow-server/src/test/java/com/vaadin/flow/component/HasPlaceholderTest.java diff --git a/flow-server/src/main/java/com/vaadin/flow/component/HasPlaceholder.java b/flow-server/src/main/java/com/vaadin/flow/component/HasPlaceholder.java new file mode 100644 index 00000000000..f124340b0b2 --- /dev/null +++ b/flow-server/src/main/java/com/vaadin/flow/component/HasPlaceholder.java @@ -0,0 +1,35 @@ +package com.vaadin.flow.component; + +/** + * A component which supports a placeholder. + *

+ * A placeholder is a text that should be displayed in the input element, + * when the user has not entered a value. + *

+ * The default implementations sets the placeholder 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"); + } +} diff --git a/flow-server/src/test/java/com/vaadin/flow/component/HasPlaceholderTest.java b/flow-server/src/test/java/com/vaadin/flow/component/HasPlaceholderTest.java new file mode 100644 index 00000000000..104a1023dc2 --- /dev/null +++ b/flow-server/src/test/java/com/vaadin/flow/component/HasPlaceholderTest.java @@ -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()); + } + +}