Skip to content

Commit

Permalink
fix(text-field): Do not trigger shake animation when text field is em…
Browse files Browse the repository at this point in the history
…pty (#5097)
  • Loading branch information
abhiomkar authored Sep 17, 2019
1 parent 9058846 commit 4913db9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/mdc-textfield/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export class MDCTextFieldFoundation extends MDCFoundation<MDCTextFieldAdapter> {
}

get shouldFloat(): boolean {
return this.shouldAlwaysFloat_ || this.isFocused_ || Boolean(this.getValue()) || this.isBadInput_();
return this.shouldAlwaysFloat_ || this.isFocused_ || !!this.getValue() || this.isBadInput_();
}

get shouldShake(): boolean {
return !this.isFocused_ && !this.isValid() && Boolean(this.getValue());
return !this.isFocused_ && !this.isValid() && !!this.getValue();
}

/**
Expand Down Expand Up @@ -315,7 +315,7 @@ export class MDCTextFieldFoundation extends MDCFoundation<MDCTextFieldAdapter> {
this.isValid_ = isValid;
this.styleValidity_(isValid);

const shouldShake = !isValid && !this.isFocused_;
const shouldShake = !isValid && !this.isFocused_ && !!this.getValue();
if (this.adapter_.hasLabel()) {
this.adapter_.shakeLabel(shouldShake);
}
Expand Down
5 changes: 5 additions & 0 deletions test/screenshot/spec/mdc-textfield/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ window.mdc.testFixture.fontsLoaded.then(() => {
textField.useNativeValidation = false;
textField.valid = false;
}

el.querySelector('.mdc-text-field__input').addEventListener('blur', () => {
// Focusing out from empty text field input shouldn't trigger shake animation.
textField.valid = false;
});
});

// Fixes the wide notched outline issue.
Expand Down
13 changes: 13 additions & 0 deletions test/unit/mdc-textfield/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ test('#setValid overrides native validation when useNativeValidation set to fals
test('#setValid updates classes', () => {
const {foundation, mockAdapter, helperText} = setupTest({useHelperText: true});
td.when(mockAdapter.hasLabel()).thenReturn(true);
td.when(mockAdapter.getNativeInput()).thenReturn({
value: 'test',
});

foundation.setValid(false);
td.verify(mockAdapter.addClass(cssClasses.INVALID));
Expand Down Expand Up @@ -367,6 +370,16 @@ test('#setValid removes mdc-textfied--invalid when set to true', () => {
td.verify(mockAdapter.removeClass(cssClasses.INVALID));
});

test('#setValid should not trigger shake animation when text field is empty', () => {
const {foundation, mockAdapter} = setupTest();
td.when(mockAdapter.hasLabel()).thenReturn(true);
td.when(mockAdapter.getNativeInput()).thenReturn({
value: '',
});
foundation.setValid(false);
td.verify(mockAdapter.shakeLabel(false));
});

test('#init focuses on input if adapter.isFocused is true', () => {
const {foundation, mockAdapter} = setupTest();
td.when(mockAdapter.isFocused()).thenReturn(true);
Expand Down

0 comments on commit 4913db9

Please sign in to comment.