Skip to content

Commit

Permalink
fix(select): Float label on focus/blur (#2560)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matty Goo authored and Kenneth G. Franqueiro committed Apr 16, 2018
1 parent 5ba7e27 commit 68c08f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 8 additions & 3 deletions packages/mdc-select/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ export default class MDCSelectFoundation extends MDCFoundation {

this.adapter_.setSelectedIndex(index);
this.adapter_.addClass(IS_CHANGING);
const optionHasValue = this.adapter_.getValue().length > 0;

this.adapter_.floatLabel(optionHasValue);
this.floatLabelWithValue_();

setTimeout(() => {
this.adapter_.removeClass(IS_CHANGING);
Expand All @@ -97,11 +95,18 @@ export default class MDCSelectFoundation extends MDCFoundation {
}
}

floatLabelWithValue_() {
const optionHasValue = this.adapter_.getValue().length > 0;
this.adapter_.floatLabel(optionHasValue);
}

handleFocus_() {
this.adapter_.floatLabel(true);
this.adapter_.activateBottomLine();
}

handleBlur_() {
this.floatLabelWithValue_();
this.adapter_.deactivateBottomLine();
}

Expand Down
18 changes: 17 additions & 1 deletion test/unit/mdc-select/foundation-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,34 @@ function setupTest() {

suite('MDCSelectFoundation - Events');

test('on focus activates bottom line', () => {
test('on focus activates bottom line and floats label', () => {
const {mockAdapter, handlers} = setupTest();
handlers.focus();
td.verify(mockAdapter.activateBottomLine(), {times: 1});
td.verify(mockAdapter.floatLabel(true), {times: 1});
});

test('on blur deactivates bottom line', () => {
const {mockAdapter, handlers} = setupTest();
td.when(mockAdapter.getValue()).thenReturn('');
handlers.blur();
td.verify(mockAdapter.deactivateBottomLine(), {times: 1});
});

test('on blur with no value defloats label', () => {
const {mockAdapter, handlers} = setupTest();
td.when(mockAdapter.getValue()).thenReturn('');
handlers.blur();
td.verify(mockAdapter.floatLabel(false), {times: 1});
});

test('on blur with value floats label', () => {
const {mockAdapter, handlers} = setupTest();
td.when(mockAdapter.getValue()).thenReturn('some value');
handlers.blur();
td.verify(mockAdapter.floatLabel(true), {times: 1});
});

test('on select value change with option value', () => {
const clock = lolex.install();
const {mockAdapter, handlers} = setupTest();
Expand Down

0 comments on commit 68c08f7

Please sign in to comment.