Skip to content

Commit 07c2931

Browse files
fix: set currentDateDisplayed on date click (#343)
1 parent 0164952 commit 07c2931

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/Calendar/Calendar.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ export class Calendar extends Component {
113113
let date = this.state.currentDateDisplayed;
114114
let months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
115115
date.setMonth(months.indexOf(month));
116+
// reset date to first of month
117+
date.setDate(1);
116118

117119
//updates month state
118120
if (!this.props.enableRangeSelection) {
@@ -270,6 +272,7 @@ export class Calendar extends Component {
270272
}
271273

272274
this.setState({
275+
currentDateDisplayed: day,
273276
selectedDate: day,
274277
arrSelectedDates: selectedDates,
275278
dateClick: true
@@ -565,7 +568,7 @@ Calendar.propTypes = {
565568
};
566569

567570
Calendar.defaultProps = {
568-
onChange: () => {}
571+
onChange: () => { }
569572
};
570573

571574
Calendar.propDescriptions = {

src/Calendar/Calendar.test.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ describe('<Calendar />', () => {
9696
const currentDateDisplayed = wrapper.state('currentDateDisplayed');
9797

9898
expect(currentDateDisplayed.getMonth()).toEqual(3);
99+
100+
// check that first of month is selected
101+
expect(currentDateDisplayed.getDate()).toEqual(1);
99102
});
100103

101104
test('click month from list with date range', () => {
@@ -320,20 +323,10 @@ describe('<Calendar />', () => {
320323
.at(0)
321324
.simulate('click');
322325

323-
const currentDateDisplayed = new Date(wrapper.state('selectedDate'));
324-
325-
// select 2nd day of month
326-
wrapper
327-
.find(
328-
'table.fd-calendar__table tbody.fd-calendar__group tr.fd-calendar__row td.fd-calendar__item:not(.fd-calendar__item--other-month)'
329-
)
330-
.at(1)
331-
.simulate('click');
332-
333-
const newDateDisplayed = wrapper.state('selectedDate');
334-
currentDateDisplayed.setDate(currentDateDisplayed.getDate() + 1);
326+
let selectedDate = new Date(wrapper.state('selectedDate'));
327+
let currentDateDisplayed = new Date(wrapper.state('currentDateDisplayed'));
335328

336-
expect(newDateDisplayed.getDate()).toEqual(currentDateDisplayed.getDate());
329+
expect(selectedDate.getDate()).toEqual(currentDateDisplayed.getDate());
337330
});
338331

339332
test('click on day with range enabled', () => {

0 commit comments

Comments
 (0)