forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPickerRenderer.tsx
112 lines (98 loc) · 3.13 KB
/
PickerRenderer.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
// import { css } from '@emotion/css';
import React, { CSSProperties, FunctionComponent, PropsWithChildren, ReactElement, useMemo } from 'react';
// eslint-disable-next-line no-restricted-imports
import { useSelector } from 'react-redux';
import { selectors } from '@grafana/e2e-selectors';
import { Tooltip } from '@grafana/ui';
import { FnGlobalState } from 'app/core/reducers/fn-slice';
import type { StoreState } from 'app/types';
import { variableAdapters } from '../adapters';
import { VariableHide, VariableModel } from '../types';
interface Props {
variable: VariableModel;
readOnly?: boolean;
}
// not using this style for now.
// const renderWrapperStyle = css`
// & button,
// & span,
// & label,
// & input {
// height: 35px;
// font-size: 14px;
// }
// /* Center align filter picker buttons */
// & button {
// margin-top: 2px;
// }
// /* Adhoc filter: Disable green border and make input height similar to others */
// & div[class*='input-wrapper'] {
// min-height: 0 !important;
// box-shadow: none !important;
// }
// `;
export const PickerRenderer: FunctionComponent<Props> = (props) => {
const PickerToRender = useMemo(() => variableAdapters.get(props.variable.type).picker, [props.variable]);
if (!props.variable) {
return <div>Couldn't load variable</div>;
}
return (
<div className="gf-form">
<PickerLabel variable={props.variable} />
{props.variable.hide !== VariableHide.hideVariable && PickerToRender && (
<div>
<PickerToRender variable={props.variable} readOnly={props.readOnly ?? false} />
</div>
)}
</div>
);
};
const COMMON_PICKER_LABEL_STYLE: CSSProperties = {
border: 'none',
fontWeight: 500,
fontSize: '14px',
padding: '3px 6px',
letterSpacing: '0.15px',
height: '24px',
marginTop: '2px',
};
function PickerLabel({ variable }: PropsWithChildren<Props>): ReactElement | null {
const labelOrName = useMemo(() => variable.label || variable.name, [variable]);
const { FNDashboard, mode } = useSelector<StoreState, FnGlobalState>(({ fnGlobalState }) => fnGlobalState);
const fnLabelStyle = useMemo(
() => ({
...COMMON_PICKER_LABEL_STYLE,
color: mode === 'light' ? '#2D333E' : '#DBD9D7',
}),
[mode]
);
if (variable.hide !== VariableHide.dontHide) {
return null;
}
const fnLabelOrName = FNDashboard ? labelOrName.replace('druid_adhoc_filters', 'ad-hoc') : labelOrName;
const elementId = `var-${variable.id}`;
if (variable.description) {
return (
<Tooltip content={variable.description} placement={'bottom'}>
<label
className="gf-form-label gf-form-label--variable"
style={fnLabelStyle}
data-testid={selectors.pages.Dashboard.SubMenu.submenuItemLabels(labelOrName)}
htmlFor={elementId}
>
{fnLabelOrName}
</label>
</Tooltip>
);
}
return (
<label
className="gf-form-label gf-form-label--variable"
style={fnLabelStyle}
data-testid={selectors.pages.Dashboard.SubMenu.submenuItemLabels(labelOrName)}
htmlFor={elementId}
>
{fnLabelOrName}
</label>
);
}