1
1
import { PanelOptionsEditorBuilder , SelectableValue } from '@grafana/data' ;
2
2
import { getBackendSrv } from '@grafana/runtime' ;
3
- import { Button , Field , Input , Select } from '@grafana/ui' ;
3
+ import { Button , Collapse , Field , Input , Select } from '@grafana/ui' ;
4
4
import React from 'react' ;
5
5
import { ButtonOptions , Options } from 'types' ;
6
6
@@ -11,6 +11,7 @@ interface EditorProps {
11
11
12
12
const Editor : React . FC < EditorProps > = ( { buttons, onChange } ) => {
13
13
const [ elems , setElems ] = React . useState < SelectableValue < string > [ ] > ( ) ;
14
+ const [ isOpen , setOpen ] = React . useState < boolean > ( true ) ;
14
15
React . useEffect ( ( ) => {
15
16
let isSubscribed = true ;
16
17
const fetchData = async ( ) => {
@@ -32,11 +33,17 @@ const Editor: React.FC<EditorProps> = ({ buttons, onChange }) => {
32
33
return (
33
34
< React . Fragment >
34
35
{ buttons . map ( ( b : ButtonOptions , index : number ) => (
35
- < div >
36
- < Field label = "Text" description = "Text to be displayed on button" >
36
+ < Collapse
37
+ label = { 'Button ' + ( index + 1 ) . toString ( ) }
38
+ isOpen = { isOpen }
39
+ collapsible
40
+ onToggle = { ( ) => setOpen ( ! isOpen ) }
41
+ >
42
+ < Field label = "Text" description = "Text to be displayed on the button" >
37
43
< Input
38
44
id = { 't-' + index . toString ( ) }
39
45
value = { b . text }
46
+ placeholder = "Button"
40
47
onChange = { ( e : React . ChangeEvent < HTMLInputElement > ) => {
41
48
let button = { ...buttons [ index ] } ;
42
49
onChange ( [
@@ -74,11 +81,31 @@ const Editor: React.FC<EditorProps> = ({ buttons, onChange }) => {
74
81
} }
75
82
/>
76
83
</ Field >
77
- < Button variant = "secondary" icon = "plus" size = "sm" >
78
- Add Button
79
- </ Button >
80
- </ div >
84
+ < Field >
85
+ < Button
86
+ icon = "trash-alt"
87
+ variant = "destructive"
88
+ onClick = { ( ) => {
89
+ console . log ( 'trash' ) ;
90
+ } }
91
+ >
92
+ Delete
93
+ </ Button >
94
+ </ Field >
95
+ </ Collapse >
81
96
) ) }
97
+ < Field >
98
+ < Button
99
+ variant = "secondary"
100
+ icon = "plus"
101
+ size = "sm"
102
+ onClick = { ( ) => {
103
+ onChange ( [ ...buttons , { text : '' , datasource : '' , query : '' } ] ) ;
104
+ } }
105
+ >
106
+ Add Button
107
+ </ Button >
108
+ </ Field >
82
109
</ React . Fragment >
83
110
) ;
84
111
} ;
@@ -87,8 +114,8 @@ export function addEditor(builder: PanelOptionsEditorBuilder<Options>) {
87
114
builder . addCustomEditor ( {
88
115
id : 'buttons' ,
89
116
path : 'buttons' ,
90
- name : 'Buttons ' ,
91
- defaultValue : [ { text : 'click ' , datasource : '' , query : '' } ] ,
117
+ name : '' ,
118
+ defaultValue : [ { text : '' , datasource : '' , query : '' } ] ,
92
119
editor : props => < Editor buttons = { props . value } onChange = { props . onChange } /> ,
93
120
} ) ;
94
121
}
0 commit comments