Skip to content

Commit

Permalink
Merge pull request #2 from tom-mi/master
Browse files Browse the repository at this point in the history
Fix 'last day of prev month' selection
  • Loading branch information
rkipsch committed Oct 7, 2021
2 parents 5ae15b8 + f30a7ae commit 9d2cf0f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/DateTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,17 +372,17 @@ export default class Datetime extends React.Component {
let updateOnView = this.getUpdateOn( this.getFormat('date') );
let viewDate = this.state.viewDate.clone();

// Set the value into day/month/year
viewDate[ this.viewToMethod[currentView] ](
parseInt( e.target.getAttribute('data-value'), 10 )
);

// Need to set month and year will for days view (prev/next month)
// Need to set month and year for days view (prev/next month)
if ( currentView === 'days' ) {
viewDate.month( parseInt( e.target.getAttribute('data-month'), 10 ) );
viewDate.year( parseInt( e.target.getAttribute('data-year'), 10 ) );
}

// Set the value into day/month/year
viewDate[ this.viewToMethod[currentView] ](
parseInt( e.target.getAttribute('data-value'), 10 )
);

let update = {viewDate: viewDate};
if ( currentView === updateOnView ) {
update.selectedDate = viewDate.clone();
Expand Down
24 changes: 24 additions & 0 deletions test/tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ describe('Datetime', () => {
expect(component.find('.rdtSwitch').text()).toEqual('February 2019');
});

it('click on first day of the next month', () => {
const component = utils.createDatetime({
initialViewMode: 'days',
initialValue: new Date(2019, 0, 1)
});

utils.openDatepicker(component);
utils.clickClassItem(component, '.rdtNew', 0);

expect(component.find('.form-control').getDOMNode().value).toEqual('02/01/2019 12:00 AM');
});

it('click on day of the prev month', () => {
const component = utils.createDatetime({
initialViewMode: 'days',
Expand All @@ -248,6 +260,18 @@ describe('Datetime', () => {
expect(component.find('.rdtSwitch').text()).toEqual('December 2018');
});

it('click on last day of the prev month', () => {
const component = utils.createDatetime({
initialViewMode: 'days',
initialValue: new Date(2019, 0, 1)
});

utils.openDatepicker(component);
utils.clickClassItem(component, '.rdtOld', 1);

expect(component.find('.form-control').getDOMNode().value).toEqual('12/31/2018 12:00 AM');
});

it('sets CSS class on selected item (day)', () => {
const component = utils.createDatetime({ initialViewMode: 'days' });
utils.openDatepicker(component);
Expand Down

0 comments on commit 9d2cf0f

Please sign in to comment.