forked from biztek/pmp-simple-date-input
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpmp-simple-date-input.js
228 lines (198 loc) · 6.86 KB
/
pmp-simple-date-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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
`pmp-simple-date-input`
@demo demo/index.html
*/
/*
FIXME(polymer-modulizer): the above comments were extracted
from HTML and may be out of place here. Review them and
then delete this comment!
*/
import '@polymer/polymer/polymer-legacy.js';
import { PaperInputBehavior } from '@polymer/paper-input/paper-input-behavior.js';
import '@polymer/paper-input/paper-input-container.js';
import '@polymer/iron-input/iron-input.js';
import '@polymer/paper-input/paper-input-error.js';
import { IronFormElementBehavior } from '@polymer/iron-form-element-behavior/iron-form-element-behavior.js';
import moment from 'moment';
import { Polymer } from '@polymer/polymer/lib/legacy/polymer-fn.js';
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
Polymer({
_template: html`
<style>
:host {
display: block;
}
input {
position: relative; /* to make a stacking context */
outline: none;
box-shadow: none;
margin: 0;
padding: 0;
width: 100%;
max-width: 100%;
background: transparent;
border: none;
color: var(--paper-input-container-input-color, var(--primary-text-color));
-webkit-appearance: none;
text-align: inherit;
vertical-align: bottom;
/* Firefox sets a min-width on the input, which can cause layout issues */
min-width: 0;
@apply --paper-font-subhead;
@apply --paper-input-container-input;
}
input:disabled {
@apply --paper-input-container-input-disabled;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
@apply --paper-input-container-input-webkit-spinner;
}
input::-webkit-clear-button {
@apply --paper-input-container-input-webkit-clear;
}
input::-webkit-calendar-picker-indicator {
@apply --paper-input-container-input-webkit-calendar-picker-indicator;
}
input::-webkit-input-placeholder {
color: var(--paper-input-container-color, var(--secondary-text-color));
}
input:-moz-placeholder {
color: var(--paper-input-container-color, var(--secondary-text-color));
}
input::-moz-placeholder {
color: var(--paper-input-container-color, var(--secondary-text-color));
}
input::-ms-clear {
@apply --paper-input-container-ms-clear;
}
input::-ms-reveal {
@apply --paper-input-container-ms-reveal;
}
input:-ms-input-placeholder {
color: var(--paper-input-container-color, var(--secondary-text-color));
}
</style>
<paper-input-container id="container" disabled\$="[[disabled]]" always-float-label="[[_computeAlwaysFloatLabel(alwaysFloatLabel,placeholder)]]" invalid="[[invalid]]">
<label hidden\$="[[!label]]" slot="label">[[label]]</label>
<iron-input bind-value="{{value}}" slot="input" id="input" maxlength\$="[[maxlength]]" allowed-pattern="[0-9\\/]" invalid="{{invalid}}">
<input aria-labelledby\$="[[_ariaLabelledBy]]" aria-describedby\$="[[_ariaDescribedBy]]" disabled\$="[[disabled]]" bind-value="{{value}}" required\$="[[required]]" maxlength\$="[[maxlength]]" name\$="[[name]]" prevent-invalid-input="" autofocus\$="[[autofocus]]" inputmode\$="[[inputmode]]" placeholder\$="[[placeholder]]">
</iron-input>
<template is="dom-if" if="[[errorMessage]]">
<paper-input-error id="error" slot="error">[[errorMessage]]</paper-input-error>
</template>
</paper-input-container>
`,
// jshint ignore:line
is: 'pmp-simple-date-input',
behaviors: [
PaperInputBehavior,
IronFormElementBehavior
],
properties: {
label: {
type: String,
value: 'Date of Birth'
},
value: {
type: String,
notify: true,
observer: '_onValueChanged'
},
invalid: {
type: Boolean,
value: false,
notify: true
},
autoValidate: {
type: Boolean,
value: false,
notify: true
},
required: {
type: Boolean,
value: false,
notify: true
},
disabled: {
type: Boolean,
value: false
},
datePattern: {
type: String,
value: 'MM/DD/YYYY',
observer: '_patternChanged'
},
placeholder: {
type: String,
value: 'MM/DD/YYYY'
},
maxlength: {
type: Number,
value: 10
}
},
observers: [
'_onFocusedChanged(focused)'
],
ready: function() {
if (this.value)
this._handleAutoValidate();
},
_onValueChanged: function(value, oldValue) {
if (typeof oldValue === 'undefined' || value === oldValue)
return;
value = value ? value.toString() : '';
let start = this.$.input.inputElement.selectionStart;
let initialSlashesBeforeCaret = value.substr(0, start).split('/').length - 1;
value = value.replace(/\//g, '');
let shouldFormat = value.length <= this.datePattern.replace(/\//g, '').length;
let formattedValue = '';
let currentSlashIndex = 0;
let totalSlashesAdded = 0;
for (let i = 0; i < value.length; i++) {
currentSlashIndex = this.datePattern.indexOf('/', currentSlashIndex);
if (shouldFormat && i == (currentSlashIndex - totalSlashesAdded)) { // jshint ignore:line
formattedValue += '/';
currentSlashIndex++;
totalSlashesAdded++;
}
formattedValue += value[i];
}
let updatedSlashesBeforeCaret = formattedValue.substr(0, start).split('/').length - 1;
let slashesDifference = updatedSlashesBeforeCaret - initialSlashesBeforeCaret;
this.updateValueAndPreserveCaret(formattedValue.trim());
this.$.input.inputElement.selectionStart = this.$.input.inputElement.selectionEnd = start + slashesDifference;
this._handleAutoValidate();
},
_onFocusedChanged: function(focused) {
if (!focused) {
this._handleAutoValidate();
}
},
validate: function() {
var valid = this.$.input.validate();
if (this.value && this.value.length === 10) {
this.invalid = !(moment(this.value, this.datePattern, true).isValid()); // jshint ignore:line
this.$.container.invalid = this.invalid;
this.set('invalid', this.invalid);
} else {
this.$.container.invalid = !valid;
}
this.$.container.updateAddons({
inputElement: this.$.input,
value: this.value,
invalid: !valid
});
return valid;
},
_patternChanged: function() {
var regex = '';
regex = this.datePattern.replace(/\s/g, '\\s');
regex = regex.replace(/M/gi, '\d');
regex = regex.replace(/D/gi, '\\d');
regex = regex.replace(/Y/gi, '\\d');
regex = regex.replace(/\+/g, '\\+');
this.$.input.pattern = regex;
}
});