Skip to content

Commit

Permalink
paper-input: Exposed invalid status via onInvalid action.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanChadwick committed Apr 26, 2016
1 parent 0dd83d9 commit 5521f3b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion addon/components/paper-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default BaseFocusable.extend(ColorMixin, FlexMixin, {
tabindex: null,
hideAllMessages: false,
isTouched: false,
lastIsInvalid: undefined,

hasValue: computed('value', 'isNativeInvalid', function() {
let value = this.get('value');
Expand All @@ -38,8 +39,21 @@ export default BaseFocusable.extend(ColorMixin, FlexMixin, {
return `input-${this.get('elementId')}`;
}),

/**
* The result of isInvalid is appropriate for controlling the display of
* validation error messages. It also may be used to distinguish whether
* the input would be considered valid after it is touched.
*
* @public
*
* @return {boolean|null} Whether the input is or would be invalid.
* null: input has not yet been touched, but would be invalid if it were
* false: input is valid (touched or not)
* true: input has been touched and is invalid.
*/
isInvalid: computed('isTouched', 'validationErrorMessages.length', 'isNativeInvalid', function() {
return this.get('isTouched') && (this.get('validationErrorMessages.length') || this.get('isNativeInvalid'));
let isInvalid = this.get('validationErrorMessages.length') || this.get('isNativeInvalid');
return isInvalid && !this.get('isTouched') ? null : !!isInvalid;
}),

renderCharCount: computed('value', function() {
Expand Down Expand Up @@ -120,6 +134,7 @@ export default BaseFocusable.extend(ColorMixin, FlexMixin, {
didReceiveAttrs() {
this._super(...arguments);
assert('{{paper-input}} and {{paper-select}} require an `onChange` action.', !!this.get('onChange'));
this.notifyInvalid();
},

didInsertElement() {
Expand Down Expand Up @@ -179,17 +194,27 @@ export default BaseFocusable.extend(ColorMixin, FlexMixin, {
return offsetHeight + (line > 0 ? line : 0);
},

notifyInvalid() {
let isInvalid = this.get('isInvalid');
if (this.get('lastIsInvalid') !== isInvalid) {
this.sendAction('onInvalid', this.get('isInvalid'));
this.set('lastIsinvalid');
}
},

actions: {
handleInput(e) {
this.sendAction('onChange', e.target.value);
this.growTextarea();
let inputElement = this.$('input').get(0);
this.set('isNativeInvalid', inputElement && inputElement.validity && inputElement.validity.badInput);
this.notifyInvalid();
},

handleBlur(e) {
this.sendAction('onBlur', e);
this.set('isTouched', true);
this.notifyInvalid();
}
}
});

0 comments on commit 5521f3b

Please sign in to comment.