private boolean prompting = false;
private int lastCursorPos = -1;
+ // used while checking if FF has set input prompt as value
+ private boolean possibleInputError = false;
+
public VTextField() {
this(DOM.createInputText());
}
super(node);
setStyleName(CLASSNAME);
addChangeHandler(this);
- if (BrowserInfo.get().isIE()) {
- // IE does not send change events when pressing enter in a text
- // input so we handle it using a key listener instead
+ if (BrowserInfo.get().isIE() || BrowserInfo.get().isFirefox()) {
addKeyDownHandler(this);
}
addFocusHandler(this);
if (focusedTextField == this) {
focusedTextField = null;
}
+ if (BrowserInfo.get().isFirefox()) {
+ removeOnInputListener(getElement());
+ }
}
@Override
if (listenTextChangeEvents) {
detachCutEventListener(getElement());
}
+ if (BrowserInfo.get().isFirefox()) {
+ // Workaround for FF setting input prompt as the value if esc is
+ // pressed while the field is focused and empty (#8051).
+ addOnInputListener(getElement());
+ }
}
/** For internal use only. May be removed or replaced in the future. */
@Override
public void onKeyDown(KeyDownEvent event) {
- if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
+ if (BrowserInfo.get().isIE()
+ && event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
+ // IE does not send change events when pressing enter in a text
+ // input so we handle it using a key listener instead
valueChange(false);
+ } else if (BrowserInfo.get().isFirefox()
+ && event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE
+ && getText().equals("")) {
+ // check after onInput event if inputPrompt has appeared as the
+ // value of the field
+ possibleInputError = true;
}
}
String wrap = getElement().getAttribute("wrap");
return !"off".equals(wrap);
}
+
+ private native void addOnInputListener(Element el)
+ /*-{
+ var self = this;
+ el.oninput = $entry(function() {
+ self.@com.vaadin.client.ui.VTextField::checkForInputError()();
+ });
+ }-*/;
+
+ private native void removeOnInputListener(Element el)
+ /*-{
+ el.oninput = null;
+ }-*/;
+
+ private void checkForInputError() {
+ if (possibleInputError && getText().equals(inputPrompt)) {
+ setText("");
+ }
+ possibleInputError = false;
+ }
}