Skip to content

Commit

Permalink
fix: do not throw on focus before adding to the DOM (#7365) (#7366)
Browse files Browse the repository at this point in the history
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
  • Loading branch information
vaadin-bot and web-padawan committed Apr 29, 2024
1 parent a31e53d commit 4af11f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/date-time-picker/src/vaadin-date-time-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,9 @@ class DateTimePicker extends FieldMixin(DisabledMixin(FocusMixin(ThemableMixin(E
}

focus() {
this.__datePicker.focus();
if (this.__datePicker) {
this.__datePicker.focus();
}
}

/**
Expand Down
14 changes: 11 additions & 3 deletions packages/date-time-picker/test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,17 @@ describe('Basic features', () => {
expect(dateTimePicker.value).to.equal('2019-09-19T15:00');
});

it('should delegate focus() to date picker', () => {
dateTimePicker.focus();
expect(datePicker.hasAttribute('focused')).to.be.true;
describe('focus', () => {
it('should focus the date-picker when calling focus()', () => {
const spy = sinon.spy(datePicker, 'focus');
dateTimePicker.focus();
expect(spy).to.be.calledOnce;
});

it('should not throw on focus when not attached to the DOM', () => {
const element = document.createElement('vaadin-date-time-picker');
expect(() => element.focus()).not.to.throw(Error);
});
});

describe('focused', () => {
Expand Down

0 comments on commit 4af11f8

Please sign in to comment.