UNPKG

11.4 kBMarkdownView Raw
1## Introduction
2
3The Material Design Lite (MDL) **text field** component is an enhanced version of the standard HTML `<input type="text">` and `<input type="textarea">` elements. A text field consists of a horizontal line indicating where keyboard input can occur and, typically, text that clearly communicates the intended contents of the text field. The MDL text field component provides various types of text fields, and allows you to add both display and click effects.
4
5Text fields are a common feature of most user interfaces, regardless of a site's content or function. Their design and use is therefore an important factor in the overall user experience. See the text field component's [Material Design specifications page](http://www.google.com/design/spec/components/text-fields.html) for details.
6
7The enhanced text field component has a more vivid visual look than a standard text field, and may be initially or programmatically *disabled*.
8There are three main types of text fields in the text field component, each with its own basic coding requirements. The types are *single-line*, *multi-line*, and *expandable*.
9
10### To include a *single-line* MDL **text field** component:
11
12&nbsp;1. Code a `<div>` element to hold the text field.
13```html
14<div>
15...
16</div>
17```
18&nbsp;2. Inside the div, code an `<input>` element with a `type` attribute of `"text"` (the text field), and an `id` attribute of your choice.
19```html
20<div>
21 <input type="text" id="user">
22</div>
23```
24&nbsp;3. Also inside the div, after the text field, code a `<label>` element with a `for` attribute whose value matches the `input` element's `id` value, and a short string to be used as the field's placeholder text.
25```html
26<div>
27 <input type="text" id="user">
28 <label for="user">User name</label>
29</div>
30```
31&nbsp;4. Optionally, add a `pattern` attribute and value to the `<input>` element (see the [W3C HTML5 forms specification](http://www.w3.org/TR/html5/forms.html#the-pattern-attribute) for details) and an associated error message in a `<span>` element following the `<label>`.
32```html
33<div>
34 <input type="text" id="user" pattern="[A-Z,a-z, ]*">
35 <label for="user">User name</label>
36 <span>Letters and spaces only</span>
37</div>
38```
39&nbsp;5. Add one or more MDL classes, separated by spaces, to the div container, text field, field label, and error message using the `class` attribute.
40```html
41<div class="mdl-textfield mdl-js-textfield">
42 <input class="mdl-textfield__input" type="text" id="user" pattern="[A-Z,a-z, ]*">
43 <label class="mdl-textfield__label" for="user">User name</label>
44 <span class="mdl-textfield__error">Letters and spaces only</span>
45</div>
46```
47The single-line text field component is ready for use.
48
49#### Examples
50
51Single-line text field with a standard label.
52```html
53<div class="mdl-textfield mdl-js-textfield">
54 <input class="mdl-textfield__input" type="text" id="fname">
55 <label class="mdl-textfield__label" for="fname">First name</label>
56</div>
57```
58
59Single-line text field with a floating label.
60```html
61<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
62 <input class="mdl-textfield__input" type="text" id="addr1">
63 <label class="mdl-textfield__label" for="addr1">Address line 1</label>
64</div>
65```
66
67Single-line text field with a standard label, pattern matching, and error message.
68```html
69<div class="mdl-textfield mdl-js-textfield">
70 <input class="mdl-textfield__input" type="text" pattern="[0-9]*" id="phone">
71 <label class="mdl-textfield__label" for="phone">Phone</label>
72 <span class="mdl-textfield__error">Digits only</span>
73</div>
74```
75
76### To include a *multi-line* MDL **text field** component:
77
78&nbsp;1. Code a `<div>` element to hold the text field.
79```html
80<div>
81...
82</div>
83```
84&nbsp;2. Inside the div, code a `<textarea>` element with a `type` attribute of `"text"` (the multi-line text field), and an `id` attribute of your choice. Include a `rows` attribute with a value of `"1"` (this attribute sets the number of *concurrently visible* input rows).
85```html
86<div>
87 <textarea type="text" rows="1" id="address"></textarea>
88</div>
89```
90&nbsp;3. Also inside the div, after the text field, code a `<label>` element with a `for` attribute whose value matches the `<textarea>` element's `id` value, and a short string to be used as the field's placeholder text.
91```html
92<div>
93 <textarea type="text" rows="1" id="address"></textarea>
94 <label for="address">Full address</label>
95</div>
96```
97&nbsp;4. Add one or more MDL classes, separated by spaces, to the div container, text field, and field label using the `class` attribute.
98```html
99<div class="mdl-textfield mdl-js-textfield">
100 <textarea class="mdl-textfield__input" type="text" rows="1" id="address"></textarea>
101 <label class="mdl-textfield__label" for="address">Full address</label>
102</div>
103```
104
105The multi-line text field component is ready for use.
106
107#### Examples
108
109Multi-line text field with one visible input line.
110```html
111<div class="mdl-textfield mdl-js-textfield">
112 <textarea class="mdl-textfield__input" type="text" rows="1" id="schools"></textarea>
113 <label class="mdl-textfield__label" for="schools">Schools attended</label>
114</div>
115```
116
117Multi-line text field with one visible input line and floating label.
118```html
119<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
120 <textarea class="mdl-textfield__input" type="text" rows= "1" id="schools"></textarea>
121 <label class="mdl-textfield__label" for="schools">Schools attended</label>
122</div>
123```
124
125Multi-line text field with multiple visible input lines and a maximum number of lines.
126```html
127<div class="mdl-textfield mdl-js-textfield">
128 <textarea class="mdl-textfield__input" type="text" rows="3" maxrows="6"
129 id="schools"></textarea>
130 <label class="mdl-textfield__label" for="schools">Schools attended (max. 6)</label>
131</div>
132```
133
134### To include an *expandable* MDL **text field** component:
135
136&nbsp;1. Code an "outer" `<div>` element to hold the expandable text field.
137```html
138<div>
139...
140</div>
141```
142&nbsp;2. Inside the div, code a `<label>` element with a `for` attribute whose value will match the `<input>` element's `id` value (to be coded in step 5).
143```html
144<div>
145 <label for="expando1">
146 ...
147 </label>
148</div>
149```
150&nbsp;3. Inside the label, code a `<span>` element; the span should be empty, and should be the label's only content. This element will contain the expandable text field's icon.
151```html
152<div>
153 <label for="expando1">
154 <span></span>
155 </label>
156</div>
157```
158&nbsp;4. Still inside the "outer" div, after the label containing the span, code an "inner" (nested) `<div>` element.
159```html
160<div>
161 <label for="expando1">
162 <span></span>
163 </label>
164 <div>
165 ...
166 </div>
167</div>
168```
169&nbsp;5. Inside the "inner" div, code an `<input>` element with a `type` attribute of `"text"` (the text field), and an `id` attribute whose value matches that of the `for` attribute in step 2.
170```html
171<div>
172 <label for="expando1">
173 <span></span>
174 </label>
175 <div>
176 <input type="text" id="expando1">
177 </div>
178</div>
179```
180&nbsp;6. Still inside the "inner" div, after the text field, code a `<label>` element with a `for` attribute whose value also matches the `<input>` element's `id` value (coded in step 5), and a short string to be used as the field's placeholder text.
181```html
182<div>
183 <label for="expando1">
184 <span></span>
185 </label>
186 <div>
187 <input type="text" id="expando1">
188 <label for="expando1">Expandable text field</label>
189 </div>
190</div>
191```
192&nbsp;7. Add one or more MDL classes, separated by spaces, to the "outer" div container, label, and span, and to the "inner" div container, text field, and field label using the `class` attribute.
193```html
194<div class="mdl-textfield mdl-js-textfield mdl-textfield--expandable">
195 <label class="mdl-button mdl-js-button mdl-button--icon" for="expando1">
196 <i class="material-icons">search</i>
197 </label>
198 <div class="mdl-textfield__expandable-holder">
199 <input class="mdl-textfield__input" type="text" id="expando1">
200 <label class="mdl-textfield__label" for="expando1">Expandable text field</label>
201 </div>
202</div>
203```
204
205The expandable text field component is ready for use. It will expand when the icon (the empty `<span>`) is clicked or gains focus.
206
207#### Examples
208
209Expandable text field with a standard label.
210```html
211<div class="mdl-textfield mdl-js-textfield mdl-textfield--expandable">
212 <label class="mdl-button mdl-js-button mdl-button--icon" for="search-expandable">
213 <i class="material-icons">search</i>
214 </label>
215 <div class="mdl-textfield__expandable-holder">
216 <input class="mdl-textfield__input" type="text" id="search-expandable">
217 <label class="mdl-textfield__label" for="search-expandable">Search text</label>
218 </div>
219</div>
220```
221
222Expandable text field with a floating label.
223```html
224<div class="mdl-textfield mdl-js-textfield mdl-textfield--expandable
225 mdl-textfield--floating-label">
226 <label class="mdl-button mdl-js-button mdl-button--icon" for="search-expandable2">
227 <i class="material-icons">search</i>
228 </label>
229 <div class="mdl-textfield__expandable-holder">
230 <input class="mdl-textfield__input" type="text" id="search-expandable2">
231 <label class="mdl-textfield__label" for="search-expandable2">
232 Enter search text below
233 </label>
234 </div>
235</div>
236```
237## Configuration options
238
239The MDL CSS classes apply various predefined visual and behavioral enhancements to the text field. The table below lists the available classes and their effects.
240
241| MDL class | Effect | Remarks |
242|-----------|--------|---------|
243| `mdl-textfield` | Defines container as an MDL component | Required on "outer" div element|
244| `mdl-js-textfield` | Assigns basic MDL behavior to input | Required on "outer" div element |
245| `mdl-textfield__input` | Defines element as textfield input | Required on input or textarea element |
246| `mdl-textfield__label` | Defines element as textfield label | Required on label element for input or textarea elements |
247| `mdl-textfield--floating-label` | Applies *floating label* effect | Optional; goes on "outer" div element |
248| `mdl-textfield__error` | Defines span as an MDL error message | Optional; goes on span element for MDL input element with *pattern*|
249| `mdl-textfield--expandable` | Defines a div as an MDL expandable text field container | For expandable input fields, required on "outer" div element |
250| `mdl-button` | Defines label as an MDL icon button | For expandable input fields, required on "outer" div's label element |
251| `mdl-js-button` | Assigns basic behavior to icon container | For expandable input fields, required on "outer" div's label element |
252| `mdl-button--icon` | Defines label as an MDL icon container | For expandable input fields, required on "outer" div's label element |
253| `mdl-input__expandable-holder` | Defines a container as an MDL component | For expandable input fields, required on "inner" div element |
254| `is-invalid` | Defines the textfield as invalid on initial load. | Optional on `mdl-textfield` element |
255
256(1) The "search" icon is used here as an example. Other icons can be used by modifying the text. For a list of available icons, see [this page](https://www.google.com/design/icons).
257
258>**Note:** Disabled versions of each text field type are provided, and are invoked with the standard HTML boolean attribute `disabled`. `<input class="mdl-textfield mdl-js-textfield" type="text" disabled>`
259>This attribute may be added or removed programmatically via scripting.