forked from PolymerElements/gold-cc-expiration-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date-input.html
174 lines (149 loc) · 4.43 KB
/
date-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
<!--
@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="../iron-input/iron-input.html">
<link rel="import" href="../iron-flex-layout/classes/iron-flex-layout.html">
<link rel="import" href="../iron-validatable-behavior/iron-validatable-behavior.html">
<link rel="import" href="date-validator.html">
<dom-module id="date-input">
<style>
:host {
display: inline-block;
}
span {
@apply(--paper-input-container-font);
opacity: 0.33;
text-align: center;
}
input {
position: relative; /* to make a stacking context */
outline: none;
box-shadow: none;
padding: 0;
width: 100%;
background: transparent;
border: none;
color: var(--paper-input-container-input-color, --primary-text-color);
text-align: center;
@apply(--paper-font-subhead);
@apply(--paper-input-container-input);
}
</style>
<template>
<date-validator id="validator"></date-validator>
<span aria-hidden id="monthLabel" hidden>Month</span>
<span aria-hidden id="yearLabel" hidden>Year</span>
<div class="horizontal layout">
<input is="iron-input"
aria-labelledby$="[[_computeAriaLabel(ariaLabelPrefix,'monthLabel')]]"
required$="[[required]]"
maxlength="2"
bind-value="{{month}}"
placeholder="MM"
allowed-pattern="[0-9]"
prevent-invalid-input
autocomplete="cc-exp-month"
type="tel"
class="flex"
disabled$="[[disabled]]"
invalid="{{invalid}}"
autofocus$="[[autofocus]]"
inputmode$="[[inputmode]]"
placeholder$="[[placeholder]]"
readonly$="[[readonly]]">
<span>/</span>
<input is="iron-input"
id="expirationYear"
aria-labelledby$="[[_computeAriaLabel(ariaLabelPrefix,'yearLabel')]]"
required$="[[required]]"
maxlength="2"
bind-value="{{year}}"
placeholder="YY"
allowed-pattern="[0-9]"
prevent-invalid-input
autocomplete="cc-exp-year"
type="tel"
class="flex"
disabled$="[[disabled]]"
invalid="{{invalid}}"
autofocus$="[[autofocus]]"
inputmode$="[[inputmode]]"
placeholder$="[[placeholder]]"
readonly$="[[readonly]]">
</div>
</template>
</dom-module>
<script>
Polymer({
is: 'date-input',
behaviors: [
Polymer.IronValidatableBehavior
],
properties: {
/**
* Set to true to mark the input as required.
*/
required: {
type: Boolean,
value: false
},
/**
* The month component of the date displayed.
*/
month: {
type: String
},
/**
* The year component of the date displayed.
*/
year: {
type: String
},
/**
* The date object used by the validator. Has two properties, month and year.
*/
date: {
notify: true,
type: Object
},
validator: {
type: String,
value: 'date-validator'
},
ariaLabelPrefix: {
type:String
}
},
observers: [
'_computeDate(month,year)'
],
_computeDate: function(month, year) {
// Months are 0-11.
this.date = {month: month, year: year};
this.fire('dateChanged', this.date);
// Advance cursor to year after month entry
if (month.length === 2) {
this.$.expirationYear.focus();
}
},
validate: function() {
// Empty, non-required input is valid.
if (!this.required && this.month == '' && this.year == '') {
return true;
}
this.invalid = !this.$.validator.validate(this.date);
this.fire('iron-input-validate');
return !this.invalid;
},
_computeAriaLabel: function(dateLabel, monthLabel) {
return dateLabel + ' ' + monthLabel;
}
});
</script>