Skip to content

Commit 7ebddf4

Browse files
authored
fix(popover): DLT-3550 guard focusFirstElementIfNeeded against unset content ref (#1407)
1 parent 9618ddd commit 7ebddf4

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

packages/dialtone-vue/components/Popover/Popover.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,24 @@ describe('DtPopover Tests', () => {
471471
expect(document.activeElement).toBe(button.element);
472472
});
473473
});
474+
475+
describe('When there are no focusable elements and the content ref is unset (DLT-3550)', () => {
476+
beforeEach(async () => {
477+
mockProps = { showCloseButton: false, initialFocusElement: 'first' };
478+
mockSlots = { content: 'No focusable elements here' };
479+
480+
updateWrapper();
481+
await wrapper.setProps({ open: true });
482+
await flushPromises();
483+
});
484+
485+
it('does not throw when a delayed after-enter callback fires against a stale content ref', () => {
486+
const domEl = wrapper.vm.$refs.popover__content;
487+
wrapper.vm.$refs.content = null;
488+
489+
expect(() => wrapper.vm.focusFirstElementIfNeeded(domEl)).not.toThrow();
490+
});
491+
});
474492
});
475493

476494
describe('Pass-through class props', () => {

packages/dialtone-vue/components/Popover/Popover.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ export default {
10931093
this.$refs.popover__header?.focusCloseButton();
10941094
} else {
10951095
// if there are no focusable elements at all focus the dialog itself
1096-
returnFirstEl(this.$refs.content?.$el).focus();
1096+
returnFirstEl(this.$refs.content?.$el)?.focus();
10971097
}
10981098
},
10991099

0 commit comments

Comments
 (0)