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

Commit b3ab9e1

Browse files
material-web-copybaraasyncliz
authored andcommitted
Project import generated by Copybara.
PiperOrigin-RevId: 288775969
1 parent 615f86f commit b3ab9e1

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

packages/mdc-textfield/icon/foundation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export class MDCTextFieldIconFoundation extends MDCFoundation<MDCTextFieldIconAd
104104
handleInteraction(evt: MouseEvent | KeyboardEvent) {
105105
const isEnterKey = (evt as KeyboardEvent).key === 'Enter' || (evt as KeyboardEvent).keyCode === 13;
106106
if (evt.type === 'click' || isEnterKey) {
107+
evt.preventDefault(); // stop click from causing host label to focus
108+
// input
107109
this.adapter_.notifyIconAction();
108110
}
109111
}

packages/mdc-textfield/test/component.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ const {cssClasses} = MDCTextFieldFoundation;
3636
const getFixture = () => {
3737
const wrapper = document.createElement('div');
3838
wrapper.innerHTML = `
39-
<div class="mdc-text-field mdc-text-field--with-leading-icon">
39+
<label class="mdc-text-field mdc-text-field--with-leading-icon">
4040
<i class="material-icons mdc-text-field__icon mdc-text-field__icon--leading" tabindex="0" role="button">event</i>
41-
<input type="text" class="mdc-text-field__input" id="my-text-field">
42-
<label class="mdc-floating-label" for="my-text-field">My Label</label>
41+
<input type="text" class="mdc-text-field__input" aria-labelledby="my-label">
42+
<span class="mdc-floating-label" id="my-label">My Label</span>
4343
<div class="mdc-line-ripple"></div>
44-
</div>
44+
</label>
4545
`;
4646
const el = wrapper.firstElementChild as HTMLElement;
4747
wrapper.removeChild(el);
@@ -674,6 +674,24 @@ describe('MDCTextField', () => {
674674
expect(lineRipple.setRippleCenter).toHaveBeenCalledWith(100);
675675
});
676676

677+
it('should not focus input when clicking icon', () => {
678+
const root = getFixture();
679+
const icon = root.querySelector('.mdc-text-field__icon');
680+
const component = new MDCTextField(root);
681+
document.body.appendChild(root);
682+
component.root_.click();
683+
assert.equal(
684+
component.input_, document.activeElement, 'input_ should be focused');
685+
component.input_.blur();
686+
assert.notEqual(
687+
component.input_, document.activeElement, 'ensure input_ was blurred');
688+
icon.click();
689+
assert.notEqual(
690+
component.input_, document.activeElement,
691+
'input_ should not be focused');
692+
document.body.removeChild(root);
693+
});
694+
677695
function setupMockFoundationTest(root = getFixture()) {
678696
const mockFoundation = createMockFoundation(MDCTextFieldFoundation);
679697
const component = new MDCTextField(root, mockFoundation);

0 commit comments

Comments
 (0)