-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
TimeZoneOption.tsx
161 lines (145 loc) · 4.68 KB
/
TimeZoneOption.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
import { css, cx } from '@emotion/css';
import { isString } from 'lodash';
import React, { PropsWithChildren, RefCallback } from 'react';
import { GrafanaTheme2, SelectableValue, getTimeZoneInfo } from '@grafana/data';
import { useTheme2 } from '../../../themes/ThemeContext';
import { stylesFactory } from '../../../themes/stylesFactory';
import { Icon } from '../../Icon/Icon';
import { TimeZoneDescription } from './TimeZoneDescription';
import { TimeZoneOffset } from './TimeZoneOffset';
import { TimeZoneTitle } from './TimeZoneTitle';
interface Props {
isFocused: boolean;
isSelected: boolean;
innerProps: JSX.IntrinsicElements['div'];
innerRef: RefCallback<HTMLDivElement>;
data: SelectableZone;
}
const offsetClassName = 'tz-utc-offset';
export interface SelectableZone extends SelectableValue<string> {
searchIndex: string;
}
export const WideTimeZoneOption: React.FC<PropsWithChildren<Props>> = (props, ref) => {
const { children, innerProps, innerRef, data, isSelected, isFocused } = props;
const theme = useTheme2();
const styles = getStyles(theme);
const timestamp = Date.now();
const containerStyles = cx(styles.container, isFocused && styles.containerFocused);
if (!isString(data.value)) {
return null;
}
const timeZoneInfo = getTimeZoneInfo(data.value, timestamp);
return (
<div className={containerStyles} {...innerProps} ref={innerRef} aria-label="Select option">
<div className={cx(styles.leftColumn, styles.row)}>
<div className={cx(styles.leftColumn, styles.wideRow)}>
<TimeZoneTitle title={children} />
<div className={styles.spacer} />
<TimeZoneDescription info={timeZoneInfo} />
</div>
<div className={styles.rightColumn}>
<TimeZoneOffset
/* Use the timeZoneInfo to pass the correct timeZone name,
as 'Default' has value '' which defaults to browser timezone */
timeZone={timeZoneInfo?.ianaName || data.value}
timestamp={timestamp}
className={offsetClassName}
/>
{isSelected && (
<span>
<Icon name="check" />
</span>
)}
</div>
</div>
</div>
);
};
export const CompactTimeZoneOption: React.FC<React.PropsWithChildren<Props>> = (props, ref) => {
const { children, innerProps, innerRef, data, isSelected, isFocused } = props;
const theme = useTheme2();
const styles = getStyles(theme);
const timestamp = Date.now();
const containerStyles = cx(styles.container, isFocused && styles.containerFocused);
if (!isString(data.value)) {
return null;
}
const timeZoneInfo = getTimeZoneInfo(data.value, timestamp);
return (
<div className={containerStyles} {...innerProps} ref={innerRef} aria-label="Select option">
<div className={styles.body}>
<div className={styles.row}>
<div className={styles.leftColumn}>
<TimeZoneTitle title={children} />
</div>
<div className={styles.rightColumn}>
{isSelected && (
<span>
<Icon name="check" />
</span>
)}
</div>
</div>
<div className={styles.row}>
<div className={styles.leftColumn}>
<TimeZoneDescription info={timeZoneInfo} />
</div>
<div className={styles.rightColumn}>
<TimeZoneOffset
timestamp={timestamp}
/* Use the timeZoneInfo to pass the correct timeZone name,
as 'Default' has value '' which defaults to browser timezone */
timeZone={timeZoneInfo?.ianaName || data.value}
className={offsetClassName}
/>
</div>
</div>
</div>
</div>
);
};
const getStyles = stylesFactory((theme: GrafanaTheme2) => {
return {
container: css`
display: flex;
align-items: center;
flex-direction: row;
flex-shrink: 0;
white-space: nowrap;
cursor: pointer;
padding: 6px 8px 4px;
&:hover {
background: ${theme.colors.action.hover};
}
`,
containerFocused: css`
background: ${theme.colors.action.hover};
`,
body: css`
display: flex;
font-weight: ${theme.typography.fontWeightMedium};
flex-direction: column;
flex-grow: 1;
`,
row: css`
display: flex;
flex-direction: row;
`,
leftColumn: css`
flex-grow: 1;
text-overflow: ellipsis;
`,
rightColumn: css`
justify-content: flex-end;
align-items: center;
`,
wideRow: css`
display: flex;
flex-direction: row;
align-items: baseline;
`,
spacer: css`
margin-left: 6px;
`,
};
});