Skip to content

Commit 000e81d

Browse files
haojonatLMulvey
authored andcommitted
[Fix] DayPicker: update calendarMonthWeeks if numberOfMonths is changed
Fixes #1394. Co-authored-by: Lee Mulvey <lmulvey@me.com> Co-authored-by: Jonathan Hao <haojonat@amazon.com>
1 parent 7a29ea7 commit 000e81d

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

src/components/DayPicker.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,19 @@ class DayPicker extends React.PureComponent {
391391
monthTitleHeight,
392392
} = this.state;
393393

394+
let shouldAdjustHeight = false;
395+
if (numberOfMonths !== prevProps.numberOfMonths) {
396+
this.setCalendarMonthWeeks(currentMonth);
397+
shouldAdjustHeight = true;
398+
}
394399
if (
395400
this.isHorizontal()
396401
&& (orientation !== prevProps.orientation || daySize !== prevProps.daySize)
397402
) {
403+
shouldAdjustHeight = true;
404+
}
405+
406+
if (shouldAdjustHeight) {
398407
const visibleCalendarWeeks = this.calendarMonthWeeks.slice(1, numberOfMonths + 1);
399408
const calendarMonthWeeksHeight = Math.max(0, ...visibleCalendarWeeks) * (daySize - 1);
400409
const newMonthHeight = monthTitleHeight + calendarMonthWeeksHeight + 1;

test/components/DayPicker_spec.jsx

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import CalendarMonthGrid from '../../src/components/CalendarMonthGrid';
1212
import DayPickerNavigation from '../../src/components/DayPickerNavigation';
1313
import DayPickerKeyboardShortcuts from '../../src/components/DayPickerKeyboardShortcuts';
1414
import {
15+
DAY_SIZE,
1516
HORIZONTAL_ORIENTATION,
1617
VERTICAL_ORIENTATION,
1718
VERTICAL_SCROLLABLE,
@@ -22,8 +23,9 @@ const today = moment().locale('en');
2223
const event = { preventDefault() {}, stopPropagation() {} };
2324

2425
describe('DayPicker', () => {
26+
let adjustDayPickerHeightSpy;
2527
beforeEach(() => {
26-
sinon.stub(PureDayPicker.prototype, 'adjustDayPickerHeight');
28+
adjustDayPickerHeightSpy = sinon.stub(PureDayPicker.prototype, 'adjustDayPickerHeight');
2729
});
2830

2931
afterEach(() => {
@@ -907,13 +909,8 @@ describe('DayPicker', () => {
907909
});
908910
});
909911

910-
describe.skip('life cycle methods', () => {
911-
let adjustDayPickerHeightSpy;
912-
beforeEach(() => {
913-
adjustDayPickerHeightSpy = sinon.stub(PureDayPicker.prototype, 'adjustDayPickerHeight');
914-
});
915-
916-
describe('#componentDidMount', () => {
912+
describe('life cycle methods', () => {
913+
describe.skip('#componentDidMount', () => {
917914
describe('props.orientation === HORIZONTAL_ORIENTATION', () => {
918915
it('calls adjustDayPickerHeight', () => {
919916
mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
@@ -927,7 +924,7 @@ describe('DayPicker', () => {
927924
});
928925
});
929926

930-
describe('props.orientation === VERTICAL_ORIENTATION', () => {
927+
describe.skip('props.orientation === VERTICAL_ORIENTATION', () => {
931928
it('does not call adjustDayPickerHeight', () => {
932929
mount(<DayPicker orientation={VERTICAL_ORIENTATION} />);
933930
expect(adjustDayPickerHeightSpy.called).to.equal(false);
@@ -949,7 +946,7 @@ describe('DayPicker', () => {
949946
});
950947
});
951948

952-
describe('#componentWillReceiveProps', () => {
949+
describe.skip('#componentWillReceiveProps', () => {
953950
describe('props.orientation === VERTICAL_SCROLLABLE', () => {
954951
it('updates state.currentMonthScrollTop', () => {
955952
sinon.spy(DayPicker.prototype, 'setTransitionContainerRef');
@@ -966,39 +963,45 @@ describe('DayPicker', () => {
966963

967964
describe('#componentDidUpdate', () => {
968965
let updateStateAfterMonthTransitionSpy;
966+
let setCalendarMonthWeeksSpy;
967+
969968
beforeEach(() => {
970969
updateStateAfterMonthTransitionSpy = sinon.stub(
971-
DayPicker.prototype,
970+
PureDayPicker.prototype,
972971
'updateStateAfterMonthTransition',
973972
);
973+
setCalendarMonthWeeksSpy = sinon.stub(
974+
PureDayPicker.prototype,
975+
'setCalendarMonthWeeks',
976+
);
974977
});
975978

976979
describe('props.orientation === HORIZONTAL_ORIENTATION', () => {
977-
it('calls adjustDayPickerHeight if state.monthTransition is truthy', () => {
980+
it.skip('calls adjustDayPickerHeight if state.monthTransition is truthy', () => {
978981
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
979982
wrapper.setState({
980983
monthTransition: 'foo',
981984
});
982985
expect(adjustDayPickerHeightSpy).to.have.property('callCount', 2);
983986
});
984987

985-
it('does not call adjustDayPickerHeight if state.monthTransition is falsy', () => {
988+
it.skip('does not call adjustDayPickerHeight if state.monthTransition is falsy', () => {
986989
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
987990
wrapper.setState({
988991
monthTransition: null,
989992
});
990993
expect(adjustDayPickerHeightSpy.calledTwice).to.equal(false);
991994
});
992995

993-
it('calls adjustDayPickerHeight if orientation has changed from HORIZONTAL_ORIENTATION to VERTICAL_ORIENTATION', () => {
996+
it.skip('calls adjustDayPickerHeight if orientation has changed from HORIZONTAL_ORIENTATION to VERTICAL_ORIENTATION', () => {
994997
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
995998
wrapper.setState({
996999
orientation: VERTICAL_ORIENTATION,
9971000
});
9981001
expect(adjustDayPickerHeightSpy).to.have.property('callCount', 2);
9991002
});
10001003

1001-
it('calls adjustDayPickerHeight if daySize has changed', () => {
1004+
it.skip('calls adjustDayPickerHeight if daySize has changed', () => {
10021005
const wrapper = mount(<DayPicker daySize={39} orientation={HORIZONTAL_ORIENTATION} />);
10031006
wrapper.setState({
10041007
daySize: 40,
@@ -1007,24 +1010,44 @@ describe('DayPicker', () => {
10071010
expect(adjustDayPickerHeightSpy).to.have.property('callCount', 2);
10081011
});
10091012

1010-
it('calls updateStateAfterMonthTransition if state.monthTransition is truthy', () => {
1013+
it.skip('calls updateStateAfterMonthTransition if state.monthTransition is truthy', () => {
10111014
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
10121015
wrapper.setState({
10131016
monthTransition: 'foo',
10141017
});
10151018
expect(updateStateAfterMonthTransitionSpy).to.have.property('callCount', 1);
10161019
});
10171020

1018-
it('does not call updateStateAfterMonthTransition if state.monthTransition is falsy', () => {
1021+
it.skip('does not call updateStateAfterMonthTransition if state.monthTransition is falsy', () => {
10191022
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
10201023
wrapper.setState({
10211024
monthTransition: null,
10221025
});
10231026
expect(updateStateAfterMonthTransitionSpy.calledOnce).to.equal(false);
10241027
});
1028+
1029+
it('calls adjustDayPickerHeightSpy if props.numberOfMonths changes', () => {
1030+
const wrapper = shallow(<DayPicker numberOfMonths={2} />).dive();
1031+
wrapper.instance().componentDidUpdate({
1032+
daySize: DAY_SIZE,
1033+
numberOfMonths: 3,
1034+
orientation: HORIZONTAL_ORIENTATION,
1035+
});
1036+
expect(adjustDayPickerHeightSpy.callCount).to.equal(1);
1037+
});
1038+
1039+
it('does not call adjustDayPickerHeightSpy if props.numberOfMonths does not change', () => {
1040+
const wrapper = shallow(<DayPicker numberOfMonths={2} />).dive();
1041+
wrapper.instance().componentDidUpdate({
1042+
daySize: DAY_SIZE,
1043+
numberOfMonths: 2,
1044+
orientation: HORIZONTAL_ORIENTATION,
1045+
});
1046+
expect(adjustDayPickerHeightSpy.called).to.equal(false);
1047+
});
10251048
});
10261049

1027-
describe('props.orientation === VERTICAL_ORIENTATION', () => {
1050+
describe.skip('props.orientation === VERTICAL_ORIENTATION', () => {
10281051
it('does not call adjustDayPickerHeight if state.monthTransition is truthy', () => {
10291052
const wrapper = mount(<DayPicker orientation={VERTICAL_ORIENTATION} />);
10301053
wrapper.setState({
@@ -1075,7 +1098,7 @@ describe('DayPicker', () => {
10751098
});
10761099
});
10771100

1078-
describe('props.orientation === VERTICAL_SCROLLABLE', () => {
1101+
describe.skip('props.orientation === VERTICAL_SCROLLABLE', () => {
10791102
it('does not update transitionContainer ref`s scrollTop currentMonth stays the same', () => {
10801103
sinon.spy(DayPicker.prototype, 'setTransitionContainerRef');
10811104
const wrapper = mount(<DayPicker orientation={VERTICAL_SCROLLABLE} />);
@@ -1097,7 +1120,7 @@ describe('DayPicker', () => {
10971120
});
10981121
});
10991122

1100-
describe('when isFocused is updated to true', () => {
1123+
describe.skip('when isFocused is updated to true', () => {
11011124
const prevProps = { isFocused: false };
11021125
const newProps = { isFocused: true };
11031126

0 commit comments

Comments
 (0)