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

Pass currentMonth into onPrevMonthClick and onNextMonthClick #667

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
10 changes: 6 additions & 4 deletions src/components/DayPickerRangeController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,16 @@ export default class DayPickerRangeController extends React.Component {
const prevMonth = currentMonth.clone().subtract(2, 'months');
const prevMonthVisibleDays = getVisibleDays(prevMonth, 1, enableOutsideDays, true);

const newCurrentMonth = currentMonth.clone().subtract(1, 'month');
this.setState({
currentMonth: currentMonth.clone().subtract(1, 'month'),
currentMonth: newCurrentMonth,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it miiiight be a good idea to return a different clone to the callback as the one in the state... just in case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.  ✅  Fixed, 1c4bc4e.

visibleDays: {
...newVisibleDays,
...this.getModifiers(prevMonthVisibleDays),
},
});

onPrevMonthClick();
onPrevMonthClick(newCurrentMonth.clone());
}

onNextMonthClick() {
Expand All @@ -514,15 +515,16 @@ export default class DayPickerRangeController extends React.Component {
const nextMonth = currentMonth.clone().add(numberOfMonths + 1, 'month');
const nextMonthVisibleDays = getVisibleDays(nextMonth, 1, enableOutsideDays, true);

const newCurrentMonth = currentMonth.clone().add(1, 'month');
this.setState({
currentMonth: currentMonth.clone().add(1, 'month'),
currentMonth: newCurrentMonth,
visibleDays: {
...newVisibleDays,
...this.getModifiers(nextMonthVisibleDays),
},
});

onNextMonthClick();
onNextMonthClick(newCurrentMonth.clone());
}

onMultiplyScrollableMonths() {
Expand Down
7 changes: 4 additions & 3 deletions src/components/DayPickerSingleDateController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export default class DayPickerSingleDateController extends React.Component {
},
});

onPrevMonthClick();
onPrevMonthClick(prevMonth.clone());
}

onNextMonthClick() {
Expand All @@ -346,15 +346,16 @@ export default class DayPickerSingleDateController extends React.Component {
const nextMonth = currentMonth.clone().add(numberOfMonths, 'month');
const nextMonthVisibleDays = getVisibleDays(nextMonth, 1, enableOutsideDays);

const newCurrentMonth = currentMonth.clone().add(1, 'month');
this.setState({
currentMonth: currentMonth.clone().add(1, 'month'),
currentMonth: newCurrentMonth,
visibleDays: {
...newVisibleDays,
...this.getModifiers(nextMonthVisibleDays),
},
});

onNextMonthClick();
onNextMonthClick(newCurrentMonth.clone());
}


Expand Down
16 changes: 14 additions & 2 deletions test/components/DayPickerRangeController_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,7 @@ describe('DayPickerRangeController', () => {
expect(getModifiersSpy.callCount).to.equal(1);
});

it('calls props.onPrevMonthClick', () => {
it('calls props.onPrevMonthClick with new month', () => {
const onPrevMonthClickStub = sinon.stub();
const wrapper = shallow(
<DayPickerRangeController
Expand All @@ -1997,8 +1997,14 @@ describe('DayPickerRangeController', () => {
onPrevMonthClick={onPrevMonthClickStub}
/>,
);
wrapper.setState({
currentMonth: today,
});
const newMonth = moment().subtract(1, 'month');
wrapper.instance().onPrevMonthClick();
expect(onPrevMonthClickStub.callCount).to.equal(1);
expect(onPrevMonthClickStub.firstCall.args[0].year()).to.equal(newMonth.year());
expect(onPrevMonthClickStub.firstCall.args[0].month()).to.equal(newMonth.month());
});
});

Expand Down Expand Up @@ -2064,7 +2070,7 @@ describe('DayPickerRangeController', () => {
expect(getModifiersSpy.callCount).to.equal(1);
});

it('calls props.onNextMonthClick', () => {
it('calls props.onNextMonthClick with new month', () => {
const onNextMonthClickStub = sinon.stub();
const wrapper = shallow(
<DayPickerRangeController
Expand All @@ -2073,8 +2079,14 @@ describe('DayPickerRangeController', () => {
onNextMonthClick={onNextMonthClickStub}
/>,
);
wrapper.setState({
currentMonth: today,
});
const newMonth = moment().add(1, 'month');
wrapper.instance().onNextMonthClick();
expect(onNextMonthClickStub.callCount).to.equal(1);
expect(onNextMonthClickStub.firstCall.args[0].year()).to.equal(newMonth.year());
expect(onNextMonthClickStub.firstCall.args[0].month()).to.equal(newMonth.month());
});
});

Expand Down
16 changes: 14 additions & 2 deletions test/components/DayPickerSingleDateController_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ describe('DayPickerSingleDateController', () => {
expect(getModifiersSpy.callCount).to.equal(1);
});

it('calls props.onPrevMonthClick', () => {
it('calls props.onPrevMonthClick with new month', () => {
const onPrevMonthClickStub = sinon.stub();
const wrapper = shallow(
<DayPickerSingleDateController
Expand All @@ -714,8 +714,14 @@ describe('DayPickerSingleDateController', () => {
onPrevMonthClick={onPrevMonthClickStub}
/>,
);
wrapper.setState({
currentMonth: today,
});
const newMonth = moment().subtract(1, 'month');
wrapper.instance().onPrevMonthClick();
expect(onPrevMonthClickStub.callCount).to.equal(1);
expect(onPrevMonthClickStub.firstCall.args[0].year()).to.equal(newMonth.year());
expect(onPrevMonthClickStub.firstCall.args[0].month()).to.equal(newMonth.month());
});
});

Expand Down Expand Up @@ -781,7 +787,7 @@ describe('DayPickerSingleDateController', () => {
expect(getModifiersSpy.callCount).to.equal(1);
});

it('calls props.onNextMonthClick', () => {
it('calls props.onNextMonthClick with new month', () => {
const onNextMonthClickStub = sinon.stub();
const wrapper = shallow(
<DayPickerSingleDateController
Expand All @@ -790,8 +796,14 @@ describe('DayPickerSingleDateController', () => {
onNextMonthClick={onNextMonthClickStub}
/>,
);
wrapper.setState({
currentMonth: today,
});
const newMonth = moment().add(1, 'month');
wrapper.instance().onNextMonthClick();
expect(onNextMonthClickStub.callCount).to.equal(1);
expect(onNextMonthClickStub.firstCall.args[0].year()).to.equal(newMonth.year());
expect(onNextMonthClickStub.firstCall.args[0].month()).to.equal(newMonth.month());
});
});

Expand Down