| OLD | NEW |
| (Empty) |
| 1 <!-- | |
| 2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | |
| 3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE | |
| 4 The complete set of authors may be found at http://polymer.github.io/AUTHORS | |
| 5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS | |
| 6 Code distributed by Google as part of the polymer project is also | |
| 7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS | |
| 8 --> | |
| 9 | |
| 10 <!-- | |
| 11 `paper-input` is a single- or multi-line text field where user can enter input. | |
| 12 It can optionally have a label. | |
| 13 | |
| 14 Example: | |
| 15 | |
| 16 <paper-input label="Your Name"></paper-input> | |
| 17 <paper-input multiline label="Enter multiple lines here"></paper-input> | |
| 18 | |
| 19 Theming | |
| 20 -------- | |
| 21 | |
| 22 Set `CoreStyle.g.paperInput.focusedColor` and `CoreStyle.g.paperInput.invalidCol
or` to theme | |
| 23 the focused and invalid states. | |
| 24 | |
| 25 To add custom styling to only some elements, use these selectors: | |
| 26 | |
| 27 html /deep/ paper-input[focused] .floated-label { | |
| 28 /* floating label color when the input has focus */ | |
| 29 color: green; | |
| 30 } | |
| 31 | |
| 32 html /deep/ paper-input .focused-underline, | |
| 33 html /deep/ paper-input .cursor { | |
| 34 /* line and cursor color when the input has focus */ | |
| 35 background-color: green; | |
| 36 } | |
| 37 | |
| 38 html /deep/ paper-input.invalid[focused] .floated-label, | |
| 39 html /deep/ paper-input[focused] .error-text, | |
| 40 html /deep/ paper-input[focused] .error-icon { | |
| 41 /* error text, icon, and floating label color when input is invalid */ | |
| 42 color: salmon; | |
| 43 } | |
| 44 | |
| 45 html /deep/ paper-input.invalid .focused-underline, | |
| 46 html /deep/ paper-input.invalid .cursor { | |
| 47 /* line and cursor color when the input is invalid */ | |
| 48 background-color: salmon; | |
| 49 } | |
| 50 | |
| 51 @group Paper Elements | |
| 52 @element paper-input | |
| 53 @extends core-input | |
| 54 @homepage github.io | |
| 55 --> | |
| 56 <link href="../polymer/polymer.html" rel="import"> | |
| 57 <link href="../core-icon/core-icon.html" rel="import"> | |
| 58 <link href="../core-icons/core-icons.html" rel="import"> | |
| 59 <link href="../core-input/core-input.html" rel="import"> | |
| 60 <link href="../core-style/core-style.html" rel="import"> | |
| 61 | |
| 62 <core-style id="paper-input"> | |
| 63 | |
| 64 :host([focused]) .floated-label { | |
| 65 color: {{g.paperInput.focusedColor}}; | |
| 66 } | |
| 67 | |
| 68 .focused-underline, | |
| 69 .cursor { | |
| 70 background-color: {{g.paperInput.focusedColor}}; | |
| 71 } | |
| 72 | |
| 73 | |
| 74 :host(.invalid[focused]) .floated-label, | |
| 75 :host([focused]) .error-text, | |
| 76 :host([focused]) .error-icon { | |
| 77 color: {{g.paperInput.invalidColor}}; | |
| 78 } | |
| 79 | |
| 80 :host(.invalid) .focused-underline, | |
| 81 :host(.invalid) .cursor { | |
| 82 background-color: {{g.paperInput.invalidColor}}; | |
| 83 } | |
| 84 | |
| 85 </core-style> | |
| 86 | |
| 87 <polymer-element name="paper-input" extends="core-input" layout vertical attribu
tes="label floatingLabel maxRows error" on-transitionEnd="{{transitionEndAction}
}" on-webkitTransitionEnd="{{transitionEndAction}}"> | |
| 88 | |
| 89 <template> | |
| 90 | |
| 91 <!-- | |
| 92 Input tests: | |
| 93 | |
| 94 - set value to integer 0 | |
| 95 - various html5 input types | |
| 96 - sizing: | |
| 97 - single-line: size with CSS | |
| 98 - single-line: can fit to container | |
| 99 - multi-line: size with CSS | |
| 100 - multi-line: size with rows | |
| 101 - multi-line: can fit to container | |
| 102 - multi-line: grows with typing | |
| 103 - multi-line: max rows | |
| 104 - multi-line: max rows, scrolls after | |
| 105 --> | |
| 106 | |
| 107 <link href="paper-input.css" rel="stylesheet"> | |
| 108 <core-style ref="paper-input"></core-style> | |
| 109 | |
| 110 <div class="floated-label" aria-hidden="true" hidden?="{{!floatingLabel}}" i
nvisible?="{{!inputValue && !(type === 'number' && !validity.valid) || labelAnim
ated}}"> | |
| 111 <!-- needed for floating label animation measurement --> | |
| 112 <span id="floatedLabelText" class="label-text">{{label}}</span> | |
| 113 </div> | |
| 114 | |
| 115 <!-- <div class="input-body" flex auto relative on-down="{{downAction}}" on-
up="{{upAction}}"> --> | |
| 116 <div class="input-body" flex auto relative> | |
| 117 | |
| 118 <!-- the mirror sizes the input/textarea so it grows with typing --> | |
| 119 <div id="mirror" class="mirror-text" invisible aria-hidden="true"></div> | |
| 120 | |
| 121 <div class="label" fit aria-hidden="true"> | |
| 122 <!-- needed for floating label animation measurement --> | |
| 123 <span id="labelText" class="label-text" invisible?="{{inputValue || !inp
utValue && type === 'number' && !validity.valid}}" animated?= | |
| 124 "{{labelAnimated}}">{{label}}</span> | |
| 125 </div> | |
| 126 | |
| 127 <div class="cursor" invisible?="{{!cursorAnimated}}" animated?="{{cursorAn
imated}}"></div> | |
| 128 | |
| 129 <!-- size the input/textarea with a div, because the textarea has intrinsi
c size in ff --> | |
| 130 <div class="input-container" on-down="{{downAction}}" on-up="{{upAction}}"
> | |
| 131 <shadow></shadow> | |
| 132 </div> | |
| 133 | |
| 134 </div> | |
| 135 | |
| 136 <div id="underline" class="underline" relative> | |
| 137 <div class="unfocused-underline" fit invisible?="{{disabled}}"></div> | |
| 138 <div id="focusedUnderline" class="focused-underline" fit invisible?="{{!fo
cused}}" animated?="{{underlineAnimated}}"></div> | |
| 139 </div> | |
| 140 | |
| 141 <div layout horizontal center hidden?="{{!invalid}}"> | |
| 142 <div class="error-text" flex auto role="alert" aria-hidden="{{!invalid}}">
{{error || validationMessage}}</div> | |
| 143 <core-icon class="error-icon" icon="warning"></core-icon> | |
| 144 </div> | |
| 145 | |
| 146 </template> | |
| 147 | |
| 148 <script> | |
| 149 | |
| 150 (function() { | |
| 151 | |
| 152 var paperInput = CoreStyle.g.paperInput = CoreStyle.g.paperInput || {}; | |
| 153 paperInput.focusedColor = '#4059a9'; | |
| 154 paperInput.invalidColor = '#d34336'; | |
| 155 | |
| 156 Polymer('paper-input', { | |
| 157 | |
| 158 publish: { | |
| 159 /** | |
| 160 * The label for this input. It normally appears as grey text inside | |
| 161 * the text input and disappears once the user enters text. | |
| 162 * | |
| 163 * @attribute label | |
| 164 * @type string | |
| 165 * @default '' | |
| 166 */ | |
| 167 label: '', | |
| 168 | |
| 169 /** | |
| 170 * If true, the label will "float" above the text input once the | |
| 171 * user enters text instead of disappearing. | |
| 172 * | |
| 173 * @attribute floatingLabel | |
| 174 * @type boolean | |
| 175 * @default false | |
| 176 */ | |
| 177 floatingLabel: false, | |
| 178 | |
| 179 /** | |
| 180 * (multiline only) If set to a non-zero value, the height of this | |
| 181 * text input will grow with the value changes until it is maxRows | |
| 182 * rows tall. If the maximum size does not fit the value, the text | |
| 183 * input will scroll internally. | |
| 184 * | |
| 185 * @attribute maxRows | |
| 186 * @type number | |
| 187 * @default 0 | |
| 188 */ | |
| 189 maxRows: 0, | |
| 190 | |
| 191 /** | |
| 192 * The message to display if the input value fails validation. If this | |
| 193 * is unset or the empty string, a default message is displayed dependin
g | |
| 194 * on the type of validation error. | |
| 195 * | |
| 196 * @attribute error | |
| 197 * @type string | |
| 198 */ | |
| 199 error: '', | |
| 200 | |
| 201 focused: {value: false, reflect: true} | |
| 202 | |
| 203 }, | |
| 204 | |
| 205 get inputValueForMirror() { | |
| 206 var tokens = this.inputValue ? String(this.inputValue).replace(/&/gm, '&
amp;').replace(/"/gm, '"').replace(/'/gm, ''').replace(/</gm, '<').r
eplace(/>/gm, '>').split('\n') : ['']; | |
| 207 | |
| 208 // Enforce the min and max heights for a multiline input here to | |
| 209 // avoid measurement | |
| 210 if (this.multiline) { | |
| 211 if (this.maxRows && tokens.length > this.maxRows) { | |
| 212 tokens = tokens.slice(0, this.maxRows); | |
| 213 } | |
| 214 while (this.rows && tokens.length < this.rows) { | |
| 215 tokens.push(''); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 return tokens.join('<br>') + ' '; | |
| 220 }, | |
| 221 | |
| 222 get inputHasValue() { | |
| 223 // if type = number, the input value is the empty string until a valid n
umber | |
| 224 // is entered so we must do some hacks here | |
| 225 return this.inputValue || (this.type === 'number' && !this.validity.vali
d); | |
| 226 }, | |
| 227 | |
| 228 syncInputValueToMirror: function() { | |
| 229 this.$.mirror.innerHTML = this.inputValueForMirror; | |
| 230 }, | |
| 231 | |
| 232 ready: function() { | |
| 233 this.syncInputValueToMirror(); | |
| 234 }, | |
| 235 | |
| 236 prepareLabelTransform: function() { | |
| 237 var toRect = this.$.floatedLabelText.getBoundingClientRect(); | |
| 238 var fromRect = this.$.labelText.getBoundingClientRect(); | |
| 239 if (toRect.width !== 0) { | |
| 240 var sy = toRect.height / fromRect.height; | |
| 241 this.$.labelText.cachedTransform = | |
| 242 'scale3d(' + (toRect.width / fromRect.width) + ',' + sy + ',1) ' + | |
| 243 'translate3d(0,' + (toRect.top - fromRect.top) / sy + 'px,0)'; | |
| 244 } | |
| 245 }, | |
| 246 | |
| 247 animateFloatingLabel: function() { | |
| 248 if (!this.floatingLabel || this.labelAnimated) { | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 if (!this.$.labelText.cachedTransform) { | |
| 253 this.prepareLabelTransform(); | |
| 254 } | |
| 255 | |
| 256 // If there's still no cached transform, the input is invisible so don't | |
| 257 // do the animation. | |
| 258 if (!this.$.labelText.cachedTransform) { | |
| 259 return; | |
| 260 } | |
| 261 | |
| 262 this.labelAnimated = true; | |
| 263 // Handle interrupted animation | |
| 264 this.async(function() { | |
| 265 this.transitionEndAction(); | |
| 266 }, null, 250); | |
| 267 | |
| 268 if (this.inputHasValue) { | |
| 269 this.$.labelText.style.webkitTransform = this.$.labelText.cachedTransf
orm; | |
| 270 this.$.labelText.style.transform = this.$.labelText.cachedTransform; | |
| 271 } else { | |
| 272 // Handle if the label started out floating | |
| 273 if (!this.$.labelText.style.webkitTransform && !this.$.labelText.style
.transform) { | |
| 274 this.$.labelText.style.webkitTransform = this.$.labelText.cachedTran
sform; | |
| 275 this.$.labelText.style.transform = this.$.labelText.cachedTransform; | |
| 276 this.$.labelText.offsetTop; | |
| 277 } | |
| 278 this.$.labelText.style.webkitTransform = ''; | |
| 279 this.$.labelText.style.transform = ''; | |
| 280 } | |
| 281 }, | |
| 282 | |
| 283 inputValueChanged: function(old) { | |
| 284 this.super(); | |
| 285 | |
| 286 this.syncInputValueToMirror(); | |
| 287 if (old && !this.inputValue || !old && this.inputValue) { | |
| 288 this.animateFloatingLabel(); | |
| 289 } | |
| 290 }, | |
| 291 | |
| 292 placeholderChanged: function() { | |
| 293 this.label = this.placeholder; | |
| 294 }, | |
| 295 | |
| 296 inputFocusAction: function() { | |
| 297 this.super(arguments); | |
| 298 this.focused = true; | |
| 299 }, | |
| 300 | |
| 301 inputBlurAction: function(e) { | |
| 302 this.super(arguments); | |
| 303 this.focused = false; | |
| 304 }, | |
| 305 | |
| 306 downAction: function(e) { | |
| 307 if (this.disabled) { | |
| 308 return; | |
| 309 } | |
| 310 | |
| 311 if (this.focused) { | |
| 312 return; | |
| 313 } | |
| 314 | |
| 315 // The underline spills from the tap location | |
| 316 var rect = this.$.underline.getBoundingClientRect(); | |
| 317 var right = e.x - rect.left; | |
| 318 this.$.focusedUnderline.style.mozTransformOrigin = right + 'px'; | |
| 319 this.$.focusedUnderline.style.webkitTransformOrigin = right + 'px '; | |
| 320 this.$.focusedUnderline.style.transformOriginX = right + 'px'; | |
| 321 | |
| 322 // Animations only run when the user interacts with the input | |
| 323 this.underlineAnimated = true; | |
| 324 | |
| 325 // Cursor animation only runs if the input is empty | |
| 326 if (!this.inputHasValue) { | |
| 327 this.cursorAnimated = true; | |
| 328 } | |
| 329 // Handle interrupted animation | |
| 330 this.async(function() { | |
| 331 this.transitionEndAction(); | |
| 332 }, null, 250); | |
| 333 }, | |
| 334 | |
| 335 keydownAction: function() { | |
| 336 this.super(); | |
| 337 | |
| 338 // more type = number hacks. see core-input for more info | |
| 339 if (this.type === 'number') { | |
| 340 var valid = !this.inputValue && this.validity.valid; | |
| 341 this.async(function() { | |
| 342 if (valid !== (!this.inputValue && this.validity.valid)) { | |
| 343 this.animateFloatingLabel(); | |
| 344 } | |
| 345 }); | |
| 346 } | |
| 347 }, | |
| 348 | |
| 349 transitionEndAction: function() { | |
| 350 this.underlineAnimated = false; | |
| 351 this.cursorAnimated = false; | |
| 352 this.labelAnimated = false; | |
| 353 } | |
| 354 | |
| 355 }); | |
| 356 | |
| 357 }()); | |
| 358 | |
| 359 </script> | |
| 360 | |
| 361 </polymer-element> | |
| OLD | NEW |