Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.patternfly.component.form.TextInput Maven / Gradle / Ivy
package org.patternfly.component.form;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import org.jboss.elemento.InputElementBuilder;
import org.jboss.elemento.InputType;
import org.patternfly.component.ComponentType;
import org.patternfly.component.HasValue;
import org.patternfly.component.WithIcon;
import org.patternfly.component.WithText;
import org.patternfly.core.Aria;
import org.patternfly.handler.ChangeHandler;
import org.patternfly.style.Classes;
import org.patternfly.style.Modifiers.Plain;
import org.patternfly.style.Modifiers.Readonly;
import elemental2.dom.Element;
import elemental2.dom.Event;
import elemental2.dom.HTMLElement;
import elemental2.dom.HTMLInputElement;
import static org.jboss.elemento.Elements.failSafeRemoveFromParent;
import static org.jboss.elemento.Elements.input;
import static org.jboss.elemento.Elements.insertFirst;
import static org.jboss.elemento.Elements.removeChildrenFrom;
import static org.jboss.elemento.Elements.span;
import static org.jboss.elemento.Elements.wrapInputElement;
import static org.jboss.elemento.EventType.keyup;
import static org.patternfly.core.Aria.invalid;
import static org.patternfly.style.Classes.component;
import static org.patternfly.style.Classes.formControl;
import static org.patternfly.style.Classes.icon;
import static org.patternfly.style.Classes.modifier;
import static org.patternfly.style.Modifiers.toggleModifier;
public class TextInput extends FormControl implements
HasValue ,
Plain ,
Readonly ,
WithIcon ,
WithText {
public static TextInput textInput (String id) {
return new TextInput(TextInputType.text, id, null );
}
public static TextInput textInput (String id, String value) {
return new TextInput(TextInputType.text, id, value);
}
public static TextInput textInput (TextInputType type, String id) {
return new TextInput(type, id, null );
}
public static TextInput textInput (TextInputType type, String id, String value) {
return new TextInput(type, id, value);
}
private static final Map typeMapping = new HashMap<>();
static {
typeMapping.put(TextInputType.date, InputType.date);
typeMapping.put(TextInputType.email, InputType.email);
typeMapping.put(TextInputType.month, InputType.month);
typeMapping.put(TextInputType.number, InputType.number);
typeMapping.put(TextInputType.search, InputType.search);
typeMapping.put(TextInputType.tel, InputType.tel);
typeMapping.put(TextInputType.text, InputType.text);
typeMapping.put(TextInputType.time, InputType.time);
typeMapping.put(TextInputType.password, InputType.password);
}
private final HTMLInputElement inputElement;
private final List> changeHandlers;
private HTMLElement iconContainer;
TextInput(TextInputType type, String id, String value) {
super (id, formControlContainer()
.add(input(typeMapping.getOrDefault(type, InputType.text))
.id(id)
.name(id)
.aria(invalid, false ))
.element(),
ComponentType.TextInput);
this .changeHandlers = new ArrayList<>();
inputElement = (HTMLInputElement) element().firstElementChild;
inputElement.addEventListener(keyup.name, e -> changeHandlers.forEach(ch -> ch.onChange(e, this , inputElement.value)));
if (value != null ) {
value(value);
}
}
@Override
public TextInput readonly (boolean readonly) {
inputElement.readOnly = readonly;
return Readonly.super .readonly(readonly);
}
public TextInput expanded () {
return expanded(true );
}
public TextInput expanded (boolean expanded) {
aria(Aria.expanded, expanded);
return toggleModifier(that(), element(), Classes.expanded, expanded);
}
@Override
public TextInput plain (boolean plain) {
if (plain) {
readonly();
}
return Plain.super .plain(plain);
}
@Override
public TextInput required (boolean required) {
inputElement.required = required;
return this ;
}
@Override
public TextInput text (String text) {
return value(text);
}
public TextInput value (String value) {
return value(value, false );
}
public TextInput value (String value, boolean fireEvent) {
boolean changed = !Objects.equals(inputElement.value, value);
inputElement.value = value;
if (fireEvent && changed && !changeHandlers.isEmpty()) {
changeHandlers.forEach(ch -> ch.onChange(new Event("" ), this , value));
}
return this ;
}
public TextInput placeholder (String placeholder) {
inputElement.placeholder = placeholder;
return this ;
}
public TextInput applyTo (Consumer > consumer) {
consumer.accept(inputElement());
return this ;
}
@Override
public TextInput icon (Element icon) {
css(modifier(Classes.icon));
if (iconContainer == null ) {
insertFirst(failSafeUtilitiesContainer(), iconContainer = span().css(component(formControl, Classes.icon))
.element());
}
removeChildrenFrom(iconContainer);
iconContainer.appendChild(icon);
return this ;
}
@Override
public TextInput removeIcon () {
failSafeRemoveFromParent(iconContainer);
if (utilitiesContainer != null && utilitiesContainer.childElementCount == 0 ) {
failSafeRemoveFromParent(utilitiesContainer);
}
element().classList.remove(modifier(icon));
return this ;
}
@Override
public TextInput that () {
return this ;
}
public TextInput onChange (ChangeHandler changeHandler) {
changeHandlers.add(changeHandler);
return this ;
}
@Override
public String value () {
return inputElement.value;
}
public InputElementBuilder inputElement () {
return wrapInputElement(inputElement);
}
@Override
public String text () {
return value();
}
@Override
void disableInputElement (boolean disabled) {
inputElement.disabled = disabled;
}
}