Skip to content

Commit

Permalink
Fix defaultValue, defaultActiveStartDate and defaultView ignored when…
Browse files Browse the repository at this point in the history
… calculating initial active start date
  • Loading branch information
wojtekmaj committed Dec 8, 2019
1 parent 80ace80 commit 0922d37
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ const getDetailValueArray = (value, minDate, maxDate, maxDetail) => {
const getActiveStartDate = (props) => {
const {
activeStartDate,
defaultActiveStartDate,
defaultValue,
defaultView,
maxDate,
maxDetail,
minDate,
Expand All @@ -138,10 +141,10 @@ const getActiveStartDate = (props) => {
view,
} = props;

const rangeType = getView(view, minDetail, maxDetail);
const rangeType = getView(view || defaultView, minDetail, maxDetail);
const valueFrom = (
activeStartDate,
getDetailValueFrom(value, minDate, maxDate, maxDetail)
activeStartDate || defaultActiveStartDate
|| getDetailValueFrom(value || defaultValue, minDate, maxDate, maxDetail)
|| new Date()
);
return getBegin(rangeType, valueFrom);
Expand All @@ -152,7 +155,7 @@ const isSingleValue = value => value && [].concat(value).length === 1;
export default class Calendar extends Component {
state = {
/* eslint-disable react/destructuring-assignment */
activeStartDate: this.props.defaultActiveStartDate || getActiveStartDate(this.props),
activeStartDate: getActiveStartDate(this.props),
view: this.props.defaultView,
value: this.props.defaultValue,
/* eslint-enable react/destructuring-assignment */
Expand Down
47 changes: 44 additions & 3 deletions src/Calendar.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,22 @@ describe('Calendar', () => {
expect(monthView.prop('showNeighboringMonth')).toBeTruthy();
});

it('displays a view with a given value when defaultValue is given', () => {
const defaultValue = new Date(2017, 0, 15);
const component = shallow(
<Calendar
defaultValue={defaultValue}
/>,
);

const monthView = component.find('MonthView');

expect(monthView.prop('activeStartDate')).toEqual(new Date(2017, 0, 1));
});

it('displays a view with a given value when value is given', () => {
const value = new Date(2017, 0, 15);
const component = mount(
const component = shallow(
<Calendar
value={value}
/>,
Expand All @@ -183,9 +196,37 @@ describe('Calendar', () => {
expect(monthView.prop('activeStartDate')).toEqual(new Date(2017, 0, 1));
});

it('displays a view with defaultActiveStartDate when value is given and defaultActiveStartDate is given', () => {
const defaultActiveStartDate = new Date(2017, 0, 1);
const value = new Date(2018, 0, 15);
const component = shallow(
<Calendar
defaultActiveStartDate={defaultActiveStartDate}
value={value}
/>,
);

const monthView = component.find('MonthView');

expect(monthView.prop('activeStartDate')).toEqual(defaultActiveStartDate);
});

it('displays a view with defaultActiveStartDate when no value is given and defaultActiveStartDate is given', () => {
const defaultActiveStartDate = new Date(2017, 0, 1);
const component = shallow(
<Calendar
defaultActiveStartDate={defaultActiveStartDate}
/>,
);

const monthView = component.find('MonthView');

expect(monthView.prop('activeStartDate')).toEqual(defaultActiveStartDate);
});

it('displays a view with activeStartDate when no value is given and activeStartDate is given', () => {
const activeStartDate = new Date(2017, 0, 1);
const component = mount(
const component = shallow(
<Calendar
activeStartDate={activeStartDate}
/>,
Expand All @@ -199,7 +240,7 @@ describe('Calendar', () => {
it('displays a view with today\'s date when no value and no activeStartDate is given', () => {
const today = new Date();
const beginOfCurrentMonth = getMonthStart(today);
const component = mount(
const component = shallow(
<Calendar />,
);

Expand Down

0 comments on commit 0922d37

Please sign in to comment.