-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-input-username.html
242 lines (221 loc) · 7.47 KB
/
paper-input-username.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../paper-spinner/paper-spinner.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<!--
Material design: [Text fields](https://www.google.com/design/spec/components/text-fields.html)
`<paper-input-username>` is a single-line text field with Material Design styling alowing you to make server-side check to verify if the username is valid.
<paper-input-username label="Username"></paper-input-username>
### Usage
When the user has finished writing his username, the username-changed event is called after the time specified with `time-before-username-check` (def: 2000ms).
After doing some server side check, you have to call `usernameValid()` or `usernameInvalid(message)`.
@demo demo/index.html
-->
<dom-module id="paper-input-username">
<template>
<style>
:host {
display: block;
}
paper-spinner{
--paper-spinner-stroke-width: 2px;
}
iron-icon[active] {
animation: fadein 0.3s linear forwards;
}
iron-icon:not([active]) {
animation: fadeout 0.3s linear forwards;
}
@keyframes fadein {
0% { opacity: 0; }
100% { opacity: 1; }
}
@keyframes fadeout {
0% { opacity: 1;}
100% { opacity: 0;}
}
</style>
<paper-input
id="inputPaper"
on-value-changed="_usernameChanged"
invalid="{{invalid}}"
disabled$="[[disabled]]"
autofocus="[[autofocus]]"
label="[[label]]"
value="{{value}}"
required$="[[required]]"
error-message="[[errorMessage]]"
allowed-pattern="[[allowedPattern]]"
minlength$="[[minlength]]"
maxlength$="[[maxlength]]">
<div suffix style="line-height:0px;margin-right:6px;">
<paper-spinner hidden id="spinner" style="vertical-align:middle;width:19px;height:19px;margin-bottom:3px;"></paper-spinner>
<iron-icon hidden id="okIcon" style="height:23px;width:23px;color:#4CAF50;" icon="done"></iron-icon>
<iron-icon hidden id="failIcon" style="height:23px;width:23px;color:#F44336;" icon="clear"></iron-icon>
</div>
</paper-input>
</template>
</dom-module>
<script>
Polymer({
is: 'paper-input-username',
/**
* Fired when the user has finished writing his username.
* The event return the username in the details.
*
* There is a delay to make the UX more smooth.
* Default: 2000ms
*
* The delay can be modified with time-before-username-check attribute
*
* @event username-changed
*/
properties: {
/**
* The value of the input.
*/
value: {
type: String,
notify: true
},
/**
* Set to true to disable this input.
*/
disabled: {
type: Boolean,
value: false
},
/**
* Set to true to mark the input as required.
*/
required: {
type: Boolean,
value: false
},
/**
* Returns true if the value is invalid
*/
invalid: {
type: Boolean,
value: false,
notify: true
},
/**
* Returns true if the value is invalid
*/
minErrorMessage: {
type: String
},
/**
* Set this to specify the pattern allowed by `preventInvalidInput`.
*/
allowedPattern: {
type: String
},
/**
* The minimum length of the input value.
*/
minlength: {
type: Number
},
/**
* The maximum length of the input value.
*/
maxlength: {
type: Number
},
/**
* The label for this input.
*/
label: String,
/**
* Autofocus propertie
*/
autofocus: Boolean,
/**
* The error message to display when the input is invalid.
*/
errorMessage: String,
/**
* Time in milliseconds before the username-changed event is fired. Make UX more fluid.
*/
timeBeforeUsernameCheck: {
type: Number,
value: 2000,
},
isTyping: {
type: Boolean,
value: false,
notify:true
},
},
_usernameChanged: function() {
if(this.$.inputPaper.value != ''){
this._typing();
if(this.minlength && this.$.inputPaper.value.length < this.minlength) {
this.typingTimer = this.async(function() {
this.usernameInvalid(this.minErrorMessage);
this.isTyping = false;
}, this.timeBeforeUsernameCheck);
} else {
this.typingTimer = this.async(function() {
this.fire('username-changed', {username: this.$.inputPaper.value});
this.isTyping = false;
}, this.timeBeforeUsernameCheck);
}
} else {
this.cancelAsync(this.typingTimer);
this.$.inputPaper.invalid = false;
this.$.failIcon.removeAttribute('active');
this.$.okIcon.removeAttribute('active');
this.$.spinner.active = false;
this.$.spinner.hidden = true;
this.$.failIcon.hidden = true;
this.$.okIcon.hidden = true;
}
},
_typing: function() {
this.isTyping = true;
this.cancelAsync(this.typingTimer);
this.$.inputPaper.invalid = false;
this.$.failIcon.removeAttribute('active');
this.$.okIcon.removeAttribute('active');
this.$.spinner.active = true;
this.$.spinner.hidden = false;
this.$.failIcon.hidden = true;
this.$.okIcon.hidden = true;
},
/**
* Call this function after server-side check to show an error.
*/
usernameInvalid: function(message) {
this.$.spinner.active = false;
this.$.inputPaper.invalid = true;
this.async(function() {
this.$.spinner.hidden = true;
this.$.failIcon.setAttribute('active', true);
this.$.failIcon.hidden = false;
this.$.inputPaper.errorMessage = message;
}, 300);
},
/**
* Call this function after server-side check to show the username is valid.
*/
usernameValid: function() {
this.$.spinner.active = false;
this.async(function() {
this.$.spinner.hidden = true;
this.$.okIcon.setAttribute('active', true);
this.$.okIcon.hidden = false;
}, 300);
},
validate: function() {
if(!this.$.inputPaper.invalid && !this.isTyping){
return this.$.inputPaper.validate();
} else {
return false;
}
}
});
</script>