Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,77 @@
/*
* Copyright 2000-2026 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.textfield;

/**
* Virtual keyboard hints for the {@code inputmode} attribute. They tell the
* browser which type of virtual keyboard to display when the user interacts
* with the field on a mobile device.
*
* @see <a href=
* "https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode">
* inputmode attribute</a>
*/
public enum InputMode {

/**
* No virtual keyboard. Useful when the field implements its own keyboard
* input control.
*/
NONE("none"),

/**
* Standard text input keyboard for the user's current locale.
*/
TEXT("text"),

/**
* Fractional numeric input keyboard that includes the digits and the
* decimal separator for the user's locale.
*/
DECIMAL("decimal"),

/**
* Numeric input keyboard that only requires the digits 0&ndash;9.
*/
NUMERIC("numeric"),

/**
* Telephone keypad input, including the digits 0&ndash;9, the asterisk (*),
* and the pound (#) key.
*/
TEL("tel"),

/**
* Virtual keyboard optimized for search input.
*/
SEARCH("search"),

/**
* Virtual keyboard optimized for entering email addresses.
*/
EMAIL("email"),

/**
* Virtual keyboard optimized for entering URLs.
*/
URL("url");

final String value;

InputMode(String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,39 @@
return getElement().getProperty("pattern");
}

/**
* Sets the {@link InputMode} that hints at the type of virtual keyboard to
* display when the user interacts with the field on a mobile device. If not
* set, the browser defaults to {@link InputMode#TEXT}.
*
* @param inputMode
* the {@code inputmode} value, or {@code null} to unset
* @see <a href=
* "https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode">
* inputmode attribute</a>
*/
public void setInputMode(InputMode inputMode) {
if (inputMode == null) {
getElement().removeProperty("inputMode");

Check failure on line 399 in vaadin-text-field-flow-parent/vaadin-text-field-flow/src/main/java/com/vaadin/flow/component/textfield/TextField.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "inputMode" 3 times.

See more on https://sonarcloud.io/project/issues?id=vaadin_flow-components&issues=AZ795A-ZRZnl_-eTN1Mj&open=AZ795A-ZRZnl_-eTN1Mj&pullRequest=9614
} else {
getElement().setProperty("inputMode", inputMode.value);
}
}

/**
* Gets the {@link InputMode} of the field.
*
* @return the {@code inputmode} value, or {@code null} if not set
* @see #setInputMode(InputMode)
*/
public InputMode getInputMode() {
String inputMode = getElement().getProperty("inputMode");
if (inputMode == null || inputMode.isEmpty()) {
return null;
}
return InputMode.valueOf(inputMode.toUpperCase());

Check warning on line 416 in vaadin-text-field-flow-parent/vaadin-text-field-flow/src/main/java/com/vaadin/flow/component/textfield/TextField.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define the locale to be used in this String operation.

See more on https://sonarcloud.io/project/issues?id=vaadin_flow-components&issues=AZ795A-ZRZnl_-eTN1Mk&open=AZ795A-ZRZnl_-eTN1Mk&pullRequest=9614
}

@Override
public String getEmptyValue() {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.vaadin.flow.component.shared.HasThemeVariant;
import com.vaadin.flow.component.shared.HasTooltip;
import com.vaadin.flow.component.shared.InputField;
import com.vaadin.flow.component.textfield.InputMode;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.component.textfield.TextFieldVariant;
import com.vaadin.flow.di.Instantiator;
Expand Down Expand Up @@ -114,6 +115,26 @@ void autoselectPropertyValue() {
assertAutoselectPropertyValueEquals(textField, false);
}

@Test
void inputMode_defaultsToNull() {
TextField textField = new TextField();
Assertions.assertNull(textField.getInputMode());
}

@Test
void setInputMode_getInputMode() {
TextField textField = new TextField();

textField.setInputMode(InputMode.NUMERIC);
Assertions.assertEquals(InputMode.NUMERIC, textField.getInputMode());
Assertions.assertEquals("numeric",
textField.getElement().getProperty("inputMode"));

textField.setInputMode(null);
Assertions.assertNull(textField.getInputMode());
Assertions.assertNull(textField.getElement().getProperty("inputMode"));
}

@Test
void elementHasValue_wrapIntoTextField_propertyIsNotSetToInitialValue() {
ComponentFromTest
Expand Down
Loading