-
Notifications
You must be signed in to change notification settings - Fork 5
/
DateTimeRange.js
107 lines (100 loc) · 3.21 KB
/
DateTimeRange.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
import classNames from 'classnames';
import React, { PropTypes } from 'react';
import HelpBlock from 'react-bootstrap/lib/HelpBlock';
import DatePicker from 'shared/date-picker';
import Field from './Field';
import Time from './Time';
export default class DateTimeRange extends React.Component {
static propTypes = {
controlProps: PropTypes.shape({
onBlur: PropTypes.func,
onChange: PropTypes.func.isRequired,
required: PropTypes.bool,
renderDatePicker: PropTypes.bool,
value: PropTypes.shape({
begin: PropTypes.object.isRequired,
end: PropTypes.object.isRequired,
}).isRequired,
}).isRequired,
help: PropTypes.string,
id: PropTypes.string.isRequired,
noLabels: PropTypes.bool,
validationState: PropTypes.string,
};
handleBlur = () => {
if (this.props.controlProps.onBlur) {
this.props.controlProps.onBlur(this.props.controlProps.value);
}
}
handleDateChange = (date) => {
const value = this.props.controlProps.value;
this.props.controlProps.onChange({
begin: { ...value.begin, date },
end: { ...value.end, date },
});
}
handleStartTimeChange = (time) => {
const value = this.props.controlProps.value;
this.props.controlProps.onChange({
...value,
begin: { ...value.begin, time },
});
}
handleEndTimeChange = (time) => {
const value = this.props.controlProps.value;
this.props.controlProps.onChange({
...value,
end: { ...value.end, time },
});
}
render() {
const value = this.props.controlProps.value;
const renderDatePicker = this.props.controlProps.renderDatePicker === undefined
? true : this.props.controlProps.renderDatePicker;
const requiredPostfix = this.props.controlProps.required ? '*' : '';
return (
<div
className={classNames(
'date-time-range-field-container',
{ 'has-error': this.props.validationState },
)}
>
<div className="date-time-range-field">
{renderDatePicker === true ? (
<Field
componentClass={DatePicker}
controlProps={{
onBlur: this.handleBlur,
onChange: this.handleDateChange,
value: value.begin.date,
}}
id={`${this.props.id}-date`}
label={this.props.noLabels ? '' : `Päivä${requiredPostfix}`}
/>
) : null}
<Field
componentClass={Time}
controlProps={{
onBlur: this.handleBlur,
onChange: this.handleStartTimeChange,
value: value.begin.time,
}}
id={`${this.props.id}-begin-time`}
label={this.props.noLabels ? '' : `Alkaa${requiredPostfix}`}
/>
<Field
componentClass={Time}
controlProps={{
onBlur: this.handleBlur,
onChange: this.handleEndTimeChange,
value: value.end.time,
}}
id={`${this.props.id}-end-time`}
label={this.props.noLabels ? '' : `Päättyy${requiredPostfix}`}
/>
</div>
{this.props.help && <HelpBlock>{this.props.help}</HelpBlock>}
</div>
);
}
}