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

Commit 4913db9

Browse files
authored
fix(text-field): Do not trigger shake animation when text field is empty (#5097)
1 parent 9058846 commit 4913db9

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

packages/mdc-textfield/foundation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ export class MDCTextFieldFoundation extends MDCFoundation<MDCTextFieldAdapter> {
5555
}
5656

5757
get shouldFloat(): boolean {
58-
return this.shouldAlwaysFloat_ || this.isFocused_ || Boolean(this.getValue()) || this.isBadInput_();
58+
return this.shouldAlwaysFloat_ || this.isFocused_ || !!this.getValue() || this.isBadInput_();
5959
}
6060

6161
get shouldShake(): boolean {
62-
return !this.isFocused_ && !this.isValid() && Boolean(this.getValue());
62+
return !this.isFocused_ && !this.isValid() && !!this.getValue();
6363
}
6464

6565
/**
@@ -315,7 +315,7 @@ export class MDCTextFieldFoundation extends MDCFoundation<MDCTextFieldAdapter> {
315315
this.isValid_ = isValid;
316316
this.styleValidity_(isValid);
317317

318-
const shouldShake = !isValid && !this.isFocused_;
318+
const shouldShake = !isValid && !this.isFocused_ && !!this.getValue();
319319
if (this.adapter_.hasLabel()) {
320320
this.adapter_.shakeLabel(shouldShake);
321321
}

test/screenshot/spec/mdc-textfield/fixture.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ window.mdc.testFixture.fontsLoaded.then(() => {
2929
textField.useNativeValidation = false;
3030
textField.valid = false;
3131
}
32+
33+
el.querySelector('.mdc-text-field__input').addEventListener('blur', () => {
34+
// Focusing out from empty text field input shouldn't trigger shake animation.
35+
textField.valid = false;
36+
});
3237
});
3338

3439
// Fixes the wide notched outline issue.

test/unit/mdc-textfield/foundation.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ test('#setValid overrides native validation when useNativeValidation set to fals
260260
test('#setValid updates classes', () => {
261261
const {foundation, mockAdapter, helperText} = setupTest({useHelperText: true});
262262
td.when(mockAdapter.hasLabel()).thenReturn(true);
263+
td.when(mockAdapter.getNativeInput()).thenReturn({
264+
value: 'test',
265+
});
263266

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

373+
test('#setValid should not trigger shake animation when text field is empty', () => {
374+
const {foundation, mockAdapter} = setupTest();
375+
td.when(mockAdapter.hasLabel()).thenReturn(true);
376+
td.when(mockAdapter.getNativeInput()).thenReturn({
377+
value: '',
378+
});
379+
foundation.setValid(false);
380+
td.verify(mockAdapter.shakeLabel(false));
381+
});
382+
370383
test('#init focuses on input if adapter.isFocused is true', () => {
371384
const {foundation, mockAdapter} = setupTest();
372385
td.when(mockAdapter.isFocused()).thenReturn(true);

0 commit comments

Comments
 (0)