Skip to content

Commit fd7121e

Browse files
authored
Merge pull request #646 from gpbl/fix-630
Fix error with focusing outside nodes
2 parents 6d16700 + cf40a4c commit fd7121e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/DayPicker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ export default class DayPicker extends Component {
300300
focusPreviousDay(dayNode) {
301301
const dayNodes = Helpers.getDayNodes(this.dayPicker, this.props.classNames);
302302
const dayNodeIndex = Helpers.nodeListToArray(dayNodes).indexOf(dayNode);
303-
303+
if (dayNodeIndex === -1) return;
304304
if (dayNodeIndex === 0) {
305305
this.showPreviousMonth(() => this.focusLastDayOfMonth());
306306
} else {
@@ -311,7 +311,7 @@ export default class DayPicker extends Component {
311311
focusNextDay(dayNode) {
312312
const dayNodes = Helpers.getDayNodes(this.dayPicker, this.props.classNames);
313313
const dayNodeIndex = Helpers.nodeListToArray(dayNodes).indexOf(dayNode);
314-
314+
if (dayNodeIndex === -1) return;
315315
if (dayNodeIndex === dayNodes.length - 1) {
316316
this.showNextMonth(() => this.focusFirstDayOfMonth());
317317
} else {

test/daypicker/methods.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ describe('DayPicker’s methods', () => {
258258
expect(document.activeElement.innerHTML).toBe('31');
259259
expect(instance.state.currentMonth.getMonth()).toBe(4);
260260
});
261+
it('should not throw an error when the node is not found', () => {
262+
expect(() =>
263+
instance.focusPreviousDay(document.createElement('div'))
264+
).not.toThrow();
265+
});
261266
});
262267

263268
describe('focusNextDay()', () => {
@@ -301,6 +306,11 @@ describe('DayPicker’s methods', () => {
301306
expect(document.activeElement.innerHTML).toBe('1');
302307
expect(instance.state.currentMonth.getMonth()).toBe(2);
303308
});
309+
it('should not throw an error when the node is not found', () => {
310+
expect(() =>
311+
instance.focusNextDay(document.createElement('div'))
312+
).not.toThrow();
313+
});
304314
});
305315

306316
describe('focusNextWeek()', () => {

0 commit comments

Comments
 (0)