Skip to content
Open
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
50 changes: 27 additions & 23 deletions src/components/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ class Calendar extends PureComponent {
...range,
color: range.color || rangeColors[i] || color,
}));

const months = new Array(this.props.months).fill().map((_, i) => {
return addMonths(this.state.focusedDate, i);
});

return (
<CalendarWrapper
className={classnames(this.styles.calendarWrapper, this.props.className)}
Expand Down Expand Up @@ -365,29 +370,26 @@ class Calendar extends PureComponent {
</div>
) : (
<Months direction={direction}>
{new Array(this.props.months).fill(null).map((_, i) => {
const monthStep = addMonths(this.state.focusedDate, i);
return (
<Month
{...this.props}
onPreviewChange={this.props.onPreviewChange || this.updatePreview}
preview={this.props.preview || this.state.preview}
ranges={ranges}
key={i}
drag={this.state.drag}
dateOptions={this.dateOptions}
disabledDates={disabledDates}
month={monthStep}
onDragSelectionStart={this.onDragSelectionStart}
onDragSelectionEnd={this.onDragSelectionEnd}
onDragSelectionMove={this.onDragSelectionMove}
onMouseLeave={() => onPreviewChange && onPreviewChange()}
styles={this.styles}
showWeekDays={!isVertical || i === 0}
showMonthName={!isVertical || i > 0}
/>
);
})}
{months.map((monthStep, i) => (
<Month
{...this.props}
onPreviewChange={this.props.onPreviewChange || this.updatePreview}
preview={this.props.preview || this.state.preview}
ranges={ranges}
key={i}
drag={this.state.drag}
dateOptions={this.dateOptions}
disabledDates={disabledDates}
month={monthStep}
onDragSelectionStart={this.onDragSelectionStart}
onDragSelectionEnd={this.onDragSelectionEnd}
onDragSelectionMove={this.onDragSelectionMove}
onMouseLeave={() => onPreviewChange && onPreviewChange()}
styles={this.styles}
showWeekDays={!isVertical || i === 0}
showMonthName={!isVertical || i > 0}
/>
))}
</Months>
)}
</CalendarWrapper>
Expand All @@ -396,6 +398,7 @@ class Calendar extends PureComponent {
}

Calendar.defaultProps = {
startOnPreviousMonth: true,
disabledDates: [],
classNames: {},
locale: defaultLocale,
Expand Down Expand Up @@ -424,6 +427,7 @@ Calendar.defaultProps = {
};

Calendar.propTypes = {
startOnPreviousMonth: PropTypes.bool,
disabledDates: PropTypes.array,
minDate: PropTypes.object,
maxDate: PropTypes.object,
Expand Down
20 changes: 20 additions & 0 deletions src/components/__tests__/Calendar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ describe('The Calendar Component', () => {
const { queryByTestId } = render(<Calendar months={months} />);
expect(queryByTestId('month').children).toHaveLength(months);
});

it('should show the previous month by default', () => {
const { container, getByText } = render(<Calendar months={months} direction="horizontal" />);

const previousMonth = `${format(
subMonths(new Date(), 1),
'MMM'
)} ${new Date().getFullYear()}`;
expect(container).toContainElement(getByText(previousMonth));
});

it('should show the next month with startOnPreviousMonth=false', () => {
const nextMonth = `${format(addMonths(new Date(), 1), 'MMM')} ${new Date().getFullYear()}`;

const { container, getByText } = render(
<Calendar months={months} startOnPreviousMonth={false} direction="horizontal" />
);

expect(container).toContainElement(getByText(nextMonth));
});
});

describe('select dates', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
endOfMonth,
startOfWeek,
endOfWeek,
subMonths,
} from 'date-fns';

export function calcFocusDate(currentFocusedDate, props) {
Expand All @@ -26,7 +27,11 @@ export function calcFocusDate(currentFocusedDate, props) {
}
targetInterval.start = startOfMonth(targetInterval.start || new Date());
targetInterval.end = endOfMonth(targetInterval.end || targetInterval.start);
const targetDate = targetInterval.start || targetInterval.end || shownDate || new Date();
let targetDate = targetInterval.start || targetInterval.end || shownDate || new Date();

if (props.startOnPreviousMonth && props.months > 1) {
targetDate = subMonths(targetDate, 1);
}

// initial focus
if (!currentFocusedDate) return shownDate || targetDate;
Expand Down