This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgold-cc-expiration-input.js
194 lines (160 loc) · 5.28 KB
/
gold-cc-expiration-input.js
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
/**
@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
*/
import '@polymer/polymer/polymer-legacy.js';
import '@polymer/paper-input/paper-input-container.js';
import '@polymer/paper-input/paper-input-error.js';
import '@polymer/iron-input/iron-input.js';
import './date-input.js';
import {IronFormElementBehavior} from '@polymer/iron-form-element-behavior/iron-form-element-behavior.js';
import {PaperInputBehavior} from '@polymer/paper-input/paper-input-behavior.js';
import {Polymer} from '@polymer/polymer/lib/legacy/polymer-fn.js';
import {html} from '@polymer/polymer/lib/utils/html-tag.js';
/**
`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
@demo demo/index.html
@class gold-cc-expiration-input
*/
Polymer({
_template: html`
<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]]"
on-date-changed="_onDateChanged">
</date-input>
<template is="dom-if" if="[[errorMessage]]">
<paper-input-error slot="add-on" id="error">
[[errorMessage]]
</paper-input-error>
</template>
</paper-input-container>
`,
is: 'gold-cc-expiration-input',
/* The underlying dateInput is tabbable */
hostAttributes: {'tabindex': -1},
behaviors: [PaperInputBehavior, IronFormElementBehavior],
properties: {
/**
* The label for this input.
*/
label: {
type: String,
value: 'Expiration Date',
},
value: {
type: String,
value: '',
observer: '_onValueChanged',
}
},
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
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 && value === '') {
return;
}
this.$.input.setProperties({
month: this._computeMonth(value),
year: this._computeYear(value),
});
this._handleAutoValidate();
},
_onDateChanged: function(event) {
// date-input's _computeDate gets called on created which is called before
// the first __onValueChanged is called
if (!this.__firstDateComputeSkipped) {
this.__firstDateComputeSkipped = true;
return;
}
var month = event.detail.value.month || '';
var year = event.detail.value.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();
}
}
});