Files
Ivana Huckova c23bc1e7b7 Prometheus: Show variable options in query builder (#44784)
* Prometheus: Show variable options

* Remove lint error

* Fix test for CodeQL

* Update public/app/plugins/datasource/prometheus/datasource.ts

Co-authored-by: Torkel Ödegaard <torkel@grafana.org>

* Update public/app/plugins/datasource/loki/datasource.ts

Co-authored-by: Torkel Ödegaard <torkel@grafana.org>

Co-authored-by: Torkel Ödegaard <torkel@grafana.org>
2022-02-03 11:40:19 +01:00

53 lines
1.5 KiB
TypeScript

import { Select } from '@grafana/ui';
import React, { useState } from 'react';
import { PromVisualQuery } from '../types';
import { SelectableValue, toOption } from '@grafana/data';
import { EditorField, EditorFieldGroup } from '@grafana/experimental';
import { css } from '@emotion/css';
export interface Props {
query: PromVisualQuery;
onChange: (query: PromVisualQuery) => void;
onGetMetrics: () => Promise<SelectableValue[]>;
}
export function MetricSelect({ query, onChange, onGetMetrics }: Props) {
const styles = getStyles();
const [state, setState] = useState<{
metrics?: Array<SelectableValue<any>>;
isLoading?: boolean;
}>({});
return (
<EditorFieldGroup>
<EditorField label="Metric">
<Select
inputId="prometheus-metric-select"
className={styles.select}
value={query.metric ? toOption(query.metric) : undefined}
placeholder="Select metric"
allowCustomValue
onOpenMenu={async () => {
setState({ isLoading: true });
const metrics = await onGetMetrics();
setState({ metrics, isLoading: undefined });
}}
isLoading={state.isLoading}
options={state.metrics}
onChange={({ value }) => {
if (value) {
onChange({ ...query, metric: value, labels: [] });
}
}}
/>
</EditorField>
</EditorFieldGroup>
);
}
const getStyles = () => ({
select: css`
min-width: 125px;
`,
});