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

Reset after-hovered-start in componentWillReceiveProps - Fixes #831 #843

Merged
merged 2 commits into from
Nov 27, 2017
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
61 changes: 39 additions & 22 deletions src/components/DayPickerRangeController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,38 @@ export default class DayPickerRangeController extends React.Component {
numberOfMonths,
enableOutsideDays,
} = nextProps;

const {
startDate: prevStartDate,
endDate: prevEndDate,
focusedInput: prevFocusedInput,
minimumNights: prevMinimumNights,
isOutsideRange: prevIsOutsideRange,
isDayBlocked: prevIsDayBlocked,
isDayHighlighted: prevIsDayHighlighted,
phrases: prevPhrases,
initialVisibleMonth: prevInitialVisibleMonth,
numberOfMonths: prevNumberOfMonths,
enableOutsideDays: prevEnableOutsideDays,
} = this.props;

let { visibleDays } = this.state;

let recomputeOutsideRange = false;
let recomputeDayBlocked = false;
let recomputeDayHighlighted = false;

if (isOutsideRange !== this.props.isOutsideRange) {
if (isOutsideRange !== prevIsOutsideRange) {
this.modifiers['blocked-out-of-range'] = day => isOutsideRange(day);
recomputeOutsideRange = true;
}

if (isDayBlocked !== this.props.isDayBlocked) {
if (isDayBlocked !== prevIsDayBlocked) {
this.modifiers['blocked-calendar'] = day => isDayBlocked(day);
recomputeDayBlocked = true;
}

if (isDayHighlighted !== this.props.isDayHighlighted) {
if (isDayHighlighted !== prevIsDayHighlighted) {
this.modifiers['highlighted-calendar'] = day => isDayHighlighted(day);
recomputeDayHighlighted = true;
}
Expand All @@ -231,16 +246,16 @@ export default class DayPickerRangeController extends React.Component {
recomputeOutsideRange || recomputeDayBlocked || recomputeDayHighlighted
);

const didStartDateChange = startDate !== this.props.startDate;
const didEndDateChange = endDate !== this.props.endDate;
const didFocusChange = focusedInput !== this.props.focusedInput;
const didStartDateChange = startDate !== prevStartDate;
const didEndDateChange = endDate !== prevEndDate;
const didFocusChange = focusedInput !== prevFocusedInput;

if (
numberOfMonths !== this.props.numberOfMonths ||
enableOutsideDays !== this.props.enableOutsideDays ||
numberOfMonths !== prevNumberOfMonths ||
enableOutsideDays !== prevEnableOutsideDays ||
(
initialVisibleMonth !== this.props.initialVisibleMonth &&
!this.props.focusedInput &&
initialVisibleMonth !== prevInitialVisibleMonth &&
!prevFocusedInput &&
didFocusChange
)
) {
Expand All @@ -256,21 +271,27 @@ export default class DayPickerRangeController extends React.Component {
let modifiers = {};

if (didStartDateChange) {
modifiers = this.deleteModifier(modifiers, this.props.startDate, 'selected-start');
modifiers = this.deleteModifier(modifiers, prevStartDate, 'selected-start');
modifiers = this.addModifier(modifiers, startDate, 'selected-start');

if (prevStartDate) {
const startSpan = prevStartDate.clone().add(1, 'day');
const endSpan = prevStartDate.clone().add(prevMinimumNights + 1, 'days');
modifiers = this.deleteModifierFromRange(modifiers, startSpan, endSpan, 'after-hovered-start');
}
}

if (didEndDateChange) {
modifiers = this.deleteModifier(modifiers, this.props.endDate, 'selected-end');
modifiers = this.deleteModifier(modifiers, prevEndDate, 'selected-end');
modifiers = this.addModifier(modifiers, endDate, 'selected-end');
}

if (didStartDateChange || didEndDateChange) {
if (this.props.startDate && this.props.endDate) {
if (prevStartDate && prevEndDate) {
modifiers = this.deleteModifierFromRange(
modifiers,
this.props.startDate,
this.props.endDate.clone().add(1, 'day'),
prevStartDate,
prevEndDate.clone().add(1, 'day'),
'selected-span',
);
}
Expand Down Expand Up @@ -298,9 +319,9 @@ export default class DayPickerRangeController extends React.Component {
modifiers = this.addModifierToRange(modifiers, startSpan, endSpan, 'after-hovered-start');
}

if (minimumNights > 0 || minimumNights !== this.props.minimumNights) {
if (minimumNights > 0 || minimumNights !== prevMinimumNights) {
if (didFocusChange || didStartDateChange) {
const startSpan = this.props.startDate ? this.props.startDate : this.today;
const startSpan = prevStartDate || this.today;
modifiers = this.deleteModifierFromRange(
modifiers,
startSpan,
Expand Down Expand Up @@ -373,7 +394,7 @@ export default class DayPickerRangeController extends React.Component {
});
}

if (didFocusChange || phrases !== this.props.phrases) {
if (didFocusChange || phrases !== prevPhrases) {
// set the appropriate CalendarDay phrase based on focusedInput
const chooseAvailableDate = getChooseAvailableDatePhrase(phrases, focusedInput);

Expand Down Expand Up @@ -462,10 +483,6 @@ export default class DayPickerRangeController extends React.Component {
}

if (startDate) {
const startSpan = startDate.clone().add(1, 'day');
const endSpan = startDate.clone().add(minimumNights + 1, 'days');
modifiers = this.deleteModifierFromRange(modifiers, startSpan, endSpan, 'after-hovered-start');

if (isSameDay(day, startDate)) {
const newStartSpan = startDate.clone().add(1, 'day');
const newEndSpan = startDate.clone().add(minimumNights + 1, 'days');
Expand Down
51 changes: 28 additions & 23 deletions test/components/DayPickerRangeController_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,34 @@ describe('DayPickerRangeController', () => {
});
expect(wrapper.instance().state.visibleDays).to.equal(visibleDays);
});
describe('startDate changed from one date to another', () => {
it('removes previous `after-hovered-start` range', () => {
const minimumNights = 5;
const startDate = moment().add(7, 'days');
const dayAfterStartDate = startDate.clone().add(1, 'day');
const firstAvailableDate = startDate.clone().add(minimumNights + 1, 'days');
const deleteModifierFromRangeSpy = sinon.spy(DayPickerRangeController.prototype, 'deleteModifierFromRange');
const nextStartDate = moment().add(4, 'days');
const wrapper = shallow((
<DayPickerRangeController
onDatesChange={sinon.stub()}
onFocusChange={sinon.stub()}
startDate={startDate}
focusedInput={START_DATE}
minimumNights={minimumNights}
/>
));
deleteModifierFromRangeSpy.reset();
wrapper.instance().componentWillReceiveProps({
...props,
startDate: nextStartDate,
});
const afterHoverStartCalls = getCallsByModifier(deleteModifierFromRangeSpy, 'after-hovered-start');
expect(afterHoverStartCalls.length).to.equal(1);
expect(isSameDay(afterHoverStartCalls[0].args[1], dayAfterStartDate)).to.equal(true);
expect(isSameDay(afterHoverStartCalls[0].args[2], firstAvailableDate)).to.equal(true);
});
});
});
});

Expand Down Expand Up @@ -1615,29 +1643,6 @@ describe('DayPickerRangeController', () => {
});

describe('startDate exists', () => {
it('removes previous `after-hovered-start` range', () => {
const minimumNights = 5;
const startDate = moment().add(7, 'days');
const dayAfterStartDate = startDate.clone().add(1, 'day');
const firstAvailableDate = startDate.clone().add(minimumNights + 1, 'days');
const deleteModifierFromRangeSpy = sinon.spy(DayPickerRangeController.prototype, 'deleteModifierFromRange');
const wrapper = shallow((
<DayPickerRangeController
onDatesChange={sinon.stub()}
onFocusChange={sinon.stub()}
startDate={startDate}
focusedInput={START_DATE}
minimumNights={minimumNights}
/>
));
deleteModifierFromRangeSpy.reset();
wrapper.instance().onDayMouseEnter(today);
const afterHoverStartCalls = getCallsByModifier(deleteModifierFromRangeSpy, 'after-hovered-start');
expect(afterHoverStartCalls.length).to.equal(1);
expect(isSameDay(afterHoverStartCalls[0].args[1], dayAfterStartDate)).to.equal(true);
expect(isSameDay(afterHoverStartCalls[0].args[2], firstAvailableDate)).to.equal(true);
});

describe('hoverDate is startDate', () => {
it('adds new `after-hovered-start` range', () => {
const minimumNights = 5;
Expand Down