All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.flow.component.textfield.GeneratedVaadinTextField Maven / Gradle / Ivy

There is a newer version: 24.4.12
Show newest version
/**
 * Copyright 2000-2024 Vaadin Ltd.
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  {@literal }  for the full
 * license.
 */
package com.vaadin.flow.component.textfield;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.vaadin.flow.component.AbstractSinglePropertyField;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.DomEvent;
import com.vaadin.flow.component.Focusable;
import com.vaadin.flow.component.HasStyle;
import com.vaadin.flow.component.HasTheme;
import com.vaadin.flow.component.NotSupported;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.function.SerializableBiFunction;
import com.vaadin.flow.function.SerializableFunction;
import com.vaadin.flow.shared.Registration;

/**
 * 

* Description copied from corresponding location in WebComponent: *

*

* {@code } is a Web Component for text field control in * forms. *

*

* <vaadin-text-field label="First Name"> * </vaadin-text-field> *

*

Prefixes and suffixes

*

* These are child elements of a {@code } that are displayed * inline with the input, before or after. In order for an element to be * considered as a prefix, it must have the slot attribute set to {@code prefix} * (and similarly for {@code suffix}). *

*

* <vaadin-text-field label="Email address"> <div * slot="prefix">Sent to:</div> <div * slot="suffix">@vaadin.com</div> </vaadin-text-area> *

*

Styling

*

* The following custom properties are available for styling: *

* * * * * * * * * * * * * * *
Custom propertyDescriptionDefault
{@code --vaadin-text-field-default-width}Set the default width of the input field{@code 12em}
*

* The following shadow DOM parts are available for styling: *

* * * * * * * * * * * * * * * * * * * * * * * * *
Part nameDescription
{@code label}The label element
{@code input-field}The element that wraps prefix, value and suffix
{@code value}The text value element inside the {@code input-field} element
{@code error-message}The error message element
*

* The following state attributes are available for styling: *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
AttributeDescriptionPart name
{@code disabled}Set to a disabled text field:host
{@code has-value}Set when the element has a value:host
{@code has-label}Set when the element has a label:host
{@code invalid}Set when the element is invalid:host
{@code input-prevented}Temporarily set when invalid input is prevented:host
{@code focused}Set when the element is focused:host
{@code focus-ring}Set when the element is keyboard focused:host
{@code readonly}Set to a readonly text field:host
*

* See * ThemableMixin * – how to apply styles for shadow parts *

*/ @Tag("vaadin-text-field") @NpmPackage(value = "@vaadin/vaadin-text-field", version = "2.10.0") @JsModule("@vaadin/vaadin-text-field/src/vaadin-text-field.js") public abstract class GeneratedVaadinTextField, T> extends AbstractSinglePropertyField implements HasStyle, Focusable, HasTheme { /** * Adds theme variants to the component. * * @param variants * theme variants to add */ public void addThemeVariants(TextFieldVariant... variants) { getThemeNames().addAll( Stream.of(variants).map(TextFieldVariant::getVariantName) .collect(Collectors.toList())); } /** * Removes theme variants from the component. * * @param variants * theme variants to remove */ public void removeThemeVariants(TextFieldVariant... variants) { getThemeNames().removeAll( Stream.of(variants).map(TextFieldVariant::getVariantName) .collect(Collectors.toList())); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Specify that this control should have input focus when the page loads. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code autofocus} property from the webcomponent */ protected boolean isAutofocusBoolean() { return getElement().getProperty("autofocus", false); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Specify that this control should have input focus when the page loads. *

* * @param autofocus * the boolean value to set */ protected void setAutofocus(boolean autofocus) { getElement().setProperty("autofocus", autofocus); } /** *

* Description copied from corresponding location in WebComponent: *

*

* If true, the user cannot interact with this element. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code disabled} property from the webcomponent */ protected boolean isDisabledBoolean() { return getElement().getProperty("disabled", false); } /** *

* Description copied from corresponding location in WebComponent: *

*

* If true, the user cannot interact with this element. *

* * @param disabled * the boolean value to set */ protected void setDisabled(boolean disabled) { getElement().setProperty("disabled", disabled); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Whether the value of the control can be automatically completed by the * browser. List of available options at: * https://developer.mozilla.org/en/docs * /Web/HTML/Element/input#attr-autocomplete *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code autocomplete} property from the webcomponent */ protected String getAutocompleteString() { return getElement().getProperty("autocomplete"); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Whether the value of the control can be automatically completed by the * browser. List of available options at: * https://developer.mozilla.org/en/docs * /Web/HTML/Element/input#attr-autocomplete *

* * @param autocomplete * the String value to set */ protected void setAutocomplete(String autocomplete) { getElement().setProperty("autocomplete", autocomplete == null ? "" : autocomplete); } /** *

* Description copied from corresponding location in WebComponent: *

*

* This is a property supported by Safari that is used to control whether * autocorrection should be enabled when the user is entering/editing the * text. Possible values are: on: Enable autocorrection. off: Disable * autocorrection. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code autocorrect} property from the webcomponent */ protected String getAutocorrectString() { return getElement().getProperty("autocorrect"); } /** *

* Description copied from corresponding location in WebComponent: *

*

* This is a property supported by Safari that is used to control whether * autocorrection should be enabled when the user is entering/editing the * text. Possible values are: on: Enable autocorrection. off: Disable * autocorrection. *

* * @param autocorrect * the String value to set */ protected void setAutocorrect(String autocorrect) { getElement().setProperty("autocorrect", autocorrect == null ? "" : autocorrect); } /** *

* Description copied from corresponding location in WebComponent: *

*

* This is a property supported by Safari and Chrome that is used to control * whether autocapitalization should be enabled when the user is * entering/editing the text. Possible values are: characters: Characters * capitalization. words: Words capitalization. sentences: Sentences * capitalization. none: No capitalization. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code autocapitalize} property from the webcomponent */ protected String getAutocapitalizeString() { return getElement().getProperty("autocapitalize"); } /** *

* Description copied from corresponding location in WebComponent: *

*

* This is a property supported by Safari and Chrome that is used to control * whether autocapitalization should be enabled when the user is * entering/editing the text. Possible values are: characters: Characters * capitalization. words: Words capitalization. sentences: Sentences * capitalization. none: No capitalization. *

* * @param autocapitalize * the String value to set */ protected void setAutocapitalize(String autocapitalize) { getElement().setProperty("autocapitalize", autocapitalize == null ? "" : autocapitalize); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Specify that the value should be automatically selected when the field * gains focus. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code autoselect} property from the webcomponent */ protected boolean isAutoselectBoolean() { return getElement().getProperty("autoselect", false); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Specify that the value should be automatically selected when the field * gains focus. *

* * @param autoselect * the boolean value to set */ protected void setAutoselect(boolean autoselect) { getElement().setProperty("autoselect", autoselect); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Set to true to display the clear icon which clears the input. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code clearButtonVisible} property from the webcomponent */ protected boolean isClearButtonVisibleBoolean() { return getElement().getProperty("clearButtonVisible", false); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Set to true to display the clear icon which clears the input. *

* * @param clearButtonVisible * the boolean value to set */ protected void setClearButtonVisible(boolean clearButtonVisible) { getElement().setProperty("clearButtonVisible", clearButtonVisible); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Error to show when the input value is invalid. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code errorMessage} property from the webcomponent */ protected String getErrorMessageString() { return getElement().getProperty("errorMessage"); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Error to show when the input value is invalid. *

* * @param errorMessage * the String value to set */ protected void setErrorMessage(String errorMessage) { getElement().setProperty("errorMessage", errorMessage == null ? "" : errorMessage); } /** *

* Description copied from corresponding location in WebComponent: *

*

* String used for the label element. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code label} property from the webcomponent */ protected String getLabelString() { return getElement().getProperty("label"); } /** *

* Description copied from corresponding location in WebComponent: *

*

* String used for the label element. *

* * @param label * the String value to set */ protected void setLabel(String label) { getElement().setProperty("label", label == null ? "" : label); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Maximum number of characters (in Unicode code points) that the user can * enter. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code maxlength} property from the webcomponent */ protected double getMaxlengthDouble() { return getElement().getProperty("maxlength", 0.0); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Maximum number of characters (in Unicode code points) that the user can * enter. *

* * @param maxlength * the double value to set */ protected void setMaxlength(double maxlength) { getElement().setProperty("maxlength", maxlength); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Minimum number of characters (in Unicode code points) that the user can * enter. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code minlength} property from the webcomponent */ protected double getMinlengthDouble() { return getElement().getProperty("minlength", 0.0); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Minimum number of characters (in Unicode code points) that the user can * enter. *

* * @param minlength * the double value to set */ protected void setMinlength(double minlength) { getElement().setProperty("minlength", minlength); } /** *

* Description copied from corresponding location in WebComponent: *

*

* The name of the control, which is submitted with the form data. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code name} property from the webcomponent */ protected String getNameString() { return getElement().getProperty("name"); } /** *

* Description copied from corresponding location in WebComponent: *

*

* The name of the control, which is submitted with the form data. *

* * @param name * the String value to set */ protected void setName(String name) { getElement().setProperty("name", name == null ? "" : name); } /** *

* Description copied from corresponding location in WebComponent: *

*

* A hint to the user of what can be entered in the control. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code placeholder} property from the webcomponent */ protected String getPlaceholderString() { return getElement().getProperty("placeholder"); } /** *

* Description copied from corresponding location in WebComponent: *

*

* A hint to the user of what can be entered in the control. *

* * @param placeholder * the String value to set */ protected void setPlaceholder(String placeholder) { getElement().setProperty("placeholder", placeholder == null ? "" : placeholder); } /** *

* Description copied from corresponding location in WebComponent: *

*

* This attribute indicates that the user cannot modify the value of the * control. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code readonly} property from the webcomponent */ protected boolean isReadonlyBoolean() { return getElement().getProperty("readonly", false); } /** *

* Description copied from corresponding location in WebComponent: *

*

* This attribute indicates that the user cannot modify the value of the * control. *

* * @param readonly * the boolean value to set */ protected void setReadonly(boolean readonly) { getElement().setProperty("readonly", readonly); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Specifies that the user must fill in a value. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code required} property from the webcomponent */ protected boolean isRequiredBoolean() { return getElement().getProperty("required", false); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Specifies that the user must fill in a value. *

* * @param required * the boolean value to set */ protected void setRequired(boolean required) { getElement().setProperty("required", required); } /** *

* Description copied from corresponding location in WebComponent: *

*

* This property is set to true when the control value is invalid. *

* * @return the {@code invalid} property from the webcomponent */ protected boolean isInvalidBoolean() { return getElement().getProperty("invalid", false); } /** *

* Description copied from corresponding location in WebComponent: *

*

* This property is set to true when the control value is invalid. *

* * @param invalid * the boolean value to set */ protected void setInvalid(boolean invalid) { getElement().setProperty("invalid", invalid); } /** *

* Description copied from corresponding location in WebComponent: *

*

* When set to true, user is prevented from typing a value that conflicts * with the given {@code pattern}, {@code maxlength} or {@code minlength} * properties. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code preventInvalidInput} property from the webcomponent */ protected boolean isPreventInvalidInputBoolean() { return getElement().getProperty("preventInvalidInput", false); } /** *

* Description copied from corresponding location in WebComponent: *

*

* When set to true, user is prevented from typing a value that conflicts * with the given {@code pattern}, {@code maxlength} or {@code minlength} * properties. *

* * @param preventInvalidInput * the boolean value to set */ protected void setPreventInvalidInput(boolean preventInvalidInput) { getElement().setProperty("preventInvalidInput", preventInvalidInput); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Identifies a list of pre-defined options to suggest to the user. The * value must be the id of a element in the same document. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code list} property from the webcomponent */ protected String getListString() { return getElement().getProperty("list"); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Identifies a list of pre-defined options to suggest to the user. The * value must be the id of a element in the same document. *

* * @param list * the String value to set */ protected void setList(String list) { getElement().setProperty("list", list == null ? "" : list); } /** *

* Description copied from corresponding location in WebComponent: *

*

* A regular expression that the value is checked against. The pattern must * match the entire value, not just some subset. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code pattern} property from the webcomponent */ protected String getPatternString() { return getElement().getProperty("pattern"); } /** *

* Description copied from corresponding location in WebComponent: *

*

* A regular expression that the value is checked against. The pattern must * match the entire value, not just some subset. *

* * @param pattern * the String value to set */ protected void setPattern(String pattern) { getElement().setProperty("pattern", pattern == null ? "" : pattern); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Message to show to the user when validation fails. *

* This property is not synchronized automatically from the client side, so * the returned value may not be the same as in client side. *

* * @return the {@code title} property from the webcomponent */ protected String getTitleString() { return getElement().getProperty("title"); } /** *

* Description copied from corresponding location in WebComponent: *

*

* The text usually displayed in a tooltip popup when the mouse is over the * field. *

* * @param title * the String value to set */ protected void setTitle(String title) { getElement().setProperty("title", title == null ? "" : title); } /** *

* Description copied from corresponding location in WebComponent: *

*

* Returns true if the current input value satisfies all constraints (if * any) *

*

* This function is not supported by Flow because it returns a * boolean. Functions with return types different than void are * not supported at this moment. */ @NotSupported protected void checkValidity() { } /** *

* Description copied from corresponding location in WebComponent: *

*

* Returns true if {@code value} is valid. {@code } uses this to * check the validity or all its elements. *

*

* This function is not supported by Flow because it returns a * boolean. Functions with return types different than void are * not supported at this moment. */ @NotSupported protected void validate() { } @DomEvent("change") public static class ChangeEvent> extends ComponentEvent { public ChangeEvent(R source, boolean fromClient) { super(source, fromClient); } } /** * Adds a listener for {@code change} events fired by the webcomponent. * * @param listener * the listener * @return a {@link Registration} for removing the event listener */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected Registration addChangeListener( ComponentEventListener> listener) { return addListener(ChangeEvent.class, (ComponentEventListener) listener); } public static class InvalidChangeEvent> extends ComponentEvent { private final boolean invalid; public InvalidChangeEvent(R source, boolean fromClient) { super(source, fromClient); this.invalid = source.isInvalidBoolean(); } public boolean isInvalid() { return invalid; } } /** * Adds a listener for {@code invalid-changed} events fired by the * webcomponent. * * @param listener * the listener * @return a {@link Registration} for removing the event listener */ protected Registration addInvalidChangeListener( ComponentEventListener> listener) { return getElement() .addPropertyChangeListener("invalid", event -> listener.onComponentEvent( new InvalidChangeEvent((R) this, event.isUserOriginated()))); } /** * Adds the given components as children of this component at the slot * 'prefix'. * * @param components * The components to add. * @see MDN * page about slots * @see Spec * website about slots */ protected void addToPrefix(Component... components) { for (Component component : components) { component.getElement().setAttribute("slot", "prefix"); getElement().appendChild(component.getElement()); } } /** * Adds the given components as children of this component at the slot * 'input'. * * @param components * The components to add. * @see MDN * page about slots * @see Spec * website about slots */ protected void addToInput(Component... components) { for (Component component : components) { component.getElement().setAttribute("slot", "input"); getElement().appendChild(component.getElement()); } } /** * Adds the given components as children of this component at the slot * 'suffix'. * * @param components * The components to add. * @see MDN * page about slots * @see Spec * website about slots */ protected void addToSuffix(Component... components) { for (Component component : components) { component.getElement().setAttribute("slot", "suffix"); getElement().appendChild(component.getElement()); } } /** * Removes the given child components from this component. * * @param components * The components to remove. * @throws IllegalArgumentException * if any of the components is not a child of this component. */ protected void remove(Component... components) { for (Component component : components) { if (getElement().equals(component.getElement().getParent())) { component.getElement().removeAttribute("slot"); getElement().removeChild(component.getElement()); } else { throw new IllegalArgumentException("The given component (" + component + ") is not a child of this component"); } } } /** * Removes all contents from this component, this includes child components, * text content as well as child elements that have been added directly to * this component using the {@link Element} API. */ protected void removeAll() { getElement().getChildren() .forEach(child -> child.removeAttribute("slot")); getElement().removeAllChildren(); } /** * Constructs a new GeneratedVaadinTextField component with the given * arguments. * * @param initialValue * the initial value to set to the value * @param defaultValue * the default value to use if the value isn't defined * @param elementPropertyType * the type of the element property * @param presentationToModel * a function that converts a string value to a model value * @param modelToPresentation * a function that converts a model value to a string value * @param

* the property type */ public

GeneratedVaadinTextField(T initialValue, T defaultValue, Class

elementPropertyType, SerializableFunction presentationToModel, SerializableFunction modelToPresentation) { super("value", defaultValue, elementPropertyType, presentationToModel, modelToPresentation); if (initialValue != null) { setModelValue(initialValue, false); setPresentationValue(initialValue); } } /** * Constructs a new GeneratedVaadinTextField component with the given * arguments. * * @param initialValue * the initial value to set to the value * @param defaultValue * the default value to use if the value isn't defined * @param acceptNullValues * whether null is accepted as a model value */ public GeneratedVaadinTextField(T initialValue, T defaultValue, boolean acceptNullValues) { super("value", defaultValue, acceptNullValues); if (initialValue != null) { setModelValue(initialValue, false); setPresentationValue(initialValue); } } /** * Constructs a new GeneratedVaadinTextField component with the given * arguments. * * @param initialValue * the initial value to set to the value * @param defaultValue * the default value to use if the value isn't defined * @param elementPropertyType * the type of the element property * @param presentationToModel * a function that accepts this component and a property value * and returns a model value * @param modelToPresentation * a function that accepts this component and a model value and * returns a property value * @param

* the property type */ public

GeneratedVaadinTextField(T initialValue, T defaultValue, Class

elementPropertyType, SerializableBiFunction presentationToModel, SerializableBiFunction modelToPresentation) { super("value", defaultValue, elementPropertyType, presentationToModel, modelToPresentation); if (initialValue != null) { setModelValue(initialValue, false); setPresentationValue(initialValue); } } /** * Default constructor. */ public GeneratedVaadinTextField() { this(null, null, null, (SerializableFunction) null, (SerializableFunction) null); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy