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

Commit

Permalink
fix(mdc-dialog): second dialog data-mdc-dialog-initial-focus doesn'…
Browse files Browse the repository at this point in the history
…t work

Fix `data-mdc-dialog-initial-focus` to work in multiple dialogs.
  • Loading branch information
ota-meshi committed Jun 26, 2020
1 parent 38197b4 commit a0ec7e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/mdc-dialog/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,6 @@ export class MDCDialog extends MDCComponent<MDCDialogFoundation> {
}

private getInitialFocusEl_(): HTMLElement|null {
return document.querySelector(`[${strings.INITIAL_FOCUS_ATTRIBUTE}]`);
return this.root.querySelector(`[${strings.INITIAL_FOCUS_ATTRIBUTE}]`);
}
}
13 changes: 12 additions & 1 deletion packages/mdc-dialog/test/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function getFixture() {
<button class="mdc-button mdc-dialog__button" data-mdc-dialog-action="no" type="button">
<span class="mdc-button__label">No</span>
</button>
<button class="mdc-button mdc-dialog__button" data-mdc-dialog-action="yes" type="button">
<button class="mdc-button mdc-dialog__button" data-mdc-dialog-action="yes" type="button" data-mdc-dialog-initial-focus>
<span class="mdc-button__label">Yes</span>
</button>
</div>
Expand Down Expand Up @@ -591,4 +591,15 @@ describe('MDCDialog', () => {
component.layout();
expect((component as any).foundation.layout).toHaveBeenCalled();
});

it(`${strings.INITIAL_FOCUS_ATTRIBUTE} will focus when the dialog is opened`, () => {
const {component: component1, yesButton: yesButton1} = setupTest();
const {component: component2, yesButton: yesButton2} = setupTest();

const initialFocusEl1 = (component1.getDefaultFoundation() as any).adapter.getInitialFocusEl();
expect(initialFocusEl1).toEqual(yesButton1);

const initialFocusEl2 = (component2.getDefaultFoundation() as any).adapter.getInitialFocusEl();
expect(initialFocusEl2).toEqual(yesButton2);
});
});

0 comments on commit a0ec7e2

Please sign in to comment.