Skip to content

Commit

Permalink
do not change time if date picker only (#2111)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-aladev committed Apr 14, 2020
1 parent efffb47 commit 8c0e03c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/index.jsx
Expand Up @@ -444,7 +444,7 @@ export default class DatePicker extends React.Component {
);
return this.preventFocusTimeout;
});
this.setSelected(date, event, undefined, monthSelectedIn);
this.setSelected(date, event, false, monthSelectedIn);
if (!this.props.shouldCloseOnSelect || this.props.showTimeSelect) {
this.setPreSelection(date);
} else if (!this.props.inline) {
Expand All @@ -461,13 +461,17 @@ export default class DatePicker extends React.Component {

if (!isEqual(this.props.selected, changedDate) || this.props.allowSameDay) {
if (changedDate !== null) {
if (this.props.selected) {
let selected = this.props.selected;
if (keepInput) selected = newDate(changedDate);
if (
this.props.selected &&
(!keepInput ||
(!this.props.showTimeSelect &&
!this.props.showTimeSelectOnly &&
!this.props.showTimeInput))
) {
changedDate = setTime(changedDate, {
hour: getHours(selected),
minute: getMinutes(selected),
second: getSeconds(selected)
hour: getHours(this.props.selected),
minute: getMinutes(this.props.selected),
second: getSeconds(this.props.selected)
});
}
if (!this.props.inline) {
Expand Down
24 changes: 23 additions & 1 deletion test/datepicker_test.js
Expand Up @@ -399,7 +399,7 @@ describe("DatePicker", () => {
expect(clearButtonText).to.equal("clear button");
});

it("should save time from the selected date", () => {
it("should save time from the selected date during day change", () => {
const selected = utils.newDate("2015-12-20 10:11:12");
let date;

Expand All @@ -423,6 +423,28 @@ describe("DatePicker", () => {
expect(utils.getSeconds(date)).to.equal(12);
});

it("should save time from the selected date during date change", () => {
const selected = utils.newDate("2015-12-20 10:11:12");
let date;

var datePicker = TestUtils.renderIntoDocument(
<DatePicker
selected={selected}
onChange={d => {
date = d;
}}
/>
);

var input = ReactDOM.findDOMNode(datePicker.input);
input.value = utils.newDate("2014-01-02");
TestUtils.Simulate.change(input);

expect(utils.getHours(date)).to.equal(10);
expect(utils.getMinutes(date)).to.equal(11);
expect(utils.getSeconds(date)).to.equal(12);
});

it("should mount and unmount properly", done => {
class TestComponent extends React.Component {
constructor(props) {
Expand Down

0 comments on commit 8c0e03c

Please sign in to comment.