| 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="../iron-flex-layout/classes/iron-flex-layout.html"> | |
| 13 <link rel="import" href="../iron-validatable-behavior/iron-validatable-behavior.
html"> | |
| 14 | |
| 15 <!-- | |
| 16 `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 | |
| 18 never scroll. | |
| 19 | |
| 20 Example: | |
| 21 | |
| 22 <iron-autogrow-textarea id="a1"> | |
| 23 <textarea id="t1"></textarea> | |
| 24 </iron-autogrow-textarea> | |
| 25 | |
| 26 Because the `textarea`'s `value` property is not observable, you should use | |
| 27 this element's `bind-value` instead for imperative updates. | |
| 28 | |
| 29 @group Iron Elements | |
| 30 @hero hero.svg | |
| 31 @demo demo/index.html | |
| 32 --> | |
| 33 | |
| 34 <dom-module id="iron-autogrow-textarea"> | |
| 35 | |
| 36 <style> | |
| 37 :host { | |
| 38 display: inline-block; | |
| 39 position: relative; | |
| 40 width: 400px; | |
| 41 border: 1px solid; | |
| 42 padding: 2px; | |
| 43 -moz-appearance: textarea; | |
| 44 -webkit-appearance: textarea; | |
| 45 } | |
| 46 | |
| 47 .mirror-text { | |
| 48 visibility: hidden; | |
| 49 word-wrap: break-word; | |
| 50 } | |
| 51 | |
| 52 textarea { | |
| 53 position: relative; | |
| 54 outline: none; | |
| 55 border: none; | |
| 56 resize: none; | |
| 57 background: inherit; | |
| 58 /* see comments in template */ | |
| 59 width: 100%; | |
| 60 height: 100%; | |
| 61 font-size: inherit; | |
| 62 font-family: inherit; | |
| 63 } | |
| 64 | |
| 65 ::content textarea:invalid { | |
| 66 box-shadow: none; | |
| 67 } | |
| 68 | |
| 69 </style> | |
| 70 <template> | |
| 71 <!-- the mirror sizes the input/textarea so it grows with typing --> | |
| 72 <div id="mirror" class="mirror-text" aria-hidden="true"> </div> | |
| 73 | |
| 74 <!-- size the input/textarea with a div, because the textarea has intrinsic
size in ff --> | |
| 75 <div class="textarea-container fit"> | |
| 76 <textarea id="textarea" required$="[[required]]" rows$="[[rows]]" maxlengt
h$="[[maxlength]]"></textarea> | |
| 77 </div> | |
| 78 </template> | |
| 79 | |
| 80 <script> | |
| 81 | |
| 82 Polymer({ | |
| 83 | |
| 84 is: 'iron-autogrow-textarea', | |
| 85 | |
| 86 behaviors: [ | |
| 87 Polymer.IronValidatableBehavior | |
| 88 ], | |
| 89 | |
| 90 properties: { | |
| 91 | |
| 92 /** | |
| 93 * Use this property instead of `value` for two-way data binding. | |
| 94 */ | |
| 95 bindValue: { | |
| 96 observer: '_bindValueChanged', | |
| 97 type: String | |
| 98 }, | |
| 99 | |
| 100 /** | |
| 101 * The initial number of rows. | |
| 102 * | |
| 103 * @attribute rows | |
| 104 * @type number | |
| 105 * @default 1 | |
| 106 */ | |
| 107 rows: { | |
| 108 type: Number, | |
| 109 value: 1, | |
| 110 observer: '_updateCached' | |
| 111 }, | |
| 112 | |
| 113 /** | |
| 114 * The maximum number of rows this element can grow to until it | |
| 115 * scrolls. 0 means no maximum. | |
| 116 * | |
| 117 * @attribute maxRows | |
| 118 * @type number | |
| 119 * @default 0 | |
| 120 */ | |
| 121 maxRows: { | |
| 122 type: Number, | |
| 123 value: 0, | |
| 124 observer: '_updateCached' | |
| 125 }, | |
| 126 | |
| 127 /** | |
| 128 * Set to true to mark the textarea as required. | |
| 129 */ | |
| 130 required: { | |
| 131 type: Boolean | |
| 132 }, | |
| 133 | |
| 134 /** | |
| 135 * The maximum length of the input value. | |
| 136 */ | |
| 137 maxlength: { | |
| 138 type: Number | |
| 139 } | |
| 140 | |
| 141 }, | |
| 142 | |
| 143 listeners: { | |
| 144 'input': '_onInput' | |
| 145 }, | |
| 146 | |
| 147 /** | |
| 148 * Returns the underlying textarea. | |
| 149 */ | |
| 150 get textarea() { | |
| 151 return this.$.textarea; | |
| 152 }, | |
| 153 | |
| 154 _update: function() { | |
| 155 this.$.mirror.innerHTML = this._valueForMirror(); | |
| 156 | |
| 157 var textarea = this.textarea; | |
| 158 // If the value of the textarea was updated imperatively, then we | |
| 159 // need to manually update bindValue as well. | |
| 160 if (textarea && this.bindValue != textarea.value) { | |
| 161 this.bindValue = textarea.value; | |
| 162 } | |
| 163 }, | |
| 164 | |
| 165 _bindValueChanged: function() { | |
| 166 var textarea = this.textarea; | |
| 167 if (!textarea) { | |
| 168 return; | |
| 169 } | |
| 170 | |
| 171 textarea.value = this.bindValue; | |
| 172 this._update(); | |
| 173 // manually notify because we don't want to notify until after setting val
ue | |
| 174 this.fire('bind-value-changed', {value: this.bindValue}); | |
| 175 }, | |
| 176 | |
| 177 _onInput: function(event) { | |
| 178 this.bindValue = event.path ? event.path[0].value : event.target.value; | |
| 179 this._update(); | |
| 180 }, | |
| 181 | |
| 182 _constrain: function(tokens) { | |
| 183 var _tokens; | |
| 184 tokens = tokens || ['']; | |
| 185 // Enforce the min and max heights for a multiline input to avoid measurem
ent | |
| 186 if (this.maxRows > 0 && tokens.length > this.maxRows) { | |
| 187 _tokens = tokens.slice(0, this.maxRows); | |
| 188 } else { | |
| 189 _tokens = tokens.slice(0); | |
| 190 } | |
| 191 while (this.rows > 0 && _tokens.length < this.rows) { | |
| 192 _tokens.push(''); | |
| 193 } | |
| 194 return _tokens.join('<br>') + ' '; | |
| 195 }, | |
| 196 | |
| 197 _valueForMirror: function() { | |
| 198 var input = this.textarea; | |
| 199 if (!input) { | |
| 200 return; | |
| 201 } | |
| 202 this.tokens = (input && input.value) ? input.value.replace(/&/gm, '&')
.replace(/"/gm, '"').replace(/'/gm, ''').replace(/</gm, '<').replace
(/>/gm, '>').split('\n') : ['']; | |
| 203 return this._constrain(this.tokens); | |
| 204 }, | |
| 205 | |
| 206 _updateCached: function() { | |
| 207 this.$.mirror.innerHTML = this._constrain(this.tokens); | |
| 208 } | |
| 209 }) | |
| 210 </script> | |
| OLD | NEW |