-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathDateTimeEditor.react.js
More file actions
107 lines (99 loc) · 2.71 KB
/
DateTimeEditor.react.js
File metadata and controls
107 lines (99 loc) · 2.71 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
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import DateTimePicker from 'components/DateTimePicker/DateTimePicker.react';
import hasAncestor from 'lib/hasAncestor';
import React from 'react';
import styles from 'components/DateTimeEditor/DateTimeEditor.scss';
import { yearMonthDayTimeFormatter } from 'lib/DateUtils';
export default class DateTimeEditor extends React.Component {
constructor(props) {
super(props);
this.state = {
value: props.value,
text: props.value.toISOString(),
open: false
};
this.editorRef = React.createRef();
this.inputRef = React.createRef();
}
componentDidMount() {
this.inputRef.current.focus();
this.inputRef.current.select();
}
handleKey(e) {
if (e.keyCode === 13) {
this.commitDate();
this.props.onCommit(this.state.value);
}
}
toggle() {
this.setState(state => ({ open: !state.open }));
}
inputDate(e) {
this.setState({ text: e.target.value });
}
commitDate() {
if (this.state.text === this.props.value.toISOString()) {
return;
}
const date = new Date(this.state.text);
if (isNaN(date.getTime())) {
this.setState({
value: this.props.value,
text: this.props.value.toISOString()
});
} else {
const utc = new Date(
Date.UTC(
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds(),
date.getMilliseconds()
)
);
this.setState({ value: utc });
}
}
render() {
let popover = null;
if (this.state.open) {
popover = (
<div style={{ position: 'absolute', top: 30, left: 0 }}>
<DateTimePicker
value={this.state.value}
width={240}
local={false}
onChange={value => this.setState({ value, text: value.toISOString() })}
close={() => {
this.setState({ open: false }, () => this.props.onCommit(this.state.value));
}}
/>
</div>
);
}
return (
<div ref={this.editorRef} style={{ width: this.props.width }} className={styles.editor}>
<input
autoFocus
type="text"
ref={this.inputRef}
value={this.state.text}
onFocus={e => e.target.select()}
onClick={this.toggle.bind(this)}
onChange={this.inputDate.bind(this)}
onBlur={this.commitDate.bind(this)}
onKeyDown={this.handleKey.bind(this)}
/>
{popover}
</div>
);
}
}