Skip to content

Commit

Permalink
fix(textfield): added isFocused to adapter incase autofocus attr is p…
Browse files Browse the repository at this point in the history
…resent (#1815)

BREAKING CHANGE: Added isFocused() to Tabs adapter

* fix(textfield): added isFocused to adapter to focus on input autofocus attr is present

* fix(textfield): revert change addressed in PR #1813

* fix(textfield): removed unnecessary newline
  • Loading branch information
moog16 authored Dec 21, 2017
1 parent 56d329d commit a1550a7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions demos/text-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ <h2>Outlined Text Field</h2>
<input id="outlined-rtl" type="checkbox">
<label for="outlined-rtl">RTL</label>
</div>
<div>
<input id="outlined-dark-theme" type="checkbox">
<label for="outlined-dark-theme">Dark Theme</label>
</div>
<script>
setTimeout(function() {
var tfEl = document.getElementById('tf-outlined-example');
Expand All @@ -193,6 +197,9 @@ <h2>Outlined Text Field</h2>
}
tf.layout();
});
document.getElementById('outlined-dark-theme').addEventListener('change', function(evt) {
wrapper.classList[evt.target.checked ? 'add' : 'remove']('mdc-theme--dark');
});
}, 0);
</script>
</section>
Expand Down
7 changes: 7 additions & 0 deletions packages/mdc-textfield/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ class MDCTextFieldAdapter {
*/
getIdleOutlineStyleValue(propertyName) {}

/**
* Returns true if the textfield is focused.
* We achieve this via `document.activeElement === this.root_`.
* @return {boolean}
*/
isFocused() {}

/**
* Returns true if the direction of the root element is set to RTL.
* @return {boolean}
Expand Down
5 changes: 5 additions & 0 deletions packages/mdc-textfield/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class MDCTextFieldFoundation extends MDCFoundation {
deregisterBottomLineEventHandler: () => {},
getNativeInput: () => {},
getIdleOutlineStyleValue: () => {},
isFocused: () => {},
isRtl: () => {},
});
}
Expand Down Expand Up @@ -109,6 +110,10 @@ class MDCTextFieldFoundation extends MDCFoundation {
this.label_.floatAbove();
}

if (this.adapter_.isFocused()) {
this.inputFocusHandler_();
}

this.adapter_.registerInputInteractionHandler('focus', this.inputFocusHandler_);
this.adapter_.registerInputInteractionHandler('blur', this.inputBlurHandler_);
this.adapter_.registerInputInteractionHandler('input', this.inputInputHandler_);
Expand Down
3 changes: 3 additions & 0 deletions packages/mdc-textfield/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ class MDCTextField extends MDCComponent {
return window.getComputedStyle(idleOutlineElement).getPropertyValue(propertyName);
}
},
isFocused: () => {
return document.activeElement === this.root_.querySelector(strings.INPUT_SELECTOR);
},
isRtl: () => window.getComputedStyle(this.root_).getPropertyValue('direction') === 'rtl',
},
this.getInputAdapterMethods_())),
Expand Down
16 changes: 15 additions & 1 deletion test/unit/mdc-textfield/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test('defaultAdapter returns a complete adapter implementation', () => {
'registerTextFieldInteractionHandler', 'deregisterTextFieldInteractionHandler',
'registerInputInteractionHandler', 'deregisterInputInteractionHandler',
'registerBottomLineEventHandler', 'deregisterBottomLineEventHandler',
'getNativeInput', 'getIdleOutlineStyleValue', 'isRtl',
'getNativeInput', 'getIdleOutlineStyleValue', 'isFocused', 'isRtl',
]);
});

Expand Down Expand Up @@ -157,6 +157,20 @@ test('#init adds mdc-text-field--upgraded class', () => {
td.verify(mockAdapter.addClass(cssClasses.UPGRADED));
});

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

test('#init does not focus if adapter.isFocused is false', () => {
const {foundation, mockAdapter} = setupTest();
td.when(mockAdapter.isFocused()).thenReturn(false);
foundation.init();
td.verify(foundation.inputFocusHandler_(), {times: 0});
});

test('#init adds event listeners', () => {
const {foundation, mockAdapter} = setupTest();
foundation.init();
Expand Down

0 comments on commit a1550a7

Please sign in to comment.