summaryrefslogtreecommitdiffstats
path: root/documentation/components/components-textfield.asciidoc
blob: a14dedb5480aad55a8c279fa7e3185318c180224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
---
title: TextField
order: 9
layout: page
---

[[components.textfield]]
= TextField

ifdef::web[]
[.sampler]
image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/text-input/text-field"]
endif::web[]

((("[classname]#TextField#", id="term.components.textfield", range="startofrange")))

[classname]#TextField# is one of the most commonly used user interface components.
It is a field that allows entering textual values with keyboard.

The following example creates a simple text field:

[source, java]
----
// Create a text field
TextField tf = new TextField("A Field");

// Put some initial content in it
tf.setValue("Stuff in the field");
----

The result is shown in <<figure.components.textfield.basic>>.

[[figure.components.textfield.basic]]
.[classname]#TextField# Example
image::img/textfield-example.png[width=40%, scaledwidth=50%]

Value changes are handled by adding a listener using [methodname]#addValueChangeListener()# method, as in most other fields. The value can be acquired with [methodname]#getValue()# directly from the text field or from the parameter
passed to the event listener. 

[source, java]
----
// Handle changes in the value
tf.addValueChangeListener(event ->
        // Do something with the value
        Notification.show("Value is: " + event.getValue()));
----

[classname]#TextField# edits [classname]#String# values, but you can use [classname]#Binder#
to bind it to any property type that has a proper converter, as described in
<<dummy/../../../framework/datamodel/datamodel-forms.asciidoc#datamodel.forms.conversion,"Conversion">>.

Much of the API of [classname]#TextField# is defined in
[classname]#AbstractTextField#, which allows different kinds of text input
fields, which do not share all the features of the single-line text fields.

[[figure.components.textfield.api]]
.Text field class relationships
image::img/textfield-diagram-hi.png[width=80%, scaledwidth=100%]

[[components.textfield.length]]
== String Length

The [methodname]#setMaxLength()# method sets the maximum length of the input
string so that the browser prevents the user from entering a longer one. As a
security feature, the input value is automatically truncated on the server-side,
as the maximum length setting could be bypassed on the client-side. The maximum
length property is defined at [classname]#AbstractTextField# level.

Notice that the maximum length setting does not affect the width of the field.
You can set the width with [methodname]#setWidth()#, as with other components.
Using __em__ widths is recommended to better approximate the proper width in
relation to the size of the used font, but the __em__ width is not exactly the
width of a letter and varies by browser and operating system. There is no standard
way in HTML for setting the width exactly to a number of letters (in a monospaced font).

[[components.textfield.textchangeevents]]
== Configuring the Granularity of Value Change Events

((("[classname]#Text change events#", id="term.components.textfield.textchangeevents", range="startofrange")))

Often you want to control how frequently [classname]#TextField# value changes are transmitted to the server.
Sometimes the changes should be sent only after the field loses focus.
In the other extreme, it can sometimes be useful to receive events every time the user presses a key.

The __value change event mode__ defines how quickly the changes are transmitted
to the server and cause a server-side event. Lazier change events allow sending
larger changes in one event if the user is typing fast, thereby reducing server
requests.

((([classname]#ValueChangeMode#)))
You can set the text change event mode of a [classname]#TextField# with
[methodname]#setValueChangeMode()#. The allowed modes are defined in
[classname]#ValueChangeMode# enum and are as follows:

[parameter]#ValueChangeMode.LAZY#(default):: An event is triggered when there is a pause in editing the text. The length of
the pause can be modified with [methodname]#setValueChangeTimeout()#.

+
This is the default mode.

[parameter]#ValueChangeMode.TIMEOUT#:: A text change in the user interface causes the event to be communicated to the
application after a timeout period. If more changes are made during this period,
the event sent to the server-side includes the changes made up to the last
change. The length of the timeout can be set with
[methodname]#setValueChangeTimeout()#.

[parameter]#ValueChangeMode.EAGER#:: The [classname]#ValueChangeEvent# is triggered immediately for every change in the text content,
typically caused by a key press. The requests are separate and are processed
sequentially one after another. Change events are nevertheless communicated
asynchronously to the server, so further input can be typed while event requests
are being processed.

[parameter]#ValueChangeMode.BLUR#:: The [classname]#ValueChangeEvent# is fired, after the field has lost focus.

[source, java]
----
// Text field with maximum length
TextField tf = new TextField("My Eventful Field");
tf.setValue("Initial content");
tf.setMaxLength(20);

// Counter for input length
Label counter = new Label();
counter.setValue(tf.getValue().length() +
                 " of " + tf.getMaxLength());

// Display the current length interactively in the counter
tf.addValueChangeListener(event -> {
    int len = event.getValue().length();
    counter.setValue(len + " of " + tf.getMaxLength());
});

tf.setValueChangeMode(ValueChangeMode.EAGER);
----


The result is shown in <<figure.components.textfield.textchangeevents>>.

[[figure.components.textfield.textchangeevents]]
.Text Change Events
image::img/textfield-textchangeevents.png[width=35%, scaledwidth=50%]


(((range="endofrange", startref="term.components.textfield.textchangeevents")))

[[components.textfield.css]]
== CSS Style Rules

[source, css]
----
.v-textfield { }
----

The HTML structure of [classname]#TextField# is extremely simple, consisting
only of an element with the [literal]#++v-textfield++# style.

For example, the following custom style uses dashed border:

[source, css]
----
.v-textfield-dashing {
    border:     thin dashed;
}
----

The result is shown in <<figure.components.textfield.css>>.

[[figure.components.textfield.css]]
.Styling TextField with CSS
image::img/textfield-css.png[]

The style name for [classname]#TextField# is also used in several components
that contain a text input field, even if the text input is not an actual
[classname]#TextField#. This ensures that the style of different text input
boxes is similar.

(((range="endofrange", startref="term.components.textfield")))