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

Commit 737f712

Browse files
moog16Kenneth G. Franqueiro
authored and
Kenneth G. Franqueiro
committedDec 21, 2017
fix(textfield): Add isFocused to adapter in case autofocus attr is present (#1815)
BREAKING CHANGE: Added isFocused() to Text Field adapter
1 parent 0e9fbe1 commit 737f712

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed
 

‎demos/text-field.html

+7
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ <h2>Outlined Text Field</h2>
177177
<input id="outlined-rtl" type="checkbox">
178178
<label for="outlined-rtl">RTL</label>
179179
</div>
180+
<div>
181+
<input id="outlined-dark-theme" type="checkbox">
182+
<label for="outlined-dark-theme">Dark Theme</label>
183+
</div>
180184
<script>
181185
setTimeout(function() {
182186
var tfEl = document.getElementById('tf-outlined-example');
@@ -193,6 +197,9 @@ <h2>Outlined Text Field</h2>
193197
}
194198
tf.layout();
195199
});
200+
document.getElementById('outlined-dark-theme').addEventListener('change', function(evt) {
201+
wrapper.classList[evt.target.checked ? 'add' : 'remove']('mdc-theme--dark');
202+
});
196203
}, 0);
197204
</script>
198205
</section>

‎packages/mdc-textfield/adapter.js

+7
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ class MDCTextFieldAdapter {
128128
*/
129129
getIdleOutlineStyleValue(propertyName) {}
130130

131+
/**
132+
* Returns true if the textfield is focused.
133+
* We achieve this via `document.activeElement === this.root_`.
134+
* @return {boolean}
135+
*/
136+
isFocused() {}
137+
131138
/**
132139
* Returns true if the direction of the root element is set to RTL.
133140
* @return {boolean}

‎packages/mdc-textfield/foundation.js

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class MDCTextFieldFoundation extends MDCFoundation {
5959
deregisterBottomLineEventHandler: () => {},
6060
getNativeInput: () => {},
6161
getIdleOutlineStyleValue: () => {},
62+
isFocused: () => {},
6263
isRtl: () => {},
6364
});
6465
}
@@ -109,6 +110,10 @@ class MDCTextFieldFoundation extends MDCFoundation {
109110
this.label_.floatAbove();
110111
}
111112

113+
if (this.adapter_.isFocused()) {
114+
this.inputFocusHandler_();
115+
}
116+
112117
this.adapter_.registerInputInteractionHandler('focus', this.inputFocusHandler_);
113118
this.adapter_.registerInputInteractionHandler('blur', this.inputBlurHandler_);
114119
this.adapter_.registerInputInteractionHandler('input', this.inputInputHandler_);

‎packages/mdc-textfield/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ class MDCTextField extends MDCComponent {
220220
return window.getComputedStyle(idleOutlineElement).getPropertyValue(propertyName);
221221
}
222222
},
223+
isFocused: () => {
224+
return document.activeElement === this.root_.querySelector(strings.INPUT_SELECTOR);
225+
},
223226
isRtl: () => window.getComputedStyle(this.root_).getPropertyValue('direction') === 'rtl',
224227
},
225228
this.getInputAdapterMethods_())),

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test('defaultAdapter returns a complete adapter implementation', () => {
3939
'registerTextFieldInteractionHandler', 'deregisterTextFieldInteractionHandler',
4040
'registerInputInteractionHandler', 'deregisterInputInteractionHandler',
4141
'registerBottomLineEventHandler', 'deregisterBottomLineEventHandler',
42-
'getNativeInput', 'getIdleOutlineStyleValue', 'isRtl',
42+
'getNativeInput', 'getIdleOutlineStyleValue', 'isFocused', 'isRtl',
4343
]);
4444
});
4545

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

160+
test('#init focuses on input if adapter.isFocused is true', () => {
161+
const {foundation, mockAdapter} = setupTest();
162+
td.when(mockAdapter.isFocused()).thenReturn(true);
163+
foundation.init();
164+
td.verify(foundation.inputFocusHandler_());
165+
});
166+
167+
test('#init does not focus if adapter.isFocused is false', () => {
168+
const {foundation, mockAdapter} = setupTest();
169+
td.when(mockAdapter.isFocused()).thenReturn(false);
170+
foundation.init();
171+
td.verify(foundation.inputFocusHandler_(), {times: 0});
172+
});
173+
160174
test('#init adds event listeners', () => {
161175
const {foundation, mockAdapter} = setupTest();
162176
foundation.init();

0 commit comments

Comments
 (0)
This repository has been archived.