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 <link rel="import" href="../polymer/polymer.html"> |
| 11 <link rel="import" href="../paper-input/paper-input-behavior.html"> |
| 12 <link rel="import" href="../paper-input/paper-input-container.html"> |
| 13 <link rel="import" href="../paper-input/paper-input-error.html"> |
| 14 <link rel="import" href="../iron-input/iron-input.html"> |
| 15 <link rel="import" href="../iron-form-element-behavior/iron-form-element-behavio
r.html"> |
| 16 <link rel="import" href="../iron-flex-layout/classes/iron-flex-layout.html"> |
| 17 |
| 18 <!-- |
| 19 `<gold-phone-input>` is a single-line text field with Material Design styling |
| 20 for entering a phone number. |
| 21 |
| 22 <gold-phone-input></gold-phone-input> |
| 23 |
| 24 It may include an optional label, which by default is "Phone number". |
| 25 |
| 26 <gold-phone-input label="call this"></gold-phone-input> |
| 27 |
| 28 ### Validation |
| 29 |
| 30 By default, the phone number is considered to be a US phone number, and |
| 31 will be validated according to the pattern `XXX-XXX-XXXX`, where `X` is a |
| 32 digit, and `-` is the separating dash. If you want to customize the input |
| 33 for a different area code or number pattern, use the `country-code` and |
| 34 `phone-number-pattern` attributes |
| 35 |
| 36 <gold-phone-input |
| 37 country-code="33" |
| 38 phone-number-pattern="X-XX-XX-XX-XX"> |
| 39 </gold-phone-input> |
| 40 |
| 41 The input can be automatically validated as the user is typing by using |
| 42 the `auto-validate` and `required` attributes. For manual validation, the |
| 43 element also has a `validate()` method, which returns the validity of the |
| 44 input as well sets any appropriate error messages and styles. |
| 45 |
| 46 See `Polymer.PaperInputBehavior` for more API docs. |
| 47 |
| 48 ### Styling |
| 49 |
| 50 See `Polymer.PaperInputContainer` for a list of custom properties used to |
| 51 style this element. |
| 52 |
| 53 @group Gold Elements |
| 54 @hero hero.svg |
| 55 @demo demo/index.html |
| 56 @class gold-phone-input |
| 57 --> |
| 58 |
| 59 <dom-module id="gold-phone-input"> |
| 60 <style> |
| 61 :host { |
| 62 display: block; |
| 63 } |
| 64 |
| 65 /* TODO: This should be a dropdown */ |
| 66 span { |
| 67 @apply(--paper-font-subhead); |
| 68 @apply(--paper-input-container-input); |
| 69 width: 40px; |
| 70 } |
| 71 |
| 72 </style> |
| 73 |
| 74 <template> |
| 75 <paper-input-container id="container" auto-validate="[[autoValidate]]" |
| 76 disabled$="[[disabled]]" |
| 77 no-label-float="[[noLabelFloat]]" |
| 78 always-float-label="[[_computeAlwaysFloatLabel(alwaysFloatLabel,placehol
der)]]" |
| 79 invalid="[[invalid]]"> |
| 80 |
| 81 <label style="left:40px;" hidden$="[[!label]]">[[label]]</label> |
| 82 |
| 83 <div class="horizontal layout"> |
| 84 <span>+<span>[[countryCode]]</span></span> |
| 85 |
| 86 <input is="iron-input" id="input" class="flex" |
| 87 aria-labelledby$="[[_ariaLabelledBy]]" |
| 88 aria-describedby$="[[_ariaDescribedBy]]" |
| 89 required$="[[required]]" |
| 90 bind-value="{{value}}" |
| 91 name$="[[name]]" |
| 92 allowed-pattern="[0-9\-]" |
| 93 autocomplete="tel" |
| 94 type="tel" |
| 95 prevent-invalid-input |
| 96 disabled$="[[disabled]]" |
| 97 invalid="{{invalid}}" |
| 98 autofocus$="[[autofocus]]" |
| 99 inputmode$="[[inputmode]]" |
| 100 placeholder$="[[placeholder]]" |
| 101 readonly$="[[readonly]]" |
| 102 size$="[[size]]"> |
| 103 </div> |
| 104 |
| 105 <template is="dom-if" if="[[errorMessage]]"> |
| 106 <paper-input-error id="error">[[errorMessage]]</paper-input-error> |
| 107 </template> |
| 108 |
| 109 </paper-input-container> |
| 110 </template> |
| 111 |
| 112 </dom-module> |
| 113 |
| 114 <script> |
| 115 (function() { |
| 116 Polymer({ |
| 117 |
| 118 is: 'gold-phone-input', |
| 119 |
| 120 behaviors: [ |
| 121 Polymer.PaperInputBehavior, |
| 122 Polymer.IronFormElementBehavior |
| 123 ], |
| 124 |
| 125 properties: { |
| 126 /** |
| 127 * The label for this input. |
| 128 */ |
| 129 label: { |
| 130 type: String, |
| 131 value: 'Phone number' |
| 132 }, |
| 133 |
| 134 /* |
| 135 * The country code that should be recognized and parsed. |
| 136 */ |
| 137 countryCode: { |
| 138 type: String, |
| 139 value: '1' |
| 140 }, |
| 141 |
| 142 /* |
| 143 * The format of a valid phone number, including formatting but excluding |
| 144 * the country code. Use 'X' to denote the digits separated by dashes. |
| 145 */ |
| 146 phoneNumberPattern: { |
| 147 type: String, |
| 148 value: 'XXX-XXX-XXXX', |
| 149 observer: '_phoneNumberPatternChanged' |
| 150 } |
| 151 }, |
| 152 |
| 153 observers: [ |
| 154 '_computeValue(value)' |
| 155 ], |
| 156 |
| 157 _phoneNumberPatternChanged: function() { |
| 158 // Transform the pattern into a regex the iron-input understands. |
| 159 var regex = ''; |
| 160 regex = this.phoneNumberPattern.replace(/\s/g, '\\s'); |
| 161 regex = regex.replace(/X/gi, '\\d'); |
| 162 regex = regex.replace(/\+/g, '\\+'); |
| 163 this.$.input.pattern = regex; |
| 164 |
| 165 if (this.autoValidate) { |
| 166 this.$.container.invalid = !this.$.input.validate(); |
| 167 } |
| 168 }, |
| 169 |
| 170 _computeValue: function(value) { |
| 171 var start = this.$.input.selectionStart; |
| 172 var previousCharADash = this.value.charAt(start - 1) == '-'; |
| 173 |
| 174 // Remove any already-applied formatting. |
| 175 value = value.replace(/-/g, ''); |
| 176 var shouldFormat = value.length <= this.phoneNumberPattern.replace(/-/g, '
').length; |
| 177 var formattedValue = ''; |
| 178 |
| 179 // Fill in the dashes according to the specified pattern. |
| 180 var currentDashIndex = 0; |
| 181 var totalDashesAdded = 0; |
| 182 for (var i = 0; i < value.length; i++) { |
| 183 currentDashIndex = this.phoneNumberPattern.indexOf('-', currentDashIndex
); |
| 184 |
| 185 // Since we remove any formatting first, we need to account added dashes |
| 186 // when counting the position of new dashes in the pattern. |
| 187 if (shouldFormat && i == (currentDashIndex - totalDashesAdded)) { |
| 188 formattedValue += '-'; |
| 189 currentDashIndex++; |
| 190 totalDashesAdded++; |
| 191 } |
| 192 |
| 193 formattedValue += value[i]; |
| 194 } |
| 195 this.updateValueAndPreserveCaret(formattedValue.trim()); |
| 196 |
| 197 // If the character right before the selection is a newly inserted |
| 198 // dash, we need to advance the selection to maintain the caret position. |
| 199 if (!previousCharADash && this.value.charAt(start - 1) == '-') { |
| 200 this.$.input.selectionStart = start + 1; |
| 201 this.$.input.selectionEnd = start + 1; |
| 202 } |
| 203 } |
| 204 |
| 205 }) |
| 206 |
| 207 })(); |
| 208 |
| 209 </script> |
OLD | NEW |