Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit 6c744e7

Browse files
author
Yvonne Yip
committed
update to use html5 constraints api (from core-input update)
1 parent 90de2d4 commit 6c744e7

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

demo.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
<br>
3737

38-
<paper-input label="Type only numbers..." validate="^[0-9]*$" error="Input is not a number!"></paper-input>
38+
<paper-input label="Type only numbers..." type="number" error="Input is not a number!"></paper-input>
3939

4040
<br>
4141

@@ -51,7 +51,7 @@
5151

5252
<br>
5353

54-
<paper-input floatingLabel label="Type only numbers... (floating)" validate="^[0-9]*$" error="Input is not a number!"></paper-input>
54+
<paper-input floatingLabel label="Type only numbers... (floating)" type="number" error="Input is not a number!"></paper-input>
5555

5656
<br>
5757

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</head>
1717
<body unresolved>
1818

19-
<core-component-page></core-component-page>
19+
<core-component-page sources='["paper-input.html","../core-input/core-input.html"]'></core-component-page>
2020

2121
</body>
2222
</html>

paper-input.html

+50-11
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
</div>
9797

9898
<div id="errorContainer">
99-
<div id="error" role="alert" aria-hidden="{{!invalid}}">{{error}}</div>
99+
<div id="error" role="alert" aria-hidden="{{!invalid}}">{{error || validationMessage}}</div>
100100
<div id="errorIcon"></div>
101101
</div>
102102

@@ -144,6 +144,16 @@
144144
*/
145145
maxRows: 0,
146146

147+
/**
148+
* The message to display if the input value fails validation. If this
149+
* is unset or the empty string, a default message is displayed depending
150+
* on the type of validation error.
151+
*
152+
* @attribute error
153+
* @type string
154+
*/
155+
error: '',
156+
147157
focused: false,
148158
pressed: false,
149159

@@ -177,6 +187,19 @@
177187
}
178188
},
179189

190+
toggleLabel: function(force) {
191+
var v = force !== undefined ? force : this.inputValue;
192+
193+
if (!this.floatingLabel) {
194+
this.$.label.classList.toggle('hidden', v);
195+
}
196+
197+
if (this.floatingLabel && !this.focused) {
198+
this.$.label.classList.toggle('hidden', v);
199+
this.$.floatedLabel.classList.toggle('hidden', !v);
200+
}
201+
},
202+
180203
rowsChanged: function() {
181204
if (this.multiline && !isNaN(parseInt(this.rows))) {
182205
this.$.minInputHeight.innerHTML = '';
@@ -209,14 +232,7 @@
209232
this.resizeInput();
210233
}
211234

212-
if (!this.floatingLabel) {
213-
this.$.label.classList.toggle('hidden', this.inputValue);
214-
}
215-
216-
if (this.floatingLabel && !this.focused) {
217-
this.$.label.classList.toggle('hidden', this.inputValue);
218-
this.$.floatedLabel.classList.toggle('hidden', !this.inputValue);
219-
}
235+
this.toggleLabel();
220236
},
221237

222238
labelChanged: function() {
@@ -249,26 +265,36 @@
249265
this.focused = true;
250266
},
251267

268+
shouldFloatLabel: function() {
269+
// if type = number, the input value is the empty string until a valid number
270+
// is entered so we must do some hacks here
271+
return this.inputValue || (this.type === 'number' && !this.validity.valid);
272+
},
273+
252274
inputBlurAction: function() {
253275
this.super(arguments);
254276

255277
this.$.underlineHighlight.classList.remove('focused');
256278
this.$.caret.classList.remove('focused');
279+
257280
if (this.floatingLabel) {
258281
this.$.floatedLabel.classList.remove('focused');
259282
this.$.floatedLabel.classList.remove('focusedColor');
260-
if (!this.inputValue) {
283+
if (!this.shouldFloatLabel()) {
261284
this.$.floatedLabel.classList.add('hidden');
262285
}
263286
}
264-
if (!this.inputValue) {
287+
288+
// type = number hack. see core-input for more info
289+
if (!this.shouldFloatLabel()) {
265290
this.$.label.classList.remove('hidden');
266291
this.$.label.classList.add('animating');
267292
this.async(function() {
268293
this.$.label.style.webkitTransform = 'none';
269294
this.$.label.style.transform = 'none';
270295
});
271296
}
297+
272298
this.focused = false;
273299
},
274300

@@ -351,6 +377,19 @@
351377
}
352378
},
353379

380+
keydownAction: function() {
381+
this.super();
382+
383+
// more type = number hacks. see core-input for more info
384+
if (this.type === 'number') {
385+
this.async(function() {
386+
if (!this.inputValue) {
387+
this.toggleLabel(!this.validity.valid);
388+
}
389+
});
390+
}
391+
},
392+
354393
keypressAction: function() {
355394
if (this.animating) {
356395
this.transitionEndAction();

0 commit comments

Comments
 (0)