Renames the API server side and uses placeholder attribute for input.
Removed PopupDateFieldInputPrompt test since it tests input prompt logic,
that is no longer there after switching to native placeholder attribute
for input element.
Change-Id: I5c66456781f8703ba2e253134b88f8372825528f
/** For internal use only. May be removed or replaced in the future. */
public boolean lenient;
- private static final String CLASSNAME_PROMPT = "prompt";
-
- /** For internal use only. May be removed or replaced in the future. */
- public static final String ATTR_INPUTPROMPT = "prompt";
-
- /** For internal use only. May be removed or replaced in the future. */
- public String inputPrompt = "";
-
- private boolean prompting = false;
-
public VTextualDate() {
super();
text = new TextBox();
public void onFocus(FocusEvent event) {
text.addStyleName(VTextField.CLASSNAME + "-"
+ VTextField.CLASSNAME_FOCUS);
- if (prompting) {
- text.setText("");
- setPrompting(false);
- }
if (getClient() != null && getClient()
.hasEventListeners(VTextualDate.this, EventId.FOCUS)) {
getClient().updateVariable(getId(), EventId.FOCUS, "",
text.removeStyleName(VTextField.CLASSNAME + "-"
+ VTextField.CLASSNAME_FOCUS);
String value = getText();
- setPrompting(inputPrompt != null
- && (value == null || "".equals(value)));
- if (prompting) {
- text.setText(readonly ? "" : inputPrompt);
- }
if (getClient() != null && getClient()
.hasEventListeners(VTextualDate.this, EventId.BLUR)) {
getClient().updateVariable(getId(), EventId.BLUR, "", true);
text.setEnabled(enabled);
}
- protected void setPrompting(boolean prompting) {
- this.prompting = prompting;
- if (prompting) {
- addStyleDependentName(CLASSNAME_PROMPT);
- } else {
- removeStyleDependentName(CLASSNAME_PROMPT);
- }
- }
-
@Override
@SuppressWarnings("deprecation")
public void onChange(ChangeEvent event) {
text.setFocus(true);
}
- protected String getText() {
- if (prompting) {
- return "";
+ /**
+ * Sets the placeholder for this textual date input.
+ *
+ * @param placeholder
+ * the placeholder to set, or {@code null} to clear
+ */
+ public void setPlaceholder(String placeholder) {
+ if (placeholder != null) {
+ text.getElement().setAttribute("placeholder", placeholder);
+ } else {
+ text.getElement().removeAttribute("placeholder");
}
+ }
+
+ /**
+ * Gets the set placeholder this textual date input, or an empty string if
+ * none is set.
+ *
+ * @return the placeholder or an empty string if none set
+ */
+ public String getPlaceHolder() {
+ return text.getElement().getAttribute("placeholder");
+ }
+
+ protected String getText() {
return text.getText();
}
protected void setText(String text) {
- if (inputPrompt != null && (text == null || "".equals(text))
- && !this.text.getStyleName().contains(VTextField.CLASSNAME + "-"
- + VTextField.CLASSNAME_FOCUS)) {
- text = readonly ? "" : inputPrompt;
- setPrompting(true);
- } else {
- setPrompting(false);
- }
-
this.text.setText(text);
}
import com.google.gwt.user.client.ui.PopupPanel;
import com.vaadin.client.ApplicationConnection;
import com.vaadin.client.UIDL;
+import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.VCalendarPanel.FocusChangeListener;
import com.vaadin.client.ui.VPopupCalendar;
getWidget().formatStr = uidl.getStringAttribute("format");
}
- getWidget().inputPrompt = uidl
- .getStringAttribute(VTextualDate.ATTR_INPUTPROMPT);
-
getWidget().lenient = !uidl.getBooleanAttribute("strict");
getWidget().buildDate();
import java.time.LocalDate;
-import com.vaadin.server.PaintException;
-import com.vaadin.server.PaintTarget;
import com.vaadin.shared.ui.datefield.DateFieldState;
/**
*/
public class DateField extends AbstractDateField {
- private String inputPrompt = null;
-
/**
* Constructs an empty <code>DateField</code> with no caption.
*/
super(caption);
}
- @Override
- public void paintContent(PaintTarget target) throws PaintException {
- super.paintContent(target);
-
- if (inputPrompt != null) {
- target.addAttribute("prompt", inputPrompt);
- }
- }
-
/**
- * Gets the current input prompt.
+ * Returns the current placeholder text.
*
- * @see #setInputPrompt(String)
- * @return the current input prompt, or null if not enabled
+ * @see #setPlaceholder(String)
+ * @return the placeholder text
*/
- public String getInputPrompt() {
- return inputPrompt;
+ public String getPlaceholder() {
+ return getState(false).placeholder;
}
/**
- * Sets the input prompt - a textual prompt that is displayed when the field
- * would otherwise be empty, to prompt the user for input.
+ * Sets the placeholder text. The placeholder is text that is displayed when
+ * the field would otherwise be empty, to prompt the user for input.
*
- * @param inputPrompt
+ * @param placeholder
+ * the placeholder text to set
*/
- public void setInputPrompt(String inputPrompt) {
- this.inputPrompt = inputPrompt;
- markAsDirty();
+ public void setPlaceholder(String placeholder) {
+ getState().placeholder = placeholder;
}
@Override
public class DateFieldDeclarativeTest extends DeclarativeTestBase<DateField> {
private String getBasicDesign() {
- return "<vaadin-date-field assistive-text='at' text-field-enabled='false' show-iso-week-numbers range-end=\"2019-01-15\" input-prompt=\"Pick a day\" value=\"2003-02-27\"></vaadin-date-field>";
+ return "<vaadin-date-field assistive-text='at' text-field-enabled='false' show-iso-week-numbers range-end=\"2019-01-15\" placeholder=\"Pick a day\" value=\"2003-02-27\"></vaadin-date-field>";
}
private DateField getBasicExpected() {
DateField pdf = new DateField();
pdf.setShowISOWeekNumbers(true);
pdf.setRangeEnd(LocalDate.of(2019, 01, 15));
- pdf.setInputPrompt("Pick a day");
+ pdf.setPlaceholder("Pick a day");
pdf.setValue(LocalDate.of(2003, 2, 27));
pdf.setTextFieldEnabled(false);
pdf.setAssistiveText("at");
*/
package com.vaadin.shared.ui.datefield;
+import com.vaadin.shared.annotations.DelegateToWidget;
import com.vaadin.shared.annotations.NoLayout;
public class DateFieldState extends TextualDateFieldState {
public boolean textFieldEnabled = true;
@NoLayout
public String descriptionForAssistiveDevices = DESCRIPTION_FOR_ASSISTIVE_DEVICES;
+ @NoLayout
+ @DelegateToWidget
+ public String placeholder = null;
}
@Override
public void execute(DateField c, String value,
Object data) {
- c.setInputPrompt(value);
+ c.setPlaceholder(value);
}
});
@Override
public void execute(DateField c, String value,
Object data) {
- c.setInputPrompt(value);
+ c.setPlaceholder(value);
}
});
+++ /dev/null
-/*
- * Copyright 2000-2016 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.tests.components.datefield;
-
-import com.vaadin.server.VaadinRequest;
-import com.vaadin.tests.components.AbstractReindeerTestUI;
-import com.vaadin.ui.DateField;
-import com.vaadin.ui.TextField;
-
-/**
- * Tests that state change doesn't set input prompt back to PopupDateField if
- * focus is still in the input field.
- *
- * @author Vaadin Ltd
- */
-public class PopupDateFieldInputPrompt extends AbstractReindeerTestUI {
-
- private TextField text = new TextField("TextField");
- private DateField dateField = new DateField();
-
- @Override
- protected void setup(VaadinRequest request) {
-
- text.addValueChangeListener(listener -> {
- // update PopupDateField's state
- dateField.setRequired(!dateField.isRequired());
- });
-
- dateField.setInputPrompt("prompt");
- dateField.setCaption("PopupDateField");
-
- addComponent(text);
- addComponent(dateField);
- }
-
- @Override
- protected String getTestDescription() {
- return "Write something to the TextField and use tabulator to move to PopupDateField."
- + "<br>PopupDateField shouldn't get input prompt back before focus leaves the input field,"
- + "<br>even if TextField's value change updates PopupDateField's state.";
- }
-
- @Override
- protected Integer getTicketNumber() {
- return 18027;
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.tests.components.datefield;
-
-import java.util.List;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.openqa.selenium.By;
-import org.openqa.selenium.Keys;
-import org.openqa.selenium.WebDriver;
-import org.openqa.selenium.WebElement;
-import org.openqa.selenium.support.ui.ExpectedCondition;
-
-import com.vaadin.testbench.customelements.DateFieldElement;
-import com.vaadin.testbench.elements.TextFieldElement;
-import com.vaadin.tests.tb3.MultiBrowserTest;
-
-/**
- * Tests that state change doesn't set input prompt back to PopupDateField if
- * focus is still in the input field.
- *
- * @author Vaadin Ltd
- */
-public class PopupDateFieldInputPromptTest extends MultiBrowserTest {
-
- @Test
- public void testInputPrompt() {
- openTestURL();
- TextFieldElement textField = $(TextFieldElement.class).first();
- final DateFieldElement dateField = $(DateFieldElement.class).first();
-
- // ensure initial state
- Assert.assertFalse("DateField required when it shouldn't be.",
- isRequired(dateField));
- WebElement input = dateField.findElement(By.className("v-textfield"));
- Assert.assertEquals("prompt", input.getAttribute("value"));
-
- // trigger ValueChange and move focus
- textField.sendKeys("foo", Keys.TAB);
-
- // wait for ValueChange to update DateField's state and the DateField to
- // gain focus.
- waitForElementRequiredAndFocused(dateField,
- By.className("v-textfield-focus"));
-
- // ensure prompt hasn't come back when field was set required
- Assert.assertNotEquals("prompt", input.getAttribute("value"));
- }
-
- private void waitForElementRequiredAndFocused(
- final DateFieldElement dateField, final By locator) {
- waitUntil(new ExpectedCondition<Boolean>() {
-
- @Override
- public Boolean apply(WebDriver arg0) {
- if (isRequired(dateField)) {
- List<WebElement> elements = dateField.findElements(locator);
- return !elements.isEmpty();
- }
-
- return false;
- }
-
- @Override
- public String toString() {
- return "dateField to become required and presence of element located by: "
- + locator;
- }
- });
- }
-
- private boolean isRequired(DateFieldElement dateField) {
- return dateField.getAttribute("class").contains("v-required");
- }
-}