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

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

The newest version!
/*
 * Copyright 2000-2024 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;

import java.util.Optional;

import com.vaadin.flow.component.AbstractField;
import com.vaadin.flow.component.AbstractSinglePropertyField;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.CompositionNotifier;
import com.vaadin.flow.component.Focusable;
import com.vaadin.flow.component.HasAriaLabel;
import com.vaadin.flow.component.HasPlaceholder;
import com.vaadin.flow.component.InputNotifier;
import com.vaadin.flow.component.KeyNotifier;
import com.vaadin.flow.component.shared.HasClearButton;
import com.vaadin.flow.component.shared.HasClientValidation;
import com.vaadin.flow.component.shared.HasValidationProperties;
import com.vaadin.flow.component.shared.InputField;
import com.vaadin.flow.component.shared.SlotUtils;
import com.vaadin.flow.data.binder.HasValidator;
import com.vaadin.flow.data.value.HasValueChangeMode;
import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.function.SerializableBiFunction;
import com.vaadin.flow.function.SerializableFunction;

/**
 * Internal class that provides base functionality for input field components,
 * such as {@link TextField}. Not intended to be used publicly.
 *
 * @param 
 *            Type of the component that extends from this class
 * @param 
 *            Type of the value of the extending component
 */
public abstract class TextFieldBase, TValue>
        extends AbstractSinglePropertyField
        implements CompositionNotifier, Focusable, HasAriaLabel,
        HasAutocapitalize, HasAutocomplete, HasAutocorrect, HasClearButton,
        HasClientValidation, HasPrefixAndSuffix, HasValidationProperties,
        HasValidator, HasValueChangeMode, HasPlaceholder, InputNotifier,
        KeyNotifier,
        InputField, TValue> {

    private ValueChangeMode currentMode;

    private int valueChangeTimeout = DEFAULT_CHANGE_TIMEOUT;

    /**
     * {@inheritDoc}
     * 

* The default value is {@link ValueChangeMode#ON_CHANGE}. */ @Override public ValueChangeMode getValueChangeMode() { return currentMode; } @Override public void setValueChangeMode(ValueChangeMode valueChangeMode) { currentMode = valueChangeMode; setSynchronizedEvent( ValueChangeMode.eventForMode(valueChangeMode, "input")); applyChangeTimeout(); } @Override public void setValueChangeTimeout(int valueChangeTimeout) { this.valueChangeTimeout = valueChangeTimeout; applyChangeTimeout(); } @Override public int getValueChangeTimeout() { return valueChangeTimeout; } void applyChangeTimeout() { ValueChangeMode.applyChangeTimeout(getValueChangeMode(), getValueChangeTimeout(), getSynchronizationRegistration()); } /** * Sets the whether the component should automatically receive focus when * the page loads. Defaults to {@code false}. * * @param autofocus * {@code true} component should automatically receive focus */ public void setAutofocus(boolean autofocus) { getElement().setProperty("autofocus", autofocus); } /** * Specify that this control should have input focus when the page loads. * * @return the {@code autofocus} property from the webcomponent */ public boolean isAutofocus() { return getElement().getProperty("autofocus", false); } /** * The text usually displayed in a tooltip popup when the mouse is over the * field. * * @return the {@code title} property from the webcomponent */ public String getTitle() { return getElement().getProperty("title"); } /** * The text usually displayed in a tooltip popup when the mouse is over the * field. * * @param title * the String value to set */ public void setTitle(String title) { getElement().setProperty("title", title == null ? "" : title); } /** * Specifies if the field value gets automatically selected when the field * gains focus. * * @return true if autoselect is active, false * otherwise */ public boolean isAutoselect() { return getElement().getProperty("autoselect", false); } /** * Set to true to always have the field value automatically * selected when the field gains focus, false otherwise. * * @param autoselect * true to set auto select on, false * otherwise */ public void setAutoselect(boolean autoselect) { getElement().setProperty("autoselect", autoselect); } /** * Specifies that the user must fill in a value. * * @param required * the boolean value to set */ public void setRequired(boolean required) { getElement().setProperty("required", required); } /** * Determines whether the field is marked as input required. * * @return {@code true} if the input is required, {@code false} otherwise */ public boolean isRequired() { return getElement().getProperty("required", false); } @Override public void setLabel(String label) { InputField.super.setLabel(label); } @Override public void setAriaLabel(String ariaLabel) { getElement().setProperty("accessibleName", ariaLabel); } @Override public Optional getAriaLabel() { return Optional.ofNullable(getElement().getProperty("accessibleName")); } @Override public void setAriaLabelledBy(String labelledBy) { getElement().setProperty("accessibleNameRef", labelledBy); } @Override public Optional getAriaLabelledBy() { return Optional .ofNullable(getElement().getProperty("accessibleNameRef")); } /** * 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) { SlotUtils.addToSlot(this, "prefix", components); } /** * 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) { SlotUtils.addToSlot(this, "input", components); } /** * 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) { SlotUtils.addToSlot(this, "suffix", components); } /** * Constructs a new component with the given arguments. *

* If {@code isInitialValueOptional} is {@code true} then the initial value * is used only if element has no {@code "value"} property value, otherwise * element {@code "value"} property is ignored and the initial value is set. * * @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 isInitialValueOptional * if {@code isInitialValueOptional} is {@code true} then the * initial value is used only if element has no {@code "value"} * property value, otherwise element {@code "value"} property is * ignored and the initial value is set * @param

* the property type */

TextFieldBase(TValue initialValue, TValue defaultValue, Class

elementPropertyType, SerializableFunction presentationToModel, SerializableFunction modelToPresentation, boolean isInitialValueOptional) { super("value", defaultValue, elementPropertyType, presentationToModel, modelToPresentation); if ((getElement().getProperty("value") == null || !isInitialValueOptional)) { setPresentationValue(initialValue); } } /** * Constructs a new component with the given arguments. *

* If {@code isInitialValueOptional} is {@code true} then the initial value * is used only if element has no {@code "value"} property value, otherwise * element {@code "value"} property is ignored and the initial value is set. * * @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 isInitialValueOptional * if {@code isInitialValueOptional} is {@code true} then the * initial value is used only if element has no {@code "value"} * property value, otherwise element {@code "value"} property is * ignored and the initial value is set * @param

* the property type */

TextFieldBase(TValue initialValue, TValue defaultValue, Class

elementPropertyType, SerializableBiFunction presentationToModel, SerializableBiFunction modelToPresentation, boolean isInitialValueOptional) { super("value", defaultValue, elementPropertyType, presentationToModel, modelToPresentation); if ((getElement().getProperty("value") == null || !isInitialValueOptional)) { setPresentationValue(initialValue); } } /** * Constructs a new component with the given arguments. *

* If {@code isInitialValueOptional} is {@code true} then the initial value * is used only if element has no {@code "value"} property value, otherwise * element {@code "value"} property is ignored and the initial value is set. * * @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 * @param isInitialValueOptional * if {@code isInitialValueOptional} is {@code true} then the * initial value is used only if element has no {@code "value"} * property value, otherwise element {@code "value"} property is * ignored and the initial value is set */ TextFieldBase(TValue initialValue, TValue defaultValue, boolean acceptNullValues, boolean isInitialValueOptional) { super("value", defaultValue, acceptNullValues); if ((getElement().getProperty("value") == null || !isInitialValueOptional)) { setPresentationValue(initialValue); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy