| 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332 |
27x
27x
8x
8x
8x
19x
18x
18x
18x
9x
9x
9x
9x
18x
1x
1x
1x
7x
7x
7x
7x
7x
7x
7x
7x
3x
2x
1x
4x
3x
2x
1x
1x
4x
2x
2x
2x
4x
37x
37x
37x
37x
37x
37x
37x
37x
8x
37x
46x
1x
1x
46x
2x
2x
45x
1x
1x
37x
89x
89x
89x
| /**
* @category controls
* @component time-picker
* @variations collab-ui-react
*/
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import moment from 'moment';
import { uniqueId } from 'lodash';
import TimePickerDropdown from '@collab-ui/react/TimePicker/TimePickerDropdown';
import TimeSelector from '@collab-ui/react/TimePicker/TimeSelector';
import {
Input,
EventOverlay,
} from '@collab-ui/react';
class TimePicker extends React.Component {
state = {
inputId: this.props.inputId || uniqueId('cui-timepicker__input-'),
isOpen: false,
selectedTime: moment(this.props.selectedTime),
activeIndex: null,
anchorNode: null
};
componentDidUpdate = (prevProps, prevState) => {
const hourEntry = this.hour;
if (!prevState.isOpen && this.state.isOpen) {
this.focusOnNode(hourEntry);
}
};
// Jest does not handle DOM.focus(), this allows Jest to mock this function
focusOnNode = node => {
node.focus();
};
hidePopover = () => {
this.setState({
isOpen: false
});
};
onFocus = () => {
this.setState({
isOpen: true,
anchorNode: ReactDOM.findDOMNode(this.clickTextField).parentNode
});
};
onMouseDown = () => {
this.setState({
isOpen: !this.state.isOpen,
anchorNode: ReactDOM.findDOMNode(this.clickTextField).parentNode
});
};
triggerOnChange = dayChange => {
this.props.onChange &&
this.props.onChange(
this.state.selectedTime.hour(),
this.state.selectedTime.minute(),
dayChange
);
};
changeTime = (unit, change) => {
const newTime = moment(this.state.selectedTime).add(change, unit);
let dayChange = 0;
if (change >= 0) {
Iif (
newTime
.clone()
.startOf('day')
.isAfter(moment().startOf('day'))
) {
newTime.add(-1, 'day');
dayChange = 1;
}
} else {
Eif (
newTime
.clone()
.startOf('day')
.isBefore(moment().startOf('day'))
) {
newTime.add(1, 'day');
dayChange = -1;
}
}
this.setState({ selectedTime: newTime }, () => this.triggerOnChange(dayChange));
};
setTime = (hour, minute, pre) => {
const meridianHour =
pre === 'PM' && parseInt(hour) < 12
? parseInt(hour) + 12
: pre === 'AM' && parseInt(hour) === 12 ? 0 : hour;
this.setState({
selectedTime: this.state.selectedTime
.clone()
.hour(meridianHour)
.minute(minute)
}, () => this.triggerOnChange(0));
};
onSelectKeyDown = (unit, e) => {
e.preventDefault();
const { minuteInterval, militaryTime } = this.props;
const hour =
!this.hour.value && unit === 'h'
? militaryTime ? 0 : 1
: this.hour.value;
const minute = !this.minute.value && unit === 'm' ? 0 : this.minute.value;
const pre = !militaryTime && this.pre.value;
Iif (e.keyCode === 65 && unit === 'pre') {
if (this.pre.value.includes('A')) return;
return this.changeTime('h', 12);
} else Iif (e.keyCode === 80 && unit === 'pre') {
if (this.pre.value.includes('P')) return;
return this.changeTime('h', 12);
} else if (e.keyCode === 38) {
if (unit === 'pre') return this.changeTime('h', 12);
if (unit === 'h') return this.changeTime('h', 1);
return this.changeTime(unit, minuteInterval);
} else if (e.keyCode === 40) {
if (unit === 'pre') return this.changeTime('h', -12);
if (unit === 'h') return this.changeTime('h', -1);
return this.changeTime(unit, -minuteInterval);
} else {
this.setTime(hour, minute, pre);
}
};
onSelectWheel = (unit, e) => {
if (e.deltaY < 0) {
this.changeTime(unit, 1);
} else Eif (e.deltaY > 0) {
this.changeTime(unit, -1);
}
e.preventDefault();
};
render() {
const { militaryTime, minuteInterval } = this.props;
const { inputId } = this.state;
// Force the global locale onto our display moment
let selectedMoment = this.state.selectedTime.locale(moment.locale());
// Splits the moment string into seperate parts in order to add functionality to the TimePicker
const timeString = militaryTime
? selectedMoment.format('HH:mm')
: selectedMoment.format('LT');
const hourText = selectedMoment.format(militaryTime ? 'HH' : 'hh');
const minuteText = selectedMoment.format('mm');
const postText = selectedMoment.format('A');
const text = (
<Input
label=""
name={inputId}
id={inputId}
onChange={() => {}}
value={timeString}
onMouseDown={this.onMouseDown}
onFocus={this.onFocus}
inputRef={ref => (this.clickTextField = ref)}
readOnly
/>
);
const dropdownElement = (
<EventOverlay
allowClickAway
anchorNode={this.state.anchorNode}
close={this.hidePopover}
isOpen={this.state.isOpen}
>
<TimePickerDropdown>
<TimeSelector
unit="h"
min={0}
value={hourText}
onWheel={this.onSelectWheel}
inputRef={ref => (this.hour = ref)}
onKeyDown={this.onSelectKeyDown}
onUpClick={() => this.changeTime('h', 1)}
onDownClick={() => this.changeTime('h', -1)}
militaryTime={militaryTime}
/>
:
<TimeSelector
unit="m"
min={0}
value={minuteText}
onWheel={this.onSelectWheel}
inputRef={ref => (this.minute = ref)}
onKeyDown={this.onSelectKeyDown}
onUpClick={() => this.changeTime('m', minuteInterval)}
onDownClick={() => this.changeTime('m', -minuteInterval)}
/>
{!militaryTime && (
<TimeSelector
unit="pre"
value={postText}
inputRef={ref => (this.pre = ref)}
onKeyDown={this.onSelectKeyDown}
onWheel={this.onSelectWheel}
onUpClick={() => this.changeTime('h', 12)}
onDownClick={() => this.changeTime('h', -12)}
/>
)}
</TimePickerDropdown>
</EventOverlay>
);
return (
<div className="cui-timepicker-container">
{text}
{dropdownElement}
</div>
);
}
}
TimePicker.propTypes = {
/** @prop Optional CSS class name | '' */
className: PropTypes.string,
/** @prop Set Input element ID | '' */
inputId: PropTypes.string,
/** @prop Choose to use military time | false */
militaryTime: PropTypes.bool,
/** @prop Determine the minute interval | 1 */
minuteInterval: PropTypes.oneOf([1, 5, 15, 30, 60]),
/** @prop Callback function invoked when user makes a change | null */
onChange: PropTypes.func,
/** @prop Set the initial selected time | null */
selectedTime: PropTypes.instanceOf(Date),
};
TimePicker.defaultProps = {
className: '',
inputId: '',
militaryTime: false,
minuteInterval: 1,
onChange: null,
selectedTime: null,
};
TimePicker.displayName = 'TimePicker';
export default TimePicker;
/**
* @component time-picker
* @section default
* @react
import { Input, TimePicker } from '@collab-ui/react';
export default class TimePickerDefault extends React.PureComponent {
render() {
return (
<div className='timePicker-container'>
<TimePicker
selectedTime={new Date()}
/>
</div>
);
}
}
**/
/**
* @component time-picker
* @section 24-hour
* @react
import { Input, TimePicker } from '@collab-ui/react';
export default class TimePicker24Hr extends React.PureComponent {
render() {
return (
<div className='timePicker-container'>
<TimePicker
selectedTime={new Date()}
militaryTime
/>
</div>
);
}
}
**/
/**
* @component time-picker
* @section 30-minute-step
* @react
import { Input, TimePicker } from '@collab-ui/react';
export default class TimePicker30MinStep extends React.PureComponent {
render() {
return (
<div className='timePicker-container'>
<TimePicker
minuteInterval={30}
selectedTime={new Date()}
/>
</div>
);
}
}
**/
|