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

Commit 68c08f7

Browse files
Matty GooKenneth G. Franqueiro
authored andcommitted
fix(select): Float label on focus/blur (#2560)
1 parent 5ba7e27 commit 68c08f7

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

packages/mdc-select/foundation.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ export default class MDCSelectFoundation extends MDCFoundation {
7373

7474
this.adapter_.setSelectedIndex(index);
7575
this.adapter_.addClass(IS_CHANGING);
76-
const optionHasValue = this.adapter_.getValue().length > 0;
77-
78-
this.adapter_.floatLabel(optionHasValue);
76+
this.floatLabelWithValue_();
7977

8078
setTimeout(() => {
8179
this.adapter_.removeClass(IS_CHANGING);
@@ -97,11 +95,18 @@ export default class MDCSelectFoundation extends MDCFoundation {
9795
}
9896
}
9997

98+
floatLabelWithValue_() {
99+
const optionHasValue = this.adapter_.getValue().length > 0;
100+
this.adapter_.floatLabel(optionHasValue);
101+
}
102+
100103
handleFocus_() {
104+
this.adapter_.floatLabel(true);
101105
this.adapter_.activateBottomLine();
102106
}
103107

104108
handleBlur_() {
109+
this.floatLabelWithValue_();
105110
this.adapter_.deactivateBottomLine();
106111
}
107112

test/unit/mdc-select/foundation-events.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,34 @@ function setupTest() {
3333

3434
suite('MDCSelectFoundation - Events');
3535

36-
test('on focus activates bottom line', () => {
36+
test('on focus activates bottom line and floats label', () => {
3737
const {mockAdapter, handlers} = setupTest();
3838
handlers.focus();
3939
td.verify(mockAdapter.activateBottomLine(), {times: 1});
40+
td.verify(mockAdapter.floatLabel(true), {times: 1});
4041
});
4142

4243
test('on blur deactivates bottom line', () => {
4344
const {mockAdapter, handlers} = setupTest();
45+
td.when(mockAdapter.getValue()).thenReturn('');
4446
handlers.blur();
4547
td.verify(mockAdapter.deactivateBottomLine(), {times: 1});
4648
});
4749

50+
test('on blur with no value defloats label', () => {
51+
const {mockAdapter, handlers} = setupTest();
52+
td.when(mockAdapter.getValue()).thenReturn('');
53+
handlers.blur();
54+
td.verify(mockAdapter.floatLabel(false), {times: 1});
55+
});
56+
57+
test('on blur with value floats label', () => {
58+
const {mockAdapter, handlers} = setupTest();
59+
td.when(mockAdapter.getValue()).thenReturn('some value');
60+
handlers.blur();
61+
td.verify(mockAdapter.floatLabel(true), {times: 1});
62+
});
63+
4864
test('on select value change with option value', () => {
4965
const clock = lolex.install();
5066
const {mockAdapter, handlers} = setupTest();

0 commit comments

Comments
 (0)