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

Unselection by passing falsy value in props #190

Merged
merged 1 commit into from
Apr 26, 2018
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
4 changes: 1 addition & 3 deletions src/DateRangePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ const DateRangePicker = createClass({
};

if (hasUpdatedValue(this.props, nextProps)) {
const isNewValueVisible = this.isStartOrEndVisible(nextProps);

if (!isNewValueVisible) {
if (!nextProps.value || !this.isStartOrEndVisible(nextProps)) {
const yearMonth = getYearMonthProps(nextProps);

updatedState.year = yearMonth.year;
Expand Down
23 changes: 20 additions & 3 deletions src/tests/DateRangePicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,17 +697,15 @@ describe('The DateRangePicker component', function () {

describe('#componentWillReceiveProps', function () {
describe('updating date states', function () {
beforeEach( function () {
it('updates state.dateStates if data provided in the props', function () {
this.useDocumentRenderer({
initialYear: 2000,
initialMonth: 6,
});
spyOn(this.renderedComponent, 'render').and.callFake(function () {
return <div />;
});
});

it('updates state.dateStates if data provided in the props', function () {
var newDateStates = ['newDateStates'];
spyOn(this.renderedComponent, 'getDateStates').and.returnValue(newDateStates);
spyOn(this.renderedComponent, 'getEnabledRange').and.returnValue('newEnabledRange');
Expand All @@ -717,6 +715,25 @@ describe('The DateRangePicker component', function () {
this.renderedComponent.componentWillReceiveProps({});
expect(this.renderedComponent.state.dateStates).toBe(newDateStates);
});

it('returns to initial view if data provided goes from a date range to a falsy value', function () {
var initialYear = 2000;
var initialMonth = 6;
var value = moment.range(moment(new Date('1982/07/03')), moment(new Date('1982/07/21')));
this.useDocumentRenderer({
initialYear,
initialMonth,
value,
});
spyOn(this.renderedComponent, 'render').and.callFake(function () {
return <div />;
});
const newProps = Object.assign({}, this.renderedComponent.props, { value: null });
this.renderedComponent.componentWillReceiveProps(newProps);

expect(this.renderedComponent.state.year).toBe(initialYear);
expect(this.renderedComponent.state.month).toBe(initialMonth);
});
});

describe('updating the value', function () {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getYearMonth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function getYearMonth(date) {
export const getYearMonthProps = function (props) {
const { selectionType, value } = props;
if (!value) {
return undefined;
return { year: props.initialYear, month: props.initialMonth };
}

if (selectionType === 'single') {
Expand Down