Skip to content

Commit 42e5996

Browse files
haojonatLMulvey
authored andcommitted
[Fix] DayPicker: update calendarMonthWeeks if numberOfMonths is changed
Fixes react-dates#1394. Co-authored-by: Lee Mulvey <[email protected]> Co-authored-by: Jonathan Hao <[email protected]>
1 parent e78b84e commit 42e5996

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-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: 38 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,40 @@ describe('DayPicker', () => {
966963

967964
describe('#componentDidUpdate', () => {
968965
let updateStateAfterMonthTransitionSpy;
966+
969967
beforeEach(() => {
970968
updateStateAfterMonthTransitionSpy = sinon.stub(
971-
DayPicker.prototype,
969+
PureDayPicker.prototype,
972970
'updateStateAfterMonthTransition',
973971
);
974972
});
975973

976974
describe('props.orientation === HORIZONTAL_ORIENTATION', () => {
977-
it('calls adjustDayPickerHeight if state.monthTransition is truthy', () => {
975+
it.skip('calls adjustDayPickerHeight if state.monthTransition is truthy', () => {
978976
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
979977
wrapper.setState({
980978
monthTransition: 'foo',
981979
});
982980
expect(adjustDayPickerHeightSpy).to.have.property('callCount', 2);
983981
});
984982

985-
it('does not call adjustDayPickerHeight if state.monthTransition is falsy', () => {
983+
it.skip('does not call adjustDayPickerHeight if state.monthTransition is falsy', () => {
986984
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
987985
wrapper.setState({
988986
monthTransition: null,
989987
});
990988
expect(adjustDayPickerHeightSpy.calledTwice).to.equal(false);
991989
});
992990

993-
it('calls adjustDayPickerHeight if orientation has changed from HORIZONTAL_ORIENTATION to VERTICAL_ORIENTATION', () => {
991+
it.skip('calls adjustDayPickerHeight if orientation has changed from HORIZONTAL_ORIENTATION to VERTICAL_ORIENTATION', () => {
994992
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
995993
wrapper.setState({
996994
orientation: VERTICAL_ORIENTATION,
997995
});
998996
expect(adjustDayPickerHeightSpy).to.have.property('callCount', 2);
999997
});
1000998

1001-
it('calls adjustDayPickerHeight if daySize has changed', () => {
999+
it.skip('calls adjustDayPickerHeight if daySize has changed', () => {
10021000
const wrapper = mount(<DayPicker daySize={39} orientation={HORIZONTAL_ORIENTATION} />);
10031001
wrapper.setState({
10041002
daySize: 40,
@@ -1007,24 +1005,44 @@ describe('DayPicker', () => {
10071005
expect(adjustDayPickerHeightSpy).to.have.property('callCount', 2);
10081006
});
10091007

1010-
it('calls updateStateAfterMonthTransition if state.monthTransition is truthy', () => {
1008+
it.skip('calls updateStateAfterMonthTransition if state.monthTransition is truthy', () => {
10111009
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
10121010
wrapper.setState({
10131011
monthTransition: 'foo',
10141012
});
10151013
expect(updateStateAfterMonthTransitionSpy).to.have.property('callCount', 1);
10161014
});
10171015

1018-
it('does not call updateStateAfterMonthTransition if state.monthTransition is falsy', () => {
1016+
it.skip('does not call updateStateAfterMonthTransition if state.monthTransition is falsy', () => {
10191017
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
10201018
wrapper.setState({
10211019
monthTransition: null,
10221020
});
10231021
expect(updateStateAfterMonthTransitionSpy.calledOnce).to.equal(false);
10241022
});
1023+
1024+
it('calls adjustDayPickerHeightSpy if props.numberOfMonths changes', () => {
1025+
const wrapper = shallow(<DayPicker numberOfMonths={2} />).dive();
1026+
wrapper.instance().componentDidUpdate({
1027+
daySize: DAY_SIZE,
1028+
numberOfMonths: 3,
1029+
orientation: HORIZONTAL_ORIENTATION,
1030+
});
1031+
expect(adjustDayPickerHeightSpy.callCount).to.equal(1);
1032+
});
1033+
1034+
it('does not call adjustDayPickerHeightSpy if props.numberOfMonths does not change', () => {
1035+
const wrapper = shallow(<DayPicker numberOfMonths={2} />).dive();
1036+
wrapper.instance().componentDidUpdate({
1037+
daySize: DAY_SIZE,
1038+
numberOfMonths: 2,
1039+
orientation: HORIZONTAL_ORIENTATION,
1040+
});
1041+
expect(adjustDayPickerHeightSpy.called).to.equal(false);
1042+
});
10251043
});
10261044

1027-
describe('props.orientation === VERTICAL_ORIENTATION', () => {
1045+
describe.skip('props.orientation === VERTICAL_ORIENTATION', () => {
10281046
it('does not call adjustDayPickerHeight if state.monthTransition is truthy', () => {
10291047
const wrapper = mount(<DayPicker orientation={VERTICAL_ORIENTATION} />);
10301048
wrapper.setState({
@@ -1075,7 +1093,7 @@ describe('DayPicker', () => {
10751093
});
10761094
});
10771095

1078-
describe('props.orientation === VERTICAL_SCROLLABLE', () => {
1096+
describe.skip('props.orientation === VERTICAL_SCROLLABLE', () => {
10791097
it('does not update transitionContainer ref`s scrollTop currentMonth stays the same', () => {
10801098
sinon.spy(DayPicker.prototype, 'setTransitionContainerRef');
10811099
const wrapper = mount(<DayPicker orientation={VERTICAL_SCROLLABLE} />);
@@ -1097,7 +1115,7 @@ describe('DayPicker', () => {
10971115
});
10981116
});
10991117

1100-
describe('when isFocused is updated to true', () => {
1118+
describe.skip('when isFocused is updated to true', () => {
11011119
const prevProps = { isFocused: false };
11021120
const newProps = { isFocused: true };
11031121

0 commit comments

Comments
 (0)