1
1
import { PanelOptionsEditorBuilder , SelectableValue } from '@grafana/data' ;
2
2
import { getBackendSrv } from '@grafana/runtime' ;
3
- import { Select } from '@grafana/ui' ;
3
+ import { Button , Field , Input , Select } from '@grafana/ui' ;
4
4
import React from 'react' ;
5
- import { SimpleOptions } from 'types' ;
5
+ import { ButtonOptions , Options } from 'types' ;
6
6
7
- const MyEditor : React . FC = ( ) => {
7
+ interface EditorProps {
8
+ buttons : ButtonOptions [ ] ;
9
+ onChange : ( buttons : ButtonOptions [ ] ) => void ;
10
+ }
11
+
12
+ const Editor : React . FC < EditorProps > = ( { buttons, onChange } ) => {
8
13
const [ elems , setElems ] = React . useState < SelectableValue < string > [ ] > ( ) ;
9
14
React . useEffect ( ( ) => {
10
15
let isSubscribed = true ;
@@ -24,15 +29,66 @@ const MyEditor: React.FC = () => {
24
29
isSubscribed = false ;
25
30
} ;
26
31
} , [ ] ) ;
27
- return < Select onChange = { ( ) => { } } options = { elems } /> ;
32
+ return (
33
+ < React . Fragment >
34
+ { buttons . map ( ( b : ButtonOptions , index : number ) => (
35
+ < div >
36
+ < Field label = "Text" description = "Text to be displayed on button" >
37
+ < Input
38
+ id = { 't-' + index . toString ( ) }
39
+ value = { b . text }
40
+ onChange = { ( e : React . ChangeEvent < HTMLInputElement > ) => {
41
+ let button = { ...buttons [ index ] } ;
42
+ onChange ( [
43
+ ...buttons . slice ( 0 , index ) ,
44
+ { text : e . target . value , datasource : button . datasource , query : button . query } ,
45
+ ...buttons . slice ( index + 1 ) ,
46
+ ] ) ;
47
+ } }
48
+ />
49
+ </ Field >
50
+ < Field label = "Datasource" description = "Select Datasource for the query" >
51
+ < Select
52
+ onChange = { ( e : SelectableValue < string > ) => {
53
+ let button = { ...buttons [ index ] } ;
54
+ onChange ( [
55
+ ...buttons . slice ( 0 , index ) ,
56
+ { text : button . text , datasource : e . value || '' , query : button . query } ,
57
+ ...buttons . slice ( index + 1 ) ,
58
+ ] ) ;
59
+ } }
60
+ options = { elems }
61
+ />
62
+ </ Field >
63
+ < Field label = "Query" description = "Query to be triggered on Button Click" >
64
+ < Input
65
+ id = { 'q-' + index . toString ( ) }
66
+ value = { b . query }
67
+ onChange = { ( e : React . ChangeEvent < HTMLInputElement > ) => {
68
+ let button = { ...buttons [ index ] } ;
69
+ onChange ( [
70
+ ...buttons . slice ( 0 , index ) ,
71
+ { text : button . text , datasource : button . datasource , query : e . target . value } ,
72
+ ...buttons . slice ( index + 1 ) ,
73
+ ] ) ;
74
+ } }
75
+ />
76
+ </ Field >
77
+ < Button variant = "secondary" icon = "plus" size = "sm" >
78
+ Add Button
79
+ </ Button >
80
+ </ div >
81
+ ) ) }
82
+ </ React . Fragment >
83
+ ) ;
28
84
} ;
29
85
30
- export function addEditor ( builder : PanelOptionsEditorBuilder < SimpleOptions > ) {
86
+ export function addEditor ( builder : PanelOptionsEditorBuilder < Options > ) {
31
87
builder . addCustomEditor ( {
32
- id : 'datasource ' ,
33
- path : 'datasource ' ,
34
- name : 'Datasource ' ,
35
- description : 'Select Datasource for input query' ,
36
- editor : ( ) => < MyEditor /> ,
88
+ id : 'buttons ' ,
89
+ path : 'buttons ' ,
90
+ name : 'Buttons ' ,
91
+ defaultValue : [ { text : 'click' , datasource : '' , query : '' } ] ,
92
+ editor : props => < Editor buttons = { props . value } onChange = { props . onChange } /> ,
37
93
} ) ;
38
94
}
0 commit comments