forked from PolymerElements/gold-cc-expiration-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gold-cc-expiration-input.html
202 lines (166 loc) · 5.84 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
<!--
@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
attr-for-value="date"
disabled$="[[disabled]]"
invalid="[[invalid]]">
<label slot="label" hidden$="[[!label]]">[[label]]</label>
<date-input class="paper-input-input" id="input"
stop-date-event-propagation$="[[!_observeDateChange]]"
date="{{date}}"
slot="input"
aria-label-prefix="[[_ariaLabelledBy]]"
required$="[[required]]"
auto-validate="[[autoValidate]]"
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">[[errorMessage]]</paper-input-error>
</template>
</paper-input-container>
</template>
<script>
(function() {
Polymer({
is: 'gold-cc-expiration-input',
behaviors: [
Polymer.PaperInputBehavior,
Polymer.IronFormElementBehavior
],
properties: {
/**
* The label for this input.
*/
label: {
type: String,
value: "Expiration Date"
},
/**
* The value as 'mm/yy'
*/
value: {
type: String,
notify: true,
value: ''
},
/**
* The value as an object with a 'month' and 'year' property
*/
date: {
type: Object,
notify: true,
observer: '_onDateChanged'
},
/**
* Composed date-input will not publish changes to its `date` property
* until `_observeDateChange` is true. This prevents the `date` property
* observer from incorrectly stepping-on the initial `value` property
*/
_observeDateChange: {
type: Boolean,
value: false
}
},
observers: [
'_onFocusedChanged(focused)',
'_onValueChanged(value, _observeDateChange)'
],
ready: function() {
this._observeDateChange = true;
/**
* PaperInputBehavior's tabindex=0 seems to cause problems
* the natural tabindex from date-input's native inputs should prevail
*/
this.removeAttribute('tabindex');
},
/**
* A handler that is called on input
*/
_onValueChanged: function(value, ready) {
var parts = (value || '').split('/');
var input = this.$.input;
input.month = parts[0];
input.year = parts[1];
},
/**
* Handles Date change events published by `date-input` by updating the
* `value` property
*/
_onDateChanged: function(d) {
d = d || {};
var month = d.month || '';
var year = d.year || '';
var value = year ? (month + '/' + year) : month;
this.value = String(value);
},
/**
* Overidden from Polymer.PaperInputBehavior.
*/
validate: function() {
return this.$.input.validate();
},
/**
* Overidden from Polymer.IronControlState.
*/
_onFocusedChanged: function(focused) {
var previouslyFocused = this._previouslyFocused || false;
this._previouslyFocused = focused || previouslyFocused;
if (!focused && previouslyFocused) {
this.validate();
}
}
})
})();
</script>
</dom-module>