Skip to content

Commit

Permalink
Merge pull request gpbl#612 from riipen/master
Browse files Browse the repository at this point in the history
Use dayPickerProps controlled month before value.
  • Loading branch information
gpbl authored Mar 5, 2018
2 parents 8f1be54 + 6b42434 commit ba73776
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
13 changes: 6 additions & 7 deletions src/DayPickerInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default class DayPickerInput extends React.Component {
getStateFromProps(props) {
const { dayPickerProps, formatDate, format } = props;
let { value } = props;
let month;

let day;
if (props.value) {
if (isDate(props.value)) {
Expand All @@ -181,13 +181,12 @@ export default class DayPickerInput extends React.Component {
} else {
day = props.parseDate(props.value, format, dayPickerProps.locale);
}
if (day) {
month = day;
}
} else {
// Otherwise display the month coming from `dayPickerProps` or the current month
month = dayPickerProps.initialMonth || dayPickerProps.month || new Date();
}

// Use DayPicker's controlled month. Then try the current `value`. Finally default to today.
const month =
dayPickerProps.initialMonth || dayPickerProps.month || day || new Date();

return {
value,
month,
Expand Down
4 changes: 2 additions & 2 deletions test/daypickerinput/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ describe('DayPickerInput', () => {
value="2017-11-8"
clickUnselectsDay
dayPickerProps={{
month: new Date(2017, 1),
month: new Date(2017, 10),
selectedDays: new Date(2017, 10, 8),
}}
/>
Expand All @@ -286,7 +286,7 @@ describe('DayPickerInput', () => {
value="2017-11-8"
clickUnselectsDay
dayPickerProps={{
month: new Date(2017, 1),
month: new Date(2017, 10),
selectedDays: [new Date(2017, 10, 8), new Date(2017, 10, 7)],
}}
/>
Expand Down
4 changes: 2 additions & 2 deletions test/daypickerinput/rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('DayPickerInput', () => {
it("should update the displayed month when `dayPickerProps.month`'s month is updated", () => {
const wrapper = mount(
<DayPickerInput
dayPickerProps={{ month: new Date(2017, 9) }}
dayPickerProps={{ month: new Date(2017, 11) }}
value="2017-12-15"
/>
);
Expand All @@ -163,7 +163,7 @@ describe('DayPickerInput', () => {
it("should update the displayed month when `dayPickerProps.month`'s year is updated", () => {
const wrapper = mount(
<DayPickerInput
dayPickerProps={{ month: new Date(2018, 10) }}
dayPickerProps={{ month: new Date(2017, 11) }}
value="2017-12-15"
/>
);
Expand Down

0 comments on commit ba73776

Please sign in to comment.