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

Commit

Permalink
update to use html5 constraints api (from core-input update)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yvonne Yip committed Jul 24, 2014
1 parent 90de2d4 commit 6c744e7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
4 changes: 2 additions & 2 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

<br>

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

<br>

Expand All @@ -51,7 +51,7 @@

<br>

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

<br>

Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</head>
<body unresolved>

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

</body>
</html>
61 changes: 50 additions & 11 deletions paper-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
</div>

<div id="errorContainer">
<div id="error" role="alert" aria-hidden="{{!invalid}}">{{error}}</div>
<div id="error" role="alert" aria-hidden="{{!invalid}}">{{error || validationMessage}}</div>
<div id="errorIcon"></div>
</div>

Expand Down Expand Up @@ -144,6 +144,16 @@
*/
maxRows: 0,

/**
* The message to display if the input value fails validation. If this
* is unset or the empty string, a default message is displayed depending
* on the type of validation error.
*
* @attribute error
* @type string
*/
error: '',

focused: false,
pressed: false,

Expand Down Expand Up @@ -177,6 +187,19 @@
}
},

toggleLabel: function(force) {
var v = force !== undefined ? force : this.inputValue;

if (!this.floatingLabel) {
this.$.label.classList.toggle('hidden', v);
}

if (this.floatingLabel && !this.focused) {
this.$.label.classList.toggle('hidden', v);
this.$.floatedLabel.classList.toggle('hidden', !v);
}
},

rowsChanged: function() {
if (this.multiline && !isNaN(parseInt(this.rows))) {
this.$.minInputHeight.innerHTML = '';
Expand Down Expand Up @@ -209,14 +232,7 @@
this.resizeInput();
}

if (!this.floatingLabel) {
this.$.label.classList.toggle('hidden', this.inputValue);
}

if (this.floatingLabel && !this.focused) {
this.$.label.classList.toggle('hidden', this.inputValue);
this.$.floatedLabel.classList.toggle('hidden', !this.inputValue);
}
this.toggleLabel();
},

labelChanged: function() {
Expand Down Expand Up @@ -249,26 +265,36 @@
this.focused = true;
},

shouldFloatLabel: function() {
// if type = number, the input value is the empty string until a valid number
// is entered so we must do some hacks here
return this.inputValue || (this.type === 'number' && !this.validity.valid);
},

inputBlurAction: function() {
this.super(arguments);

this.$.underlineHighlight.classList.remove('focused');
this.$.caret.classList.remove('focused');

if (this.floatingLabel) {
this.$.floatedLabel.classList.remove('focused');
this.$.floatedLabel.classList.remove('focusedColor');
if (!this.inputValue) {
if (!this.shouldFloatLabel()) {
this.$.floatedLabel.classList.add('hidden');
}
}
if (!this.inputValue) {

// type = number hack. see core-input for more info
if (!this.shouldFloatLabel()) {
this.$.label.classList.remove('hidden');
this.$.label.classList.add('animating');
this.async(function() {
this.$.label.style.webkitTransform = 'none';
this.$.label.style.transform = 'none';
});
}

this.focused = false;
},

Expand Down Expand Up @@ -351,6 +377,19 @@
}
},

keydownAction: function() {
this.super();

// more type = number hacks. see core-input for more info
if (this.type === 'number') {
this.async(function() {
if (!this.inputValue) {
this.toggleLabel(!this.validity.valid);
}
});
}
},

keypressAction: function() {
if (this.animating) {
this.transitionEndAction();
Expand Down

0 comments on commit 6c744e7

Please sign in to comment.