Skip to content

Commit 07e137d

Browse files
committed
Add support for onCalendarOpen and onCalendarClose
Fixes #140
1 parent 9bc4d92 commit 07e137d

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Displays an input field complete with custom inputs, native input, and a calenda
106106
|minDate|Defines minimum date that the user can select. Periods partially overlapped by minDate will also be selectable, although React-Date-Picker will ensure that no earlier date is selected.|Date: `new Date()`|
107107
|minDetail|Defines the least detailed calendar view that the user shall see. Can be "month", "year", "decade" or "century". Defaults to "century".|`"century"`|
108108
|name|Defines input name. Defaults to "date".|`"myCustomName"`|
109+
|onCalendarClose|Function called when the calendar closes.|`() => alert('Calendar closed')`|
110+
|onCalendarOpen|Function called when the calendar opens.|`() => alert('Calendar opened')`|
109111
|onChange|Function called when the user clicks an item on the most detailed view available.|`(value) => alert('New date is: ', value)`|
110112
|returnValue|Defines which dates shall be passed by the calendar to the onChange function and onClick{Period} functions. Can be "start", "end" or "range". The latter will cause an array with start and end values to be passed. Defaults to "start".|`"range"`|
111113
|required|Defines whether date input should be required. Defaults to false.|`true`|

src/DatePicker.jsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Calendar from 'react-calendar/dist/entry.nostyle';
99

1010
import DateInput from './DateInput';
1111

12+
import { callIfDefined } from './shared/utils';
13+
1214
const baseClassName = 'react-date-picker';
1315

1416
export default class DatePicker extends PureComponent {
@@ -34,6 +36,15 @@ export default class DatePicker extends PureComponent {
3436
document.addEventListener('focusin', this.onOutsideAction);
3537
}
3638

39+
componentDidUpdate(prevProps, prevState) {
40+
const { isOpen } = this.state;
41+
const { onCalendarClose, onCalendarOpen } = this.props;
42+
43+
if (isOpen !== prevState.isOpen) {
44+
callIfDefined(isOpen ? onCalendarOpen : onCalendarClose);
45+
}
46+
}
47+
3748
componentWillUnmount() {
3849
document.removeEventListener('mousedown', this.onOutsideAction);
3950
document.removeEventListener('focusin', this.onOutsideAction);

src/shared/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export {
22
between,
3+
callIfDefined,
34
} from 'react-calendar/dist/shared/utils';
45

56
const isValidNumber = a => typeof a === 'number' && !isNaN(a);

test/Test.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import './Test.less';
1515

1616
const now = new Date();
1717

18+
/* eslint-disable no-console */
19+
1820
export default class Test extends PureComponent {
1921
state = {
2022
disabled: false,
@@ -97,10 +99,8 @@ export default class Test extends PureComponent {
9799
onSubmit={(event) => {
98100
event.preventDefault();
99101

100-
/* eslint-disable no-console */
101102
console.warn('Calendar triggered submitting the form.');
102103
console.log(event);
103-
/* eslint-enable no-console */
104104
}}
105105
>
106106
<DatePicker
@@ -114,6 +114,8 @@ export default class Test extends PureComponent {
114114
minDetail={minDetail}
115115
name="myCustomName"
116116
onChange={this.onChange}
117+
onCalendarOpen={() => console.log('Calendar opened')}
118+
onCalendarClose={() => console.log('Calendar closed')}
117119
returnValue={returnValue}
118120
required={required}
119121
showLeadingZeros={showLeadingZeros}

0 commit comments

Comments
 (0)