-
Notifications
You must be signed in to change notification settings - Fork 11.6k
/
TimeRangePicker.tsx
248 lines (218 loc) · 6.84 KB
/
TimeRangePicker.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
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
import { css } from '@emotion/css';
import { useDialog } from '@react-aria/dialog';
import { FocusScope } from '@react-aria/focus';
import { useOverlay } from '@react-aria/overlays';
import React, { memo, FormEvent, createRef, useState } from 'react';
import {
isDateTime,
rangeUtil,
GrafanaTheme2,
dateTimeFormat,
timeZoneFormatUserFriendly,
TimeRange,
TimeZone,
dateMath,
} from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { useStyles2 } from '../../themes/ThemeContext';
import { t, Trans } from '../../utils/i18n';
import { ButtonGroup } from '../Button';
import { ToolbarButton } from '../ToolbarButton';
import { Tooltip } from '../Tooltip/Tooltip';
import { TimePickerContent } from './TimeRangePicker/TimePickerContent';
import { quickOptions } from './options';
/** @public */
export interface TimeRangePickerProps {
hideText?: boolean;
value: TimeRange;
timeZone?: TimeZone;
fiscalYearStartMonth?: number;
timeSyncButton?: JSX.Element;
isSynced?: boolean;
onChange: (timeRange: TimeRange) => void;
onChangeTimeZone: (timeZone: TimeZone) => void;
onChangeFiscalYearStartMonth?: (month: number) => void;
onMoveBackward: () => void;
onMoveForward: () => void;
onZoom: () => void;
history?: TimeRange[];
hideQuickRanges?: boolean;
widthOverride?: number;
isOnCanvas?: boolean;
}
export interface State {
isOpen: boolean;
}
export function TimeRangePicker(props: TimeRangePickerProps) {
const [isOpen, setOpen] = useState(false);
const {
value,
onMoveBackward,
onMoveForward,
onZoom,
timeZone,
fiscalYearStartMonth,
timeSyncButton,
isSynced,
history,
onChangeTimeZone,
onChangeFiscalYearStartMonth,
hideQuickRanges,
widthOverride,
isOnCanvas,
} = props;
const onChange = (timeRange: TimeRange) => {
props.onChange(timeRange);
setOpen(false);
};
const onOpen = (event: FormEvent<HTMLButtonElement>) => {
event.stopPropagation();
event.preventDefault();
setOpen(!isOpen);
};
const onClose = () => {
setOpen(false);
};
const ref = createRef<HTMLElement>();
const { overlayProps } = useOverlay({ onClose, isDismissable: true, isOpen }, ref);
const { dialogProps } = useDialog({}, ref);
const styles = useStyles2(getStyles);
const hasAbsolute = isDateTime(value.raw.from) || isDateTime(value.raw.to);
const variant = isSynced ? 'active' : isOnCanvas ? 'canvas' : 'default';
const currentTimeRange = formattedRange(value, timeZone);
return (
<ButtonGroup className={styles.container}>
{hasAbsolute && (
<ToolbarButton
aria-label={t('time-picker.range-picker.backwards-time-aria-label', 'Move time range backwards')}
variant={variant}
onClick={onMoveBackward}
icon="angle-left"
narrow
/>
)}
<Tooltip content={<TimePickerTooltip timeRange={value} timeZone={timeZone} />} placement="bottom" interactive>
<ToolbarButton
data-testid={selectors.components.TimePicker.openButton}
aria-label={t('time-picker.range-picker.current-time-selected', 'Time range selected: {{currentTimeRange}}', {
currentTimeRange,
})}
aria-controls="TimePickerContent"
onClick={onOpen}
icon="clock-nine"
isOpen={isOpen}
variant={variant}
>
<TimePickerButtonLabel {...props} />
</ToolbarButton>
</Tooltip>
{isOpen && (
<section ref={ref} {...overlayProps} {...dialogProps}>
<FocusScope contain autoFocus>
<TimePickerContent
timeZone={timeZone}
fiscalYearStartMonth={fiscalYearStartMonth}
value={value}
onChange={onChange}
quickOptions={quickOptions}
history={history}
showHistory
widthOverride={widthOverride}
onChangeTimeZone={onChangeTimeZone}
onChangeFiscalYearStartMonth={onChangeFiscalYearStartMonth}
hideQuickRanges={hideQuickRanges}
/>
</FocusScope>
</section>
)}
{timeSyncButton}
{hasAbsolute && (
<ToolbarButton
aria-label={t('time-picker.range-picker.forwards-time-aria-label', 'Move time range forwards')}
onClick={onMoveForward}
icon="angle-right"
narrow
variant={variant}
/>
)}
<Tooltip content={ZoomOutTooltip} placement="bottom">
<ToolbarButton
aria-label={t('time-picker.range-picker.zoom-out-button', 'Zoom out time range')}
onClick={onZoom}
icon="search-minus"
variant={variant}
/>
</Tooltip>
</ButtonGroup>
);
}
TimeRangePicker.displayName = 'TimeRangePicker';
const ZoomOutTooltip = () => (
<>
<Trans i18nKey="time-picker.range-picker.zoom-out-tooltip">
Time range zoom out <br /> CTRL+Z
</Trans>
</>
);
const TimePickerTooltip = ({ timeRange, timeZone }: { timeRange: TimeRange; timeZone?: TimeZone }) => {
const styles = useStyles2(getLabelStyles);
return (
<>
{dateTimeFormat(timeRange.from, { timeZone })}
<div className="text-center">
<Trans i18nKey="time-picker.range-picker.to">to</Trans>
</div>
{dateTimeFormat(timeRange.to, { timeZone })}
<div className="text-center">
<span className={styles.utc}>{timeZoneFormatUserFriendly(timeZone)}</span>
</div>
</>
);
};
type LabelProps = Pick<TimeRangePickerProps, 'hideText' | 'value' | 'timeZone'>;
export const TimePickerButtonLabel = memo<LabelProps>(({ hideText, value, timeZone }) => {
const styles = useStyles2(getLabelStyles);
if (hideText) {
return null;
}
return (
<span className={styles.container}>
<span>{formattedRange(value, timeZone)}</span>
<span className={styles.utc}>{rangeUtil.describeTimeRangeAbbreviation(value, timeZone)}</span>
</span>
);
});
TimePickerButtonLabel.displayName = 'TimePickerButtonLabel';
const formattedRange = (value: TimeRange, timeZone?: TimeZone) => {
const adjustedTimeRange = {
to: dateMath.isMathString(value.raw.to) ? value.raw.to : value.to,
from: dateMath.isMathString(value.raw.from) ? value.raw.from : value.from,
};
return rangeUtil.describeTimeRange(adjustedTimeRange, timeZone);
};
const getStyles = () => {
return {
container: css`
position: relative;
display: flex;
vertical-align: middle;
`,
};
};
const getLabelStyles = (theme: GrafanaTheme2) => {
return {
container: css`
display: flex;
align-items: center;
white-space: nowrap;
`,
utc: css`
color: ${theme.v1.palette.orange};
font-size: ${theme.typography.size.sm};
padding-left: 6px;
line-height: 28px;
vertical-align: bottom;
font-weight: ${theme.typography.fontWeightMedium};
`,
};
};