Skip to content

Commit 251edcf

Browse files
authored
Merge pull request Hacker0x01#1346 from webdeb/bk-fix-year-pre-selection-bug-1345
Bk fix year pre selection bug, fixes Hacker0x01#1345
2 parents ceb3b80 + 326a18d commit 251edcf

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

docs-site/src/examples/inline.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export default class Inline extends React.Component {
3535
<DatePicker
3636
inline
3737
selected={this.state.startDate}
38-
onChange={this.handleChange}/>
38+
onChange={this.handleChange}
39+
/>
3940
</div>
4041
</div>
4142
);

src/index.jsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
getSecond,
1616
getMinute,
1717
getHour,
18-
getMonth,
1918
addDays,
2019
addMonths,
2120
addWeeks,
@@ -31,13 +30,26 @@ import {
3130
getEffectiveMaxDate,
3231
parseDate,
3332
safeDateFormat,
34-
getHightLightDaysMap
33+
getHightLightDaysMap,
34+
getYear,
35+
getMonth
3536
} from "./date_utils";
3637
import onClickOutside from "react-onclickoutside";
3738

3839
const outsideClickIgnoreClass = "react-datepicker-ignore-onclickoutside";
3940
const WrappedCalendar = onClickOutside(Calendar);
4041

42+
// Compares dates year+month combinations
43+
function hasPreSelectionChanged(date1, date2) {
44+
if (date1 && date2) {
45+
return (
46+
getMonth(date1) !== getMonth(date2) || getYear(date1) !== getYear(date2)
47+
);
48+
}
49+
50+
return date1 !== date2;
51+
}
52+
4153
/**
4254
* General datepicker component.
4355
*/
@@ -164,9 +176,10 @@ export default class DatePicker extends React.Component {
164176
}
165177

166178
componentWillReceiveProps(nextProps) {
167-
const currentMonth = this.props.selected && getMonth(this.props.selected);
168-
const nextMonth = nextProps.selected && getMonth(nextProps.selected);
169-
if (this.props.inline && currentMonth !== nextMonth) {
179+
if (
180+
this.props.inline &&
181+
hasPreSelectionChanged(this.props.selected, nextProps.selected)
182+
) {
170183
this.setPreSelection(nextProps.selected);
171184
}
172185
if (this.props.highlightDates !== nextProps.highlightDates) {

test/datepicker_test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,18 @@ describe("DatePicker", () => {
930930
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
931931
).to.equal(utils.formatDate(future, "YYYY-MM-DD"));
932932
});
933+
it("should switch month in inline mode immediately, when year is updated", () => {
934+
const selected = utils.newDate();
935+
const future = utils.addYears(utils.newDate(), 1);
936+
const datePicker = mount(<DatePicker inline selected={selected} />);
937+
expect(
938+
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
939+
).to.equal(utils.formatDate(selected, "YYYY-MM-DD"));
940+
datePicker.setProps({ selected: future });
941+
expect(
942+
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
943+
).to.equal(utils.formatDate(future, "YYYY-MM-DD"));
944+
});
933945
it("should not set open state when focusing on the date input and the preventOpenOnFocus prop is set", () => {
934946
const datePicker = TestUtils.renderIntoDocument(
935947
<DatePicker preventOpenOnFocus />

0 commit comments

Comments
 (0)