OLD | NEW |
| (Empty) |
1 <!-- | |
2 @license | |
3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | |
4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | |
5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | |
7 Code distributed by Google as part of the polymer project is also | |
8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | |
9 --> | |
10 | |
11 <link rel="import" href="../polymer/polymer.html"> | |
12 <link rel="import" href="../paper-styles/paper-styles.html"> | |
13 | |
14 <!-- | |
15 `<paper-input-container>` is a container for a `<label>`, an `<input is="iron-in
put">` or | |
16 `<iron-autogrow-textarea>` and optional add-on elements such as an error message
or character | |
17 counter, used to implement Material Design text fields. | |
18 | |
19 For example: | |
20 | |
21 <paper-input-container> | |
22 <label>Your name</label> | |
23 <input is="iron-input"> | |
24 </paper-input-container> | |
25 | |
26 ### Listening for input changes | |
27 | |
28 By default, it listens for changes on the `bind-value` attribute on its children
nodes and perform | |
29 tasks such as auto-validating and label styling when the `bind-value` changes. Y
ou can configure | |
30 the attribute it listens to with the `attr-for-value` attribute. | |
31 | |
32 ### Using a custom input element | |
33 | |
34 You can use a custom input element in a `<paper-input-container>`, for example t
o implement a | |
35 compound input field like a social security number input. The custom input eleme
nt should have the | |
36 `paper-input-input` class, have a `notify:true` value property and optionally im
plements | |
37 `Polymer.IronValidatableBehavior` if it is validatble. | |
38 | |
39 <paper-input-container attr-for-value="ssn-value"> | |
40 <label>Social security number</label> | |
41 <ssn-input class="paper-input-input"></ssn-input> | |
42 </paper-input-container> | |
43 | |
44 ### Validation | |
45 | |
46 If the `auto-validate` attribute is set, the input container will validate the i
nput and update | |
47 the container styling when the input value changes. | |
48 | |
49 ### Add-ons | |
50 | |
51 Add-ons are child elements of a `<paper-input-container>` with the `add-on` attr
ibute and | |
52 implements the `Polymer.PaperInputAddonBehavior` behavior. They are notified whe
n the input value | |
53 or validity changes, and may implement functionality such as error messages or c
haracter counters. | |
54 They appear at the bottom of the input. | |
55 | |
56 ### Styling | |
57 | |
58 The following custom properties and mixins are available for styling: | |
59 | |
60 Custom property | Description | Default | |
61 ----------------|-------------|---------- | |
62 `--paper-input-container-color` | Label and underline color when the input is no
t focused | `--secondary-text-color` | |
63 `--paper-input-container-focus-color` | Label and underline color when the input
is focused | `--default-primary-color` | |
64 `--paper-input-container-invalid-color` | Label and underline color when the inp
ut is focused | `--google-red-500` | |
65 `--paper-input-container-input-color` | Input foreground color | `--primary-text
-color` | |
66 `--paper-input-container` | Mixin applied to the container | `{}` | |
67 `--paper-input-container-label` | Mixin applied to the label | `{}` | |
68 `--paper-input-container-input` | Mixin applied to the input | `{}` | |
69 | |
70 This element is `display:block` by default, but you can set the `inline` attribu
te to make it | |
71 `display:inline-block`. | |
72 --> | |
73 <dom-module id="paper-input-container"> | |
74 | |
75 <style> | |
76 | |
77 :host { | |
78 display: block; | |
79 padding: 8px 0; | |
80 | |
81 @apply(--paper-input-container); | |
82 } | |
83 | |
84 :host[inline] { | |
85 display: inline-block; | |
86 } | |
87 | |
88 :host([disabled]) { | |
89 pointer-events: none; | |
90 opacity: 0.33; | |
91 } | |
92 | |
93 .floated-label-placeholder { | |
94 @apply(--paper-font-caption); | |
95 } | |
96 | |
97 .underline { | |
98 position: relative; | |
99 } | |
100 | |
101 .focused-line { | |
102 height: 2px; | |
103 | |
104 -webkit-transform-origin: center center; | |
105 transform-origin: center center; | |
106 -webkit-transform: scale3d(0,1,1); | |
107 transform: scale3d(0,1,1); | |
108 | |
109 background: var(--paper-input-container-focus-color, --default-primary-col
or); | |
110 } | |
111 | |
112 .is-highlighted .focused-line { | |
113 -webkit-transform: none; | |
114 transform: none; | |
115 -webkit-transition: -webkit-transform 0.25s; | |
116 transition: transform 0.25s; | |
117 | |
118 @apply(--paper-transition-easing); | |
119 } | |
120 | |
121 .is-invalid .focused-line { | |
122 background: var(--paper-input-container-invalid-color, --google-red-500); | |
123 | |
124 -webkit-transform: none; | |
125 transform: none; | |
126 -webkit-transition: -webkit-transform 0.25s; | |
127 transition: transform 0.25s; | |
128 | |
129 @apply(--paper-transition-easing); | |
130 } | |
131 | |
132 .unfocused-line { | |
133 height: 1px; | |
134 background: var(--paper-input-container-color, --secondary-text-color); | |
135 } | |
136 | |
137 :host([disabled]) .unfocused-line { | |
138 border-bottom: 1px dashed; | |
139 border-color: var(--paper-input-container-color, --secondary-text-color); | |
140 background: transparent; | |
141 } | |
142 | |
143 .input-content { | |
144 position: relative; | |
145 } | |
146 | |
147 .input-content ::content label, | |
148 .input-content ::content .paper-input-label { | |
149 position: absolute; | |
150 top: 0; | |
151 right: 0; | |
152 left: 0; | |
153 font: inherit; | |
154 color: var(--paper-input-container-color, --secondary-text-color); | |
155 | |
156 @apply(--paper-font-subhead); | |
157 @apply(--paper-input-container-label); | |
158 } | |
159 | |
160 .input-content.label-is-floating ::content label, | |
161 .input-content.label-is-floating ::content .paper-input-label { | |
162 -webkit-transform: translate3d(0, -75%, 0) scale(0.75); | |
163 transform: translate3d(0, -75%, 0) scale(0.75); | |
164 -webkit-transform-origin: left top; | |
165 transform-origin: left top; | |
166 -webkit-transition: -webkit-transform 0.25s; | |
167 transition: transform 0.25s; | |
168 | |
169 @apply(--paper-transition-easing); | |
170 } | |
171 | |
172 .input-content.label-is-highlighted ::content label, | |
173 .input-content.label-is-highlighted ::content .paper-input-label { | |
174 color: var(--paper-input-container-focus-color, --default-primary-color); | |
175 } | |
176 | |
177 .input-content.is-invalid ::content label, | |
178 .input-content.is-invalid ::content .paper-input-label { | |
179 color: var(--paper-input-container-invalid-color, --google-red-500); | |
180 } | |
181 | |
182 .input-content.label-is-hidden ::content label, | |
183 .input-content.label-is-hidden ::content .paper-input-label { | |
184 visibility: hidden; | |
185 } | |
186 | |
187 .input-content ::content input, | |
188 .input-content ::content textarea, | |
189 .input-content ::content iron-autogrow-textarea, | |
190 .input-content ::content .paper-input-input { | |
191 position: relative; /* to make a stacking context */ | |
192 outline: none; | |
193 box-shadow: none; | |
194 padding: 0; | |
195 width: 100%; | |
196 background: transparent; | |
197 border: none; | |
198 color: var(--paper-input-container-input-color, --primary-text-color); | |
199 | |
200 @apply(--paper-font-subhead); | |
201 @apply(--paper-input-container-input); | |
202 } | |
203 | |
204 /* Firefox sets a min-width on the input, which can cause layout issues */ | |
205 .input-content ::content input { | |
206 min-width: 0; | |
207 } | |
208 | |
209 .input-content ::content textarea { | |
210 resize: none; | |
211 } | |
212 | |
213 .add-on-content.is-invalid ::content * { | |
214 color: var(--paper-input-container-invalid-color, --google-red-500); | |
215 } | |
216 | |
217 .add-on-content.is-highlighted ::content * { | |
218 color: var(--paper-input-container-focus-color, --default-primary-color); | |
219 } | |
220 | |
221 </style> | |
222 | |
223 <template> | |
224 | |
225 <template is="dom-if" if="[[!noLabelFloat]]"> | |
226 <div class="floated-label-placeholder"> </div> | |
227 </template> | |
228 | |
229 <div class$="[[_computeInputContentClass(noLabelFloat,alwaysFloatLabel,focus
ed,invalid,_inputHasContent)]]"> | |
230 <content select=":not([add-on])"></content> | |
231 </div> | |
232 | |
233 <div class$="[[_computeUnderlineClass(focused,invalid)]]"> | |
234 <div class="unfocused-line fit"></div> | |
235 <div class="focused-line fit"></div> | |
236 </div> | |
237 | |
238 <div class$="[[_computeAddOnContentClass(focused,invalid)]]"> | |
239 <content id="addOnContent" select="[add-on]"></content> | |
240 </div> | |
241 | |
242 </template> | |
243 | |
244 </dom-module> | |
245 | |
246 <script> | |
247 (function() { | |
248 | |
249 Polymer({ | |
250 | |
251 is: 'paper-input-container', | |
252 | |
253 properties: { | |
254 | |
255 /** | |
256 * Set to true to disable the floating label. The label disappears when th
e input value is | |
257 * not null. | |
258 */ | |
259 noLabelFloat: { | |
260 type: Boolean, | |
261 value: false | |
262 }, | |
263 | |
264 /** | |
265 * Set to true to always float the floating label. | |
266 */ | |
267 alwaysFloatLabel: { | |
268 type: Boolean, | |
269 value: false | |
270 }, | |
271 | |
272 /** | |
273 * The attribute to listen for value changes on. | |
274 */ | |
275 attrForValue: { | |
276 type: String, | |
277 value: 'bind-value' | |
278 }, | |
279 | |
280 /** | |
281 * Set to true to auto-validate the input value when it changes. | |
282 */ | |
283 autoValidate: { | |
284 type: Boolean, | |
285 value: false | |
286 }, | |
287 | |
288 /** | |
289 * True if the input is invalid. This property is set automatically when t
he input value | |
290 * changes if auto-validating, or when the `iron-input-valid` event is hea
rd from a child. | |
291 */ | |
292 invalid: { | |
293 observer: '_invalidChanged', | |
294 type: Boolean, | |
295 value: false | |
296 }, | |
297 | |
298 /** | |
299 * True if the input has focus. | |
300 */ | |
301 focused: { | |
302 readOnly: true, | |
303 type: Boolean, | |
304 value: false | |
305 }, | |
306 | |
307 _addons: { | |
308 type: Array, | |
309 value: function() { | |
310 return []; | |
311 } | |
312 }, | |
313 | |
314 _inputHasContent: { | |
315 type: Boolean, | |
316 value: false | |
317 }, | |
318 | |
319 _inputSelector: { | |
320 type: String, | |
321 value: 'input,textarea,.paper-input-input' | |
322 }, | |
323 | |
324 _boundOnFocus: { | |
325 type: Function, | |
326 value: function() { | |
327 return this._onFocus.bind(this); | |
328 } | |
329 }, | |
330 | |
331 _boundOnBlur: { | |
332 type: Function, | |
333 value: function() { | |
334 return this._onBlur.bind(this); | |
335 } | |
336 }, | |
337 | |
338 _boundOnInput: { | |
339 type: Function, | |
340 value: function() { | |
341 this._onInput.bind(this) | |
342 } | |
343 }, | |
344 | |
345 _boundValueChanged: { | |
346 type: Function, | |
347 value: function() { | |
348 return this._onValueChanged.bind(this); | |
349 } | |
350 } | |
351 | |
352 }, | |
353 | |
354 listeners: { | |
355 'addon-attached': '_onAddonAttached', | |
356 'iron-input-validate': '_onIronInputValidate' | |
357 }, | |
358 | |
359 get _valueChangedEvent() { | |
360 return this.attrForValue + '-changed'; | |
361 }, | |
362 | |
363 get _propertyForValue() { | |
364 return Polymer.CaseMap.dashToCamelCase(this.attrForValue); | |
365 }, | |
366 | |
367 get _inputElement() { | |
368 return Polymer.dom(this).querySelector(this._inputSelector); | |
369 }, | |
370 | |
371 ready: function() { | |
372 this.addEventListener('focus', this._boundOnFocus, true); | |
373 this.addEventListener('blur', this._boundOnBlur, true); | |
374 if (this.attrForValue) { | |
375 this._inputElement.addEventListener(this._valueChangedEvent, this._bound
ValueChanged); | |
376 } else { | |
377 this.addEventListener('input', this._onInput); | |
378 } | |
379 }, | |
380 | |
381 attached: function() { | |
382 this._handleValue(this._inputElement); | |
383 }, | |
384 | |
385 _onAddonAttached: function(event) { | |
386 this._addons.push(event.target); | |
387 this._handleValue(this._inputElement); | |
388 }, | |
389 | |
390 _onFocus: function() { | |
391 this._setFocused(true); | |
392 }, | |
393 | |
394 _onBlur: function() { | |
395 this._setFocused(false); | |
396 }, | |
397 | |
398 _onInput: function(event) { | |
399 this._handleValue(event.target); | |
400 }, | |
401 | |
402 _onValueChanged: function(event) { | |
403 this._handleValue(event.target); | |
404 }, | |
405 | |
406 _handleValue: function(inputElement) { | |
407 var value = inputElement[this._propertyForValue] || inputElement.value; | |
408 | |
409 if (this.autoValidate) { | |
410 var valid; | |
411 if (inputElement.validate) { | |
412 valid = inputElement.validate(value); | |
413 } else { | |
414 valid = inputElement.checkValidity(); | |
415 } | |
416 this.invalid = !valid; | |
417 } | |
418 | |
419 // type="number" hack needed because this.value is empty until it's valid | |
420 if (value || (inputElement.type === 'number' && !inputElement.checkValidit
y())) { | |
421 this._inputHasContent = true; | |
422 } else { | |
423 this._inputHasContent = false; | |
424 } | |
425 | |
426 this.updateAddons({ | |
427 inputElement: inputElement, | |
428 value: value, | |
429 invalid: this.invalid | |
430 }); | |
431 }, | |
432 | |
433 _onIronInputValidate: function(event) { | |
434 this.invalid = this._inputElement.invalid; | |
435 }, | |
436 | |
437 _invalidChanged: function() { | |
438 if (this._addons) { | |
439 this.updateAddons({invalid: this.invalid}); | |
440 } | |
441 }, | |
442 | |
443 /** | |
444 * Call this to update the state of add-ons. | |
445 * @param {Object} state Add-on state. | |
446 */ | |
447 updateAddons: function(state) { | |
448 for (var addon, index = 0; addon = this._addons[index]; index++) { | |
449 addon.update(state); | |
450 } | |
451 }, | |
452 | |
453 _computeInputContentClass: function(noLabelFloat, alwaysFloatLabel, focused,
invalid, _inputHasContent) { | |
454 var cls = 'input-content'; | |
455 if (!noLabelFloat) { | |
456 if (alwaysFloatLabel || _inputHasContent) { | |
457 cls += ' label-is-floating'; | |
458 if (invalid) { | |
459 cls += ' is-invalid'; | |
460 } else if (focused) { | |
461 cls += " label-is-highlighted"; | |
462 } | |
463 } | |
464 } else { | |
465 if (_inputHasContent) { | |
466 cls += ' label-is-hidden'; | |
467 } | |
468 } | |
469 return cls; | |
470 }, | |
471 | |
472 _computeUnderlineClass: function(focused, invalid) { | |
473 var cls = 'underline'; | |
474 if (invalid) { | |
475 cls += ' is-invalid'; | |
476 } else if (focused) { | |
477 cls += ' is-highlighted' | |
478 } | |
479 return cls; | |
480 }, | |
481 | |
482 _computeAddOnContentClass: function(focused, invalid) { | |
483 var cls = 'add-on-content'; | |
484 if (invalid) { | |
485 cls += ' is-invalid'; | |
486 } else if (focused) { | |
487 cls += ' is-highlighted' | |
488 } | |
489 return cls; | |
490 } | |
491 | |
492 }); | |
493 | |
494 })(); | |
495 </script> | |
OLD | NEW |