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 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
6×
6×
6×
6×
6×
6×
6×
6×
6×
1×
1×
1×
1×
1×
1×
1×
1×
6×
42×
12×
1×
6×
6×
1×
1×
| import React from 'react';
import PropTypes from 'prop-types';
import {
Button, ButtonGroup, FormControl, InputGroup,
Label, OverlayTrigger, Popover, Glyphicon,
} from 'react-bootstrap';
import Select from 'react-select';
import Datetime from 'react-datetime';
import 'react-datetime/css/react-datetime.css';
import moment from 'moment';
import ControlHeader from '../ControlHeader';
import PopoverSection from '../../../components/PopoverSection';
const RELATIVE_TIME_OPTIONS = ['ago', 'from now'];
const TIME_GRAIN_OPTIONS = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years'];
const propTypes = {
animation: PropTypes.bool,
name: PropTypes.string.isRequired,
label: PropTypes.string,
description: PropTypes.string,
onChange: PropTypes.func,
value: PropTypes.string,
height: PropTypes.number,
};
const defaultProps = {
animation: true,
onChange: () => {},
value: '',
};
export default class DateFilterControl extends React.Component {
constructor(props) {
super(props);
const value = props.value || '';
this.state = {
num: '7',
grain: 'days',
rel: 'ago',
dttm: '',
type: 'free',
free: '',
};
const words = value.split(' ');
if (Ewords.length >= 3 && RELATIVE_TIME_OPTIONS.indexOf(words[2]) >= 0) {
this.state.num = words[0];
this.state.grain = words[1];
this.state.rel = words[2];
this.state.type = 'rel';
} else if (moment(value).isValid()) {
this.state.dttm = value;
this.state.type = 'fix';
} else {
this.state.free = value;
this.state.type = 'free';
}
}
onControlChange(target, opt) {
this.setState({ [target]: opt.value });
}
onNumberChange(event) {
this.setState({ num: event.target.value });
}
onFreeChange(event) {
this.setState({ free: event.target.value });
}
setType(type) {
this.setState({ type });
}
setValueAndClose(val) {
this.setState({ type: 'free', free: val }, this.close);
}
setDatetime(dttm) {
this.setState({ dttm: dttm.format().substring(0, 19) });
}
close() {
let val;
if (this.state.type === 'rel') {
val = `${this.state.num} ${this.state.grain} ${this.state.rel}`;
} else if (this.state.type === 'fix') {
val = this.state.dttm;
} else if (this.state.type === 'free') {
val = this.state.free;
}
this.props.onChange(val);
this.refs.trigger.hide();
}
renderPopover() {
return (
<Popover id="filter-popover">
<div style={{ width: '250px' }}>
<PopoverSection
title="Fixed"
isSelected={this.state.type === 'fix'}
onSelect={this.setType.bind(this, 'fix')}
>
<InputGroup bsSize="small">
<InputGroup.Addon>
<Glyphicon glyph="calendar" />
</InputGroup.Addon>
<Datetime
inputProps={{ className: 'form-control input-sm' }}
dateFormat="YYYY-MM-DD"
defaultValue={this.state.dttm}
onFocus={this.setType.bind(this, 'fix')}
onChange={this.setDatetime.bind(this)}
timeFormat="h:mm:ss"
/>
</InputGroup>
</PopoverSection>
<PopoverSection
title="Relative"
isSelected={this.state.type === 'rel'}
onSelect={this.setType.bind(this, 'rel')}
>
<div className="clearfix">
<div style={{ width: '50px' }} className="input-inline">
<FormControl
onFocus={this.setType.bind(this, 'rel')}
value={this.state.num}
onChange={this.onNumberChange.bind(this)}
bsSize="small"
/>
</div>
<div style={{ width: '95px' }} className="input-inline">
<Select
onFocus={this.setType.bind(this, 'rel')}
value={this.state.grain}
clearable={false}
options={TIME_GRAIN_OPTIONS.map(s => ({ label: s, value: s }))}
onChange={this.onControlChange.bind(this, 'grain')}
/>
</div>
<div style={{ width: '95px' }} className="input-inline">
<Select
value={this.state.rel}
onFocus={this.setType.bind(this, 'rel')}
clearable={false}
options={RELATIVE_TIME_OPTIONS.map(s => ({ label: s, value: s }))}
onChange={this.onControlChange.bind(this, 'rel')}
/>
</div>
</div>
</PopoverSection>
<PopoverSection
title="Free form"
isSelected={this.state.type === 'free'}
onSelect={this.setType.bind(this, 'free')}
info={
'Superset supports smart date parsing. Strings like `last sunday` or ' +
'`last october` can be used.'
}
>
<FormControl
onFocus={this.setType.bind(this, 'free')}
value={this.state.free}
onChange={this.onFreeChange.bind(this)}
bsSize="small"
/>
</PopoverSection>
<div className="clearfix">
<Button
bsSize="small"
className="float-left ok"
bsStyle="primary"
onClick={this.close.bind(this)}
>
Ok
</Button>
<ButtonGroup
className="float-right"
>
<Button
bsSize="small"
className="now"
onClick={this.setValueAndClose.bind(this, 'now')}
>
now
</Button>
<Button
bsSize="small"
className="clear"
onClick={this.setValueAndClose.bind(this, '')}
>
clear
</Button>
</ButtonGroup>
</div>
</div>
</Popover>
);
}
render() {
const value = this.props.value || '';
return (
<div>
<ControlHeader {...this.props} />
<OverlayTrigger
animation={this.props.animation}
container={document.body}
trigger="click"
rootClose
ref="trigger"
placement="right"
overlay={this.renderPopover()}
>
<Label style={{ cursor: 'pointer' }}>
{value.replace('T00:00:00', '') || '∞'}
</Label>
</OverlayTrigger>
</div>
);
}
}
DateFilterControl.propTypes = propTypes;
DateFilterControl.defaultProps = defaultProps;
|