OLD | NEW |
1 <!-- | 1 <!-- |
2 @license | 2 @license |
3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | 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 | 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 | 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 | 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 | 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 | 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
9 --> | 9 --> |
10 | 10 |
11 <link rel="import" href="../polymer/polymer.html"> | 11 <link rel="import" href="../polymer/polymer.html"> |
| 12 <link rel="import" href="../iron-behaviors/iron-control-state.html"> |
12 <link rel="import" href="../iron-flex-layout/classes/iron-flex-layout.html"> | 13 <link rel="import" href="../iron-flex-layout/classes/iron-flex-layout.html"> |
13 <link rel="import" href="../iron-validatable-behavior/iron-validatable-behavior.
html"> | 14 <link rel="import" href="../iron-validatable-behavior/iron-validatable-behavior.
html"> |
| 15 <link rel="import" href="../iron-form-element-behavior/iron-form-element-behavio
r.html"> |
14 | 16 |
15 <!-- | 17 <!-- |
16 `iron-autogrow-textarea` is an element containing a textarea that grows in heigh
t as more | 18 `iron-autogrow-textarea` is an element containing a textarea that grows in heigh
t as more |
17 lines of input are entered. Unless an explicit height or the `maxRows` property
is set, it will | 19 lines of input are entered. Unless an explicit height or the `maxRows` property
is set, it will |
18 never scroll. | 20 never scroll. |
19 | 21 |
20 Example: | 22 Example: |
21 | 23 |
22 <iron-autogrow-textarea id="a1"> | 24 <iron-autogrow-textarea id="a1"> |
23 <textarea id="t1"></textarea> | 25 <textarea id="t1"></textarea> |
(...skipping 24 matching lines...) Expand all Loading... |
48 visibility: hidden; | 50 visibility: hidden; |
49 word-wrap: break-word; | 51 word-wrap: break-word; |
50 } | 52 } |
51 | 53 |
52 textarea { | 54 textarea { |
53 position: relative; | 55 position: relative; |
54 outline: none; | 56 outline: none; |
55 border: none; | 57 border: none; |
56 resize: none; | 58 resize: none; |
57 background: inherit; | 59 background: inherit; |
| 60 color: inherit; |
58 /* see comments in template */ | 61 /* see comments in template */ |
59 width: 100%; | 62 width: 100%; |
60 height: 100%; | 63 height: 100%; |
61 font-size: inherit; | 64 font-size: inherit; |
62 font-family: inherit; | 65 font-family: inherit; |
63 } | 66 } |
64 | 67 |
65 ::content textarea:invalid { | 68 ::content textarea:invalid { |
66 box-shadow: none; | 69 box-shadow: none; |
67 } | 70 } |
68 | 71 |
69 </style> | 72 </style> |
70 <template> | 73 <template> |
71 <!-- the mirror sizes the input/textarea so it grows with typing --> | 74 <!-- the mirror sizes the input/textarea so it grows with typing --> |
72 <div id="mirror" class="mirror-text" aria-hidden="true"> </div> | 75 <div id="mirror" class="mirror-text" aria-hidden="true"> </div> |
73 | 76 |
74 <!-- size the input/textarea with a div, because the textarea has intrinsic
size in ff --> | 77 <!-- size the input/textarea with a div, because the textarea has intrinsic
size in ff --> |
75 <div class="textarea-container fit"> | 78 <div class="textarea-container fit"> |
76 <textarea id="textarea" | 79 <textarea id="textarea" |
77 autocomplete$="[[autocomplete]]" | 80 autocomplete$="[[autocomplete]]" |
78 autofocus$="[[autofocus]]" | 81 autofocus$="[[autofocus]]" |
79 inputmode$="[[inputmode]]" | 82 inputmode$="[[inputmode]]" |
80 name$="[[name]]" | |
81 placeholder$="[[placeholder]]" | 83 placeholder$="[[placeholder]]" |
82 readonly$="[[readonly]]" | 84 readonly$="[[readonly]]" |
83 required$="[[required]]" | 85 required$="[[required]]" |
84 rows$="[[rows]]" | 86 rows$="[[rows]]" |
85 maxlength$="[[maxlength]]"></textarea> | 87 maxlength$="[[maxlength]]"></textarea> |
86 </div> | 88 </div> |
87 </template> | 89 </template> |
| 90 </dom-module> |
88 | 91 |
89 <script> | 92 <script> |
90 | 93 |
91 Polymer({ | 94 Polymer({ |
92 | 95 |
93 is: 'iron-autogrow-textarea', | 96 is: 'iron-autogrow-textarea', |
94 | 97 |
95 behaviors: [ | 98 behaviors: [ |
96 Polymer.IronValidatableBehavior | 99 Polymer.IronFormElementBehavior, |
| 100 Polymer.IronValidatableBehavior, |
| 101 Polymer.IronControlState |
97 ], | 102 ], |
98 | 103 |
99 properties: { | 104 properties: { |
100 | 105 |
101 /** | 106 /** |
102 * Use this property instead of `value` for two-way data binding. | 107 * Use this property instead of `value` for two-way data binding. |
103 */ | 108 */ |
104 bindValue: { | 109 bindValue: { |
105 observer: '_bindValueChanged', | 110 observer: '_bindValueChanged', |
106 type: String | 111 type: String |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 }, | 162 }, |
158 | 163 |
159 /** | 164 /** |
160 * Bound to the textarea's `name` attribute. | 165 * Bound to the textarea's `name` attribute. |
161 */ | 166 */ |
162 name: { | 167 name: { |
163 type: String | 168 type: String |
164 }, | 169 }, |
165 | 170 |
166 /** | 171 /** |
| 172 * The value for this input, same as `bindValue` |
| 173 */ |
| 174 value: { |
| 175 notify: true, |
| 176 type: String, |
| 177 computed: '_computeValue(bindValue)' |
| 178 }, |
| 179 |
| 180 /** |
167 * Bound to the textarea's `placeholder` attribute. | 181 * Bound to the textarea's `placeholder` attribute. |
168 */ | 182 */ |
169 placeholder: { | 183 placeholder: { |
170 type: String | 184 type: String |
171 }, | 185 }, |
172 | 186 |
173 /** | 187 /** |
174 * Bound to the textarea's `readonly` attribute. | 188 * Bound to the textarea's `readonly` attribute. |
175 */ | 189 */ |
176 readonly: { | 190 readonly: { |
(...skipping 15 matching lines...) Expand all Loading... |
192 } | 206 } |
193 | 207 |
194 }, | 208 }, |
195 | 209 |
196 listeners: { | 210 listeners: { |
197 'input': '_onInput' | 211 'input': '_onInput' |
198 }, | 212 }, |
199 | 213 |
200 /** | 214 /** |
201 * Returns the underlying textarea. | 215 * Returns the underlying textarea. |
| 216 * @type HTMLTextAreaElement |
202 */ | 217 */ |
203 get textarea() { | 218 get textarea() { |
204 return this.$.textarea; | 219 return this.$.textarea; |
205 }, | 220 }, |
206 | 221 |
| 222 /** |
| 223 * Returns true if `value` is valid. The validator provided in `validator` |
| 224 * will be used first, if it exists; otherwise, the `textarea`'s validity |
| 225 * is used. |
| 226 * @return {boolean} True if the value is valid. |
| 227 */ |
| 228 validate: function() { |
| 229 // Empty, non-required input is valid. |
| 230 if (!this.required && this.value == '') { |
| 231 this.invalid = false; |
| 232 return true; |
| 233 } |
| 234 |
| 235 var valid; |
| 236 if (this.hasValidator()) { |
| 237 valid = Polymer.IronValidatableBehavior.validate.call(this, this.value); |
| 238 } else { |
| 239 valid = this.$.textarea.validity.valid; |
| 240 this.invalid = !valid; |
| 241 } |
| 242 this.fire('iron-input-validate'); |
| 243 return valid; |
| 244 }, |
| 245 |
207 _update: function() { | 246 _update: function() { |
208 this.$.mirror.innerHTML = this._valueForMirror(); | 247 this.$.mirror.innerHTML = this._valueForMirror(); |
209 | 248 |
210 var textarea = this.textarea; | 249 var textarea = this.textarea; |
211 // If the value of the textarea was updated imperatively, then we | 250 // If the value of the textarea was updated imperatively, then we |
212 // need to manually update bindValue as well. | 251 // need to manually update bindValue as well. |
213 if (textarea && this.bindValue != textarea.value) { | 252 if (textarea && this.bindValue != textarea.value) { |
214 this.bindValue = textarea.value; | 253 this.bindValue = textarea.value; |
215 } | 254 } |
216 }, | 255 }, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 var input = this.textarea; | 290 var input = this.textarea; |
252 if (!input) { | 291 if (!input) { |
253 return; | 292 return; |
254 } | 293 } |
255 this.tokens = (input && input.value) ? input.value.replace(/&/gm, '&')
.replace(/"/gm, '"').replace(/'/gm, ''').replace(/</gm, '<').replace
(/>/gm, '>').split('\n') : ['']; | 294 this.tokens = (input && input.value) ? input.value.replace(/&/gm, '&')
.replace(/"/gm, '"').replace(/'/gm, ''').replace(/</gm, '<').replace
(/>/gm, '>').split('\n') : ['']; |
256 return this._constrain(this.tokens); | 295 return this._constrain(this.tokens); |
257 }, | 296 }, |
258 | 297 |
259 _updateCached: function() { | 298 _updateCached: function() { |
260 this.$.mirror.innerHTML = this._constrain(this.tokens); | 299 this.$.mirror.innerHTML = this._constrain(this.tokens); |
| 300 }, |
| 301 |
| 302 _computeValue: function() { |
| 303 return this.bindValue; |
261 } | 304 } |
262 }) | 305 }) |
263 </script> | 306 </script> |
OLD | NEW |