Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Implement HasPlaceholder #18081

Merged
merged 7 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/
package com.vaadin.flow.component.html;

import java.util.Optional;

import com.vaadin.flow.component.AbstractSinglePropertyField;
import com.vaadin.flow.component.Focusable;
import com.vaadin.flow.component.HasAriaLabel;
import com.vaadin.flow.component.HasPlaceholder;
import com.vaadin.flow.component.HasSize;
import com.vaadin.flow.component.HasStyle;
import com.vaadin.flow.component.PropertyDescriptor;
Expand All @@ -35,11 +34,12 @@
* @since 1.0
*/
@Tag(Tag.INPUT)
public class Input extends AbstractSinglePropertyField<Input, String> implements
Focusable<Input>, HasSize, HasStyle, HasValueChangeMode, HasAriaLabel {
public class Input extends AbstractSinglePropertyField<Input, String>
implements Focusable<Input>, HasSize, HasStyle, HasValueChangeMode,
HasAriaLabel, HasPlaceholder {

private static final PropertyDescriptor<String, Optional<String>> placeholderDescriptor = PropertyDescriptors
.optionalAttributeWithDefault("placeholder", "");
private static final PropertyDescriptor<String, String> placeholderDescriptor = PropertyDescriptors
.attributeWithDefault("placeholder", "");

private static final PropertyDescriptor<String, String> typeDescriptor = PropertyDescriptors
.attributeWithDefault("type", "text");
Expand Down Expand Up @@ -68,26 +68,11 @@ public Input(ValueChangeMode valueChangeMode) {
setValueChangeMode(valueChangeMode);
}

/**
* Sets the placeholder text that is shown if the input is empty.
*
* @param placeholder
* the placeholder text to set, or <code>null</code> to remove
* the placeholder
*/
public void setPlaceholder(String placeholder) {
set(placeholderDescriptor, placeholder);
}

/**
* Gets the placeholder text.
*
* @see #setPlaceholder(String)
*
* @return an optional placeholder, or an empty optional if no placeholder
* has been set
*/
public Optional<String> getPlaceholder() {
public String getPlaceholder() {
return get(placeholderDescriptor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected void addProperties() {
addStringProperty("type", "text");
// Object.class because of generics
addProperty("value", Object.class, "", "foo", false, false);
addOptionalStringProperty("placeholder");
addStringProperty("placeholder", "");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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;

/**
* 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,60 @@
/*
* 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());
}

}
Loading