forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatepicker.jsx
175 lines (154 loc) · 4.68 KB
/
datepicker.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var React = require( "react" );
var Popover = require( "./popover" );
var DateUtil = require( "./util/date" );
var Calendar = require( "./calendar" );
var DateInput = require( "./date_input" );
var moment = require( "moment" );
var isEqual = require( "lodash/lang/isEqual" );
var DatePicker = React.createClass( {
propTypes: {
weekdays: React.PropTypes.arrayOf( React.PropTypes.string ),
locale: React.PropTypes.string,
dateFormatCalendar: React.PropTypes.string,
popoverAttachment: React.PropTypes.string,
popoverTargetAttachment: React.PropTypes.string,
popoverTargetOffset: React.PropTypes.string,
weekStart: React.PropTypes.string,
onChange: React.PropTypes.func.isRequired,
onBlur: React.PropTypes.func
},
getDefaultProps: function() {
return {
weekdays: [ "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" ],
locale: "en",
dateFormatCalendar: "MMMM YYYY",
moment: moment,
onChange: function() {},
disabled: false
};
},
getInitialState: function() {
return {
focus: false,
virtualFocus: false,
selected: this.props.selected
};
},
componentWillReceiveProps: function( nextProps ) {
this.setState( {
selected: nextProps.selected
} );
},
shouldComponentUpdate: function( nextProps, nextState ) {
return !( isEqual( nextProps, this.props ) && isEqual( nextState, this.state ) );
},
getValue: function() {
return this.state.selected;
},
handleFocus: function() {
this.setState( {
focus: true
} );
},
handleBlur: function() {
this.setState( { virtualFocus: false }, function() {
setTimeout( function() {
if ( !this.state.virtualFocus && typeof this.props.onBlur === "function" ) {
this.props.onBlur( this.state.selected );
this.hideCalendar();
}
}.bind( this ), 200 );
}.bind( this ) );
},
hideCalendar: function() {
setTimeout( function() {
this.setState( {
focus: false
} );
}.bind( this ), 0 );
},
handleSelect: function( date ) {
this.setSelected( date );
setTimeout( function() {
this.hideCalendar();
}.bind( this ), 200 );
},
setSelected: function( date ) {
this.setState( {
selected: date.moment(),
virtualFocus: true
}, function() {
this.props.onChange( this.state.selected );
}.bind( this ) );
},
clearSelected: function() {
if ( this.state.selected === null ) return; //Due to issues with IE onchange events sometimes this gets noisy, so skip if we've already cleared
this.setState( {
selected: null
}, function() {
this.props.onChange( null );
}.bind( this ) );
},
onInputClick: function() {
this.setState( {
focus: true,
virtualFocus: true
} );
},
calendar: function() {
if ( this.state.focus ) {
return (
<Popover
attachment={this.props.popoverAttachment}
targetAttachment={this.props.popoverTargetAttachment}
targetOffset={this.props.popoverTargetOffset}>
<Calendar
weekdays={this.props.weekdays}
locale={this.props.locale}
moment={this.props.moment}
dateFormat={this.props.dateFormatCalendar}
selected={this.state.selected}
onSelect={this.handleSelect}
hideCalendar={this.hideCalendar}
minDate={this.props.minDate}
maxDate={this.props.maxDate}
excludeDates={this.props.excludeDates}
weekStart={this.props.weekStart} />
</Popover>
);
}
},
render: function() {
var clearButton = null;
if ( this.props.isClearable && this.state.selected != null ) {
clearButton = (
<button className="close-icon" onClick={this.clearSelected}></button>
);
}
return (
<div className="datepicker__input-container">
<DateInput
name={this.props.name}
date={this.state.selected}
dateFormat={this.props.dateFormat}
focus={this.state.focus}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
handleClick={this.onInputClick}
handleEnter={this.hideCalendar}
setSelected={this.setSelected}
clearSelected={this.clearSelected}
hideCalendar={this.hideCalendar}
placeholderText={this.props.placeholderText}
disabled={this.props.disabled}
className={this.props.className}
title={this.props.title}
readOnly={this.props.readOnly}
required={this.props.required} />
{clearButton}
{this.props.disabled ? null : this.calendar()}
</div>
);
}
} );
module.exports = DatePicker;