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

fix(textfield): added isFocused to adapter incase autofocus attr is present #1815

Merged
merged 6 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions demos/text-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<div class="mdc-toolbar-fixed-adjust"></div>
<section class="hero">
<div class="mdc-text-field">
<input type="text" class="mdc-text-field__input" id="my-text-field" aria-controls="my-text-field-helper-text">
<input type="text" class="mdc-text-field__input" aria-controls="my-text-field-helper-text">
<label for="my-text-field" class="mdc-text-field__label">Text Field</label>
<div class="mdc-text-field__bottom-line"></div>
</div>
Expand All @@ -90,7 +90,7 @@
<h2>Full Functionality JS Component (Floating Label, Validation)</h2>
<section id="demo-text-field-wrapper">
<div class="mdc-text-field">
<input type="text" class="mdc-text-field__input" id="my-text-field" aria-controls="my-text-field-helper-text">
<input type="text" class="mdc-text-field__input" aria-controls="my-text-field-helper-text">
<label for="my-text-field" class="mdc-text-field__label">Email Address</label>
<div class="mdc-text-field__bottom-line"></div>
</div>
Expand Down 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
1 change: 1 addition & 0 deletions packages/mdc-ripple/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ class MDCRippleFoundation extends MDCFoundation {
}
this.layoutFrame_ = requestAnimationFrame(() => {
this.layoutInternal_();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing...

this.layoutFrame_ = 0;
});
}
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