1
1
import React , { useState } from 'react' ;
2
2
import { VariableQuery } from '../types' ;
3
- import { CodeEditor , Field , InlineField , Input , Button , Alert } from '@grafana/ui' ;
3
+ import { InlineField , Input , Alert , InlineFieldRow , Button } from '@grafana/ui' ;
4
+ import { QueryEditorRaw } from './QueryEditorRaw' ;
4
5
5
6
interface VariableQueryProps {
6
7
query : VariableQuery ;
@@ -9,53 +10,44 @@ interface VariableQueryProps {
9
10
10
11
export const VariableQueryEditor = ( { onChange, query } : VariableQueryProps ) => {
11
12
const [ state , setState ] = useState ( query ) ;
12
-
13
- const saveQuery = ( ) => {
14
- onChange ( state , `${ state . collection } (${ state . queryText } )` ) ;
15
- } ;
16
-
17
- const handleCollectionChange = ( event : React . FormEvent < HTMLInputElement > ) =>
18
- setState ( {
19
- ...state ,
20
- collection : event . currentTarget . value ,
21
- } ) ;
22
-
23
- const handleQueryTextChange = ( text : string ) =>
24
- setState ( {
25
- ...state ,
26
- queryText : text ,
27
- } ) ;
28
-
29
13
return (
30
14
< >
31
- < InlineField
32
- label = "Collection"
33
- tooltip = "Enter the MongoDB collection"
34
- error = "Please enter the collection"
35
- invalid = { ! query . collection }
36
- >
37
- < Input name = "collection" onChange = { handleCollectionChange } value = { state . collection } > </ Input >
38
- </ InlineField >
39
- < Field label = "Query Text" description = "MongoDB aggregate (JSON)" >
40
- < CodeEditor
41
- width = "100%"
42
- height = { 300 }
43
- language = "json"
44
- onBlur = { saveQuery }
45
- value = { query . queryText || '' }
46
- showMiniMap = { false }
47
- showLineNumbers = { true }
48
- onChange = { handleQueryTextChange }
49
- monacoOptions = { { fontSize : 14 } }
50
- />
51
- </ Field >
52
- < Alert title = "Query info" severity = "info" >
15
+ < InlineFieldRow >
16
+ < InlineField
17
+ label = "Collection"
18
+ tooltip = "Name of MongoDB collection to query"
19
+ error = "Collection is required"
20
+ invalid = { ! state . collection }
21
+ >
22
+ < Input
23
+ name = "collection"
24
+ onChange = { ( evt ) => setState ( { ...state , collection : evt . currentTarget . value } ) }
25
+ value = { state . collection }
26
+ > </ Input >
27
+ </ InlineField >
28
+ < Button
29
+ onClick = { ( ) => {
30
+ onChange (
31
+ { queryText : state . queryText , collection : state . collection } ,
32
+ `${ state . collection } (${ state . queryText } )` ,
33
+ ) ;
34
+ } }
35
+ >
36
+ Save and Query
37
+ </ Button >
38
+ </ InlineFieldRow >
39
+
40
+ < QueryEditorRaw
41
+ query = { query . queryText ?? '' }
42
+ language = "json"
43
+ onBlur = { ( queryText ) => setState ( { ...query , queryText : queryText } ) }
44
+ height = { 300 }
45
+ fontSize = { 14 }
46
+ />
47
+ < Alert title = "Query info" severity = "info" style = { { marginTop : 2 } } >
53
48
The query result is expected to contain < code > value</ code > field which has elements of type < code > string</ code > { ' ' }
54
49
or < code > number</ code >
55
50
</ Alert >
56
- < Button onClick = { saveQuery } variant = "primary" >
57
- Query
58
- </ Button >
59
51
</ >
60
52
) ;
61
53
} ;
0 commit comments