Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error with focusing outside nodes #646

Merged
merged 2 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DayPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export default class DayPicker extends Component {
focusPreviousDay(dayNode) {
const dayNodes = Helpers.getDayNodes(this.dayPicker, this.props.classNames);
const dayNodeIndex = Helpers.nodeListToArray(dayNodes).indexOf(dayNode);

if (dayNodeIndex === -1) return;
if (dayNodeIndex === 0) {
this.showPreviousMonth(() => this.focusLastDayOfMonth());
} else {
Expand All @@ -311,7 +311,7 @@ export default class DayPicker extends Component {
focusNextDay(dayNode) {
const dayNodes = Helpers.getDayNodes(this.dayPicker, this.props.classNames);
const dayNodeIndex = Helpers.nodeListToArray(dayNodes).indexOf(dayNode);

if (dayNodeIndex === -1) return;
if (dayNodeIndex === dayNodes.length - 1) {
this.showNextMonth(() => this.focusFirstDayOfMonth());
} else {
Expand Down
10 changes: 10 additions & 0 deletions test/daypicker/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ describe('DayPicker’s methods', () => {
expect(document.activeElement.innerHTML).toBe('31');
expect(instance.state.currentMonth.getMonth()).toBe(4);
});
it('should not throw an error when the node is not found', () => {
expect(() =>
instance.focusPreviousDay(document.createElement('div'))
).not.toThrow();
});
});

describe('focusNextDay()', () => {
Expand Down Expand Up @@ -301,6 +306,11 @@ describe('DayPicker’s methods', () => {
expect(document.activeElement.innerHTML).toBe('1');
expect(instance.state.currentMonth.getMonth()).toBe(2);
});
it('should not throw an error when the node is not found', () => {
expect(() =>
instance.focusNextDay(document.createElement('div'))
).not.toThrow();
});
});

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