forked from kaiyuanshe/OpenHackathon-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTimeInput.tsx
79 lines (71 loc) · 2.11 KB
/
DateTimeInput.tsx
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
import { computed, observable } from 'mobx';
import { observer } from 'mobx-react';
import { ChangeEvent, PureComponent } from 'react';
import { Col, Form, InputGroup, Row } from 'react-bootstrap';
import { formatDate } from 'web-utility';
import { i18n } from '../models/Translation';
const { t } = i18n;
export interface DateTimeInputProps {
id?: string;
label: string;
name: string;
required?: boolean;
startAt?: string;
endAt?: string;
}
@observer
export class DateTimeInput extends PureComponent<DateTimeInputProps> {
@observable
start = '';
@observable
end = '';
@computed
get isInvalid() {
return +new Date(this.start) > +new Date(this.end);
}
handleInputChange = ({
currentTarget: { name, value },
}: ChangeEvent<HTMLInputElement>) => {
if (/StartedAt/.test(name)) {
this.start = value;
} else {
this.end = value;
}
};
render() {
const { id, label, name, required, startAt, endAt } = this.props,
{ isInvalid } = this;
return (
<Form.Group as={Row} className="mb-3" controlId={id}>
<Form.Label column sm={2}>
{label}
<span className="text-danger"> *</span>
</Form.Label>
<Col sm={10}>
<InputGroup className="mb-3">
<InputGroup.Text>{t('time_range')}</InputGroup.Text>
<Form.Control
name={`${name}StartedAt`}
type="datetime-local"
defaultValue={
startAt && formatDate(startAt, 'YYYY-MM-DDTHH:mm:ss')
}
{...{ required, isInvalid }}
onChange={this.handleInputChange}
/>
<Form.Control
name={`${name}EndedAt`}
type="datetime-local"
defaultValue={endAt && formatDate(endAt, 'YYYY-MM-DDTHH:mm:ss')}
{...{ required, isInvalid }}
onChange={this.handleInputChange}
/>
<Form.Control.Feedback type="invalid">
<span>{t('start_time_earlier_end_time')}</span>
</Form.Control.Feedback>
</InputGroup>
</Col>
</Form.Group>
);
}
}