forked from PolymerElements/gold-cc-expiration-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgold-cc-expiration-input.html
206 lines (165 loc) · 5.63 KB
/
gold-cc-expiration-input.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-input/paper-input-behavior.html">
<link rel="import" href="../paper-input/paper-input-container.html">
<link rel="import" href="../paper-input/paper-input-error.html">
<link rel="import" href="../iron-input/iron-input.html">
<link rel="import" href="../iron-form-element-behavior/iron-form-element-behavior.html">
<link rel="import" href="date-input.html">
<!--
`gold-cc-expiration-input` is a single-line text field with Material Design styling
for entering a credit card's expiration date
<gold-cc-expiration-input></gold-cc-expiration-input>
<gold-cc-expiration-input value="11/15"></gold-cc-expiration-input>
It may include an optional label, which by default is "Expiration Date".
<gold-cc-expiration-input label="Date"></gold-cc-expiration-input>
### Validation
The input can check whether the entered date is a valid, future date.
The input can be automatically validated as the user is typing by using
the `auto-validate` and `required` attributes. For manual validation, the
element also has a `validate()` method, which returns the validity of the
input as well sets any appropriate error messages and styles.
See `Polymer.PaperInputBehavior` for more API docs.
### Styling
See `Polymer.PaperInputContainer` for a list of custom properties used to
style this element.
@group Gold Elements
@hero hero.svg
@demo demo/index.html
@class gold-cc-expiration-input
-->
<dom-module id="gold-cc-expiration-input">
<template>
<style>
:host {
display: block;
}
</style>
<paper-input-container id="container"
no-label-float="[[noLabelFloat]]"
always-float-label="[[alwaysFloatLabel]]"
attr-for-value="date"
disabled$="[[disabled]]"
invalid="[[invalid]]">
<label slot="label" hidden$="[[!label]]">[[label]]</label>
<date-input class="paper-input-input"
id="input"
slot="input"
aria-label-prefix="[[_ariaLabelledBy]]"
required$="[[required]]"
autocomplete$="[[autocomplete]]"
disabled$="[[disabled]]"
invalid="{{invalid}}"
autofocus$="[[autofocus]]"
inputmode$="[[inputmode]]"
placeholder$="[[placeholder]]"
readonly$="[[readonly]]">
</date-input>
<template is="dom-if" if="[[errorMessage]]">
<paper-input-error slot="add-on" id="error">
[[errorMessage]]
</paper-input-error>
</template>
</paper-input-container>
</template>
<script>
(function() {
Polymer({
is: 'gold-cc-expiration-input',
/* The underlying dateInput is tabbable */
hostAttributes: {
'tabindex': -1
},
behaviors: [
Polymer.PaperInputBehavior,
Polymer.IronFormElementBehavior
],
properties: {
/**
* The label for this input.
*/
label: {
type: String,
value: 'Expiration Date'
},
value: {
type: String,
value: '',
observer: '_onValueChanged'
}
},
listeners: {
'dateChanged': '_dateChanged'
},
observers: [
'_onFocusedChanged(focused)'
],
ready: function() {
// If there's an initial input, validate it.
if (this.value) {
this._handleAutoValidate();
}
},
created() {
// Polymer 2 propagates `autoValidate` earlier in the lifecycle than
// in Polymer 1
if (Polymer.Element) {
this.__ignoreAutoValidation = true;
}
},
/**
* A handler that is called on input
*/
_onValueChanged: function(value, oldValue) {
// The initial property assignment is handled by `ready`.
if (oldValue == undefined) {
return;
}
this.$.input.month = this._computeMonth(value);
this.$.input.year = this._computeYear(value);
this._handleAutoValidate();
},
_dateChanged: function(event) {
var month = event.detail.month || '';
var year = event.detail.year || '';
var value = year ? (month + '/' + year) : month;
this.value = String(value);
},
_computeMonth: function(value) {
// Date is in MM/YY format.
return value.split('/')[0];
},
_computeYear: function(value) {
// Date is in MM/YY format.
return value.split('/')[1] || '';
},
/**
* Overidden from Polymer.PaperInputBehavior.
*/
validate: function() {
return this.$.input.validate();
},
/**
* Overidden from Polymer.IronControlState.
*/
_onFocusedChanged: function(focused) {
if (this.__ignoreAutoValidation) {
this.__ignoreAutoValidation = false;
return;
}
if (!focused) {
this._handleAutoValidate();
}
}
});
})();
</script>
</dom-module>