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

Update disable prev and next state on month and year change #2169

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
20 changes: 18 additions & 2 deletions src/components/DayPickerRangeController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,13 @@ export default class DayPickerRangeController extends React.PureComponent {
}

onMonthChange(newMonth) {
const { numberOfMonths, enableOutsideDays, orientation } = this.props;
const {
enableOutsideDays,
maxDate,
minDate,
numberOfMonths,
orientation,
} = this.props;
const withoutTransitionMonths = orientation === VERTICAL_SCROLLABLE;
const newVisibleDays = getVisibleDays(
newMonth,
Expand All @@ -983,11 +989,19 @@ export default class DayPickerRangeController extends React.PureComponent {
this.setState({
currentMonth: newMonth.clone(),
visibleDays: this.getModifiers(newVisibleDays),
disablePrev: this.shouldDisableMonthNavigation(minDate, newMonth),
disableNext: this.shouldDisableMonthNavigation(maxDate, newMonth),
});
}

onYearChange(newMonth) {
const { numberOfMonths, enableOutsideDays, orientation } = this.props;
const {
enableOutsideDays,
maxDate,
minDate,
numberOfMonths,
orientation,
} = this.props;
const withoutTransitionMonths = orientation === VERTICAL_SCROLLABLE;
const newVisibleDays = getVisibleDays(
newMonth,
Expand All @@ -999,6 +1013,8 @@ export default class DayPickerRangeController extends React.PureComponent {
this.setState({
currentMonth: newMonth.clone(),
visibleDays: this.getModifiers(newVisibleDays),
disablePrev: this.shouldDisableMonthNavigation(minDate, newMonth),
disableNext: this.shouldDisableMonthNavigation(maxDate, newMonth),
});
}

Expand Down
68 changes: 68 additions & 0 deletions test/components/DayPickerRangeController_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3693,6 +3693,74 @@ describe('DayPickerRangeController', () => {
});
});

describe('#onMonchChange', () => {
it('sets disableNext as false when maxDate is in visible month and a past month is selected', () => {
const wrapper = shallow((
<DayPickerRangeController
onDatesChange={sinon.stub()}
onFocusChange={sinon.stub()}
maxDate={today}
/>
));
wrapper.setState({
currentMonth: today,
});
expect(wrapper.state()).to.have.property('disableNext', true);
wrapper.instance().onMonthChange(today.clone().subtract(1, 'month'));
expect(wrapper.state()).to.have.property('disableNext', false);
});

it('sets disablePrev as false when minDate is in visible month and a future month is selected', () => {
const wrapper = shallow((
<DayPickerRangeController
onDatesChange={sinon.stub()}
onFocusChange={sinon.stub()}
minDate={today}
/>
));
wrapper.setState({
currentMonth: today,
});
expect(wrapper.state()).to.have.property('disablePrev', true);
wrapper.instance().onMonthChange(today.clone().add(1, 'month'));
expect(wrapper.state()).to.have.property('disablePrev', false);
});
});

describe('#onYearChange', () => {
it('sets disableNext as false when maxDate is in visible month and a past year is selected', () => {
const wrapper = shallow((
<DayPickerRangeController
onDatesChange={sinon.stub()}
onFocusChange={sinon.stub()}
maxDate={today}
/>
));
wrapper.setState({
currentMonth: today,
});
expect(wrapper.state()).to.have.property('disableNext', true);
wrapper.instance().onYearChange(today.clone().subtract(1, 'year'));
expect(wrapper.state()).to.have.property('disableNext', false);
});

it('sets disablePrev as false when minDate is in visible month and a future year is selected', () => {
const wrapper = shallow((
<DayPickerRangeController
onDatesChange={sinon.stub()}
onFocusChange={sinon.stub()}
minDate={today}
/>
));
wrapper.setState({
currentMonth: today,
});
expect(wrapper.state()).to.have.property('disablePrev', true);
wrapper.instance().onYearChange(today.clone().add(1, 'year'));
expect(wrapper.state()).to.have.property('disablePrev', false);
});
});

describe('#getFirstFocusableDay', () => {
describe('focusedInput === START_DATE', () => {
it('returns startDate if exists and is not blocked', () => {
Expand Down