-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathDatePicker.js
207 lines (176 loc) · 4.95 KB
/
DatePicker.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import React, { Component, PropTypes } from 'react';
import moment from 'moment-jalali';
import TetherComponent from 'react-tether';
import Calendar from './Calendar';
import classnames from 'classnames';
export const outsideClickIgnoreClass = 'ignore--click--outside';
export default class DatePicker extends Component {
static propTypes = {
value: PropTypes.object,
defaultValue: PropTypes.object,
onChange: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
children: PropTypes.node,
min: PropTypes.object,
max: PropTypes.object,
defaultMonth: PropTypes.object,
inputFormat: PropTypes.string,
removable: PropTypes.bool,
timePickerComponent: PropTypes.func,
calendarStyles: PropTypes.object,
calendarContainerProps: PropTypes.object,
selectedDays: PropTypes.array
};
static defaultProps = {
inputFormat: 'jYYYY/jM/jD',
calendarStyles: require('../styles/basic.css'),
calendarContainerProps: {},
selectedDays: []
};
state = {
isOpen: false,
momentValue: this.props.defaultValue || null,
inputValue: this.props.defaultValue ?
this.props.defaultValue.format(this.props.inputFormat) : ''
};
setOpen(isOpen) {
this.setState({ isOpen });
}
componentWillReceiveProps(nextProps) {
if ('value' in nextProps && nextProps.value !== this.props.value) {
this.setMomentValue(nextProps.value);
}
}
setMomentValue(momentValue) {
const { inputFormat } = this.props;
if (this.props.onChange) {
this.props.onChange(momentValue);
}
let inputValue = "";
if (momentValue)
inputValue = momentValue.format(inputFormat);
this.setState({ momentValue, inputValue });
}
handleFocus() {
this.setOpen(true);
}
handleBlur(event) {
const { onBlur, inputFormat } = this.props;
const { isOpen, momentValue } = this.state;
if (isOpen) {
this.refs.input.focus();
} else if (onBlur) {
onBlur(event);
}
if (momentValue) {
const inputValue = momentValue.format(inputFormat);
this.setState({ inputValue });
}
}
handleClickOutsideCalendar() {
this.setOpen(false);
}
handleSelectDay(selectedDay) {
const { momentValue: oldValue } = this.state;
let momentValue = selectedDay.clone();
if (oldValue) {
momentValue = momentValue
.set({
hour: oldValue.hours(),
minute: oldValue.minutes(),
second: oldValue.seconds()
});
}
this.setMomentValue(momentValue);
}
handleInputChange(event) {
const { inputFormat } = this.props;
const inputValue = event.target.value;
const momentValue = moment(inputValue, inputFormat);
if (momentValue.isValid()) {
this.setState({ momentValue });
}
this.setState({ inputValue });
}
handleInputClick() {
if (!this.props.disabled) {
this.setOpen(true);
}
}
renderInput() {
let { isOpen, inputValue } = this.state;
if (this.props.value) {
let value = this.props.value;
let inputFormat = this.props.inputFormat;
inputValue = value.format(inputFormat);
}
const className = classnames(this.props.className, {
[outsideClickIgnoreClass]: isOpen
});
return (
<div>
<input
className={className}
type="text"
ref="input"
onFocus={this.handleFocus.bind(this) }
onBlur={this.handleBlur.bind(this) }
onChange={this.handleInputChange.bind(this) }
onClick={this.handleInputClick.bind(this) }
value={inputValue}
/>
</div>
);
}
renderCalendar() {
const { momentValue } = this.state;
const { timePickerComponent: TimePicker, selectedDays, onChange, min, max, defaultMonth, calendarStyles, calendarContainerProps } = this.props;
return (
<div>
<Calendar
min={min}
max={max}
selectedDay={momentValue}
defaultMonth={defaultMonth}
onSelect={this.handleSelectDay.bind(this) }
onClickOutside={this.handleClickOutsideCalendar.bind(this) }
outsideClickIgnoreClass={outsideClickIgnoreClass}
styles={calendarStyles}
containerProps={calendarContainerProps}
selectedDays={selectedDays}
>
{
TimePicker ? (
<TimePicker
min={min}
max={max}
momentValue={momentValue}
setMomentValue={this.setMomentValue.bind(this) }
/>
) : null
}
</Calendar>
</div>
);
}
removeDate() {
const { onChange } = this.props;
if (onChange) {
onChange('');
}
this.setState({
input: '',
inputValue: ''
});
}
render() {
const { isOpen } = this.state;
return (
<TetherComponent attachment="top center">
{ this.renderInput() }
{ isOpen ? this.renderCalendar() : null }
</TetherComponent>
);
}
};