-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Expand file tree
/
Copy pathDatePickerDialog.js
More file actions
183 lines (163 loc) · 4.51 KB
/
DatePickerDialog.js
File metadata and controls
183 lines (163 loc) · 4.51 KB
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
import React, {Component, PropTypes} from 'react';
import EventListener from 'react-event-listener';
import keycode from 'keycode';
import Calendar from './Calendar';
import Dialog from '../Dialog';
import Popover from '../Popover/Popover';
import PopoverAnimationVertical from '../Popover/PopoverAnimationVertical';
import {dateTimeFormat} from './dateUtils';
class DatePickerDialog extends Component {
static propTypes = {
DateTimeFormat: PropTypes.func,
animation: PropTypes.func,
autoOk: PropTypes.bool,
cancelLabel: PropTypes.node,
container: PropTypes.oneOf(['dialog', 'inline']),
containerStyle: PropTypes.object,
disableYearSelection: PropTypes.bool,
firstDayOfWeek: PropTypes.number,
initialDate: PropTypes.object,
locale: PropTypes.string,
maxDate: PropTypes.object,
minDate: PropTypes.object,
mode: PropTypes.oneOf(['portrait', 'landscape']),
okLabel: PropTypes.node,
onAccept: PropTypes.func,
onDismiss: PropTypes.func,
onShow: PropTypes.func,
open: PropTypes.bool,
shouldDisableDate: PropTypes.func,
style: PropTypes.object,
};
static defaultProps = {
DateTimeFormat: dateTimeFormat,
cancelLabel: 'Cancel',
container: 'dialog',
locale: 'en-US',
okLabel: 'OK',
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
state = {
open: false,
};
show = () => {
if (this.props.onShow && !this.state.open) {
this.props.onShow();
}
this.setState({
open: true,
});
};
dismiss = () => {
if (this.props.onDismiss && this.state.open) {
this.props.onDismiss();
}
this.setState({
open: false,
});
};
handleTouchTapDay = () => {
if (this.props.autoOk) {
setTimeout(this.handleTouchTapOk, 300);
}
};
handleTouchTapCancel = () => {
this.dismiss();
};
handleRequestClose = () => {
this.dismiss();
};
handleTouchTapOk = () => {
if (this.props.onAccept && !this.refs.calendar.isSelectedDateDisabled()) {
this.props.onAccept(this.refs.calendar.getSelectedDate());
}
this.setState({
open: false,
});
};
handleWindowKeyUp = (event) => {
switch (keycode(event)) {
case 'enter':
this.handleTouchTapOk();
break;
}
};
render() {
const {
DateTimeFormat,
autoOk,
cancelLabel,
container,
containerStyle,
disableYearSelection,
initialDate,
firstDayOfWeek,
locale,
maxDate,
minDate,
mode,
okLabel,
onAccept, // eslint-disable-line no-unused-vars
onDismiss, // eslint-disable-line no-unused-vars
onShow, // eslint-disable-line no-unused-vars
shouldDisableDate,
style, // eslint-disable-line no-unused-vars
animation,
...other,
} = this.props;
const {open} = this.state;
const styles = {
dialogContent: {
width: mode === 'landscape' ? 479 : 310,
},
dialogBodyContent: {
padding: 0,
minHeight: mode === 'landscape' ? 330 : 434,
minWidth: mode === 'landscape' ? 479 : 310,
},
};
const Container = (container === 'inline' ? Popover : Dialog);
return (
<div {...other} ref="root">
<Container
anchorEl={this.refs.root} // For Popover
animation={animation || PopoverAnimationVertical} // For Popover
bodyStyle={styles.dialogBodyContent}
contentStyle={styles.dialogContent}
ref="dialog"
repositionOnUpdate={true}
open={open}
onRequestClose={this.handleRequestClose}
style={Object.assign(styles.dialogBodyContent, containerStyle)}
>
<EventListener
target="window"
onKeyUp={this.handleWindowKeyUp}
/>
<Calendar
autoOk={autoOk}
DateTimeFormat={DateTimeFormat}
cancelLabel={cancelLabel}
disableYearSelection={disableYearSelection}
firstDayOfWeek={firstDayOfWeek}
initialDate={initialDate}
locale={locale}
onTouchTapDay={this.handleTouchTapDay}
maxDate={maxDate}
minDate={minDate}
mode={mode}
open={open}
ref="calendar"
onTouchTapCancel={this.handleTouchTapCancel}
onTouchTapOk={this.handleTouchTapOk}
okLabel={okLabel}
shouldDisableDate={shouldDisableDate}
/>
</Container>
</div>
);
}
}
export default DatePickerDialog;