-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHaystackQueryTypeSelector.tsx
More file actions
43 lines (38 loc) · 1.36 KB
/
HaystackQueryTypeSelector.tsx
File metadata and controls
43 lines (38 loc) · 1.36 KB
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
import { AsyncSelect, InlineField } from '@grafana/ui';
import React, { } from 'react';
import { QueryType } from 'types';
import { DataSource, queryTypes } from '../datasource';
export interface HaystackQueryTypeSelectorProps {
datasource: DataSource | null;
type?: string;
refId: string;
onChange: (type: string) => void;
}
export function HaystackQueryTypeSelector({ datasource, type, refId, onChange }: HaystackQueryTypeSelectorProps) {
const onTypeChange = (event: QueryType | null) => {
onChange(event?.value ?? queryTypeDefault.value!);
};
const queryTypeDefault = queryTypes[0];
function queryTypeFromValue(value?: string): QueryType | null {
return queryTypes.find((queryType) => queryType.value === value) ?? null;
}
async function defaultQueryTypes(): Promise<QueryType[]> {
return new Promise<QueryType[]>((resolve) => { resolve(queryTypes);})
}
return (
<InlineField label="Type">
<AsyncSelect
loadOptions={() => {
return datasource?.loadOps(refId) ?? defaultQueryTypes();
}}
defaultOptions
value={queryTypeFromValue(type)}
width={30}
onChange={(queryType) => {
// QueryType comes back as a SelectableValue, so we just convert it to the QueryType
onTypeChange(queryTypeFromValue(queryType.value ?? ""));
}}
/>
</InlineField>
);
}