Skip to content

Commit f8c821a

Browse files
committed
collapsible
1 parent 802908c commit f8c821a

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/buttonPanel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const ButtonPanel: React.FC<Props> = ({ options }) => {
1010
<HorizontalGroup justify="center">
1111
{options.buttons.map((b: ButtonOptions, index: number) => (
1212
<Button key={index} variant="primary">
13-
{b.text} {b.query} {b.datasource}
13+
{b.text || 'Button'}
1414
</Button>
1515
))}
1616
</HorizontalGroup>

src/editor.tsx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PanelOptionsEditorBuilder, SelectableValue } from '@grafana/data';
22
import { getBackendSrv } from '@grafana/runtime';
3-
import { Button, Field, Input, Select } from '@grafana/ui';
3+
import { Button, Collapse, Field, Input, Select } from '@grafana/ui';
44
import React from 'react';
55
import { ButtonOptions, Options } from 'types';
66

@@ -11,6 +11,7 @@ interface EditorProps {
1111

1212
const Editor: React.FC<EditorProps> = ({ buttons, onChange }) => {
1313
const [elems, setElems] = React.useState<SelectableValue<string>[]>();
14+
const [isOpen, setOpen] = React.useState<boolean>(true);
1415
React.useEffect(() => {
1516
let isSubscribed = true;
1617
const fetchData = async () => {
@@ -32,11 +33,17 @@ const Editor: React.FC<EditorProps> = ({ buttons, onChange }) => {
3233
return (
3334
<React.Fragment>
3435
{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">
3743
<Input
3844
id={'t-' + index.toString()}
3945
value={b.text}
46+
placeholder="Button"
4047
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
4148
let button = { ...buttons[index] };
4249
onChange([
@@ -74,11 +81,31 @@ const Editor: React.FC<EditorProps> = ({ buttons, onChange }) => {
7481
}}
7582
/>
7683
</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>
8196
))}
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>
82109
</React.Fragment>
83110
);
84111
};
@@ -87,8 +114,8 @@ export function addEditor(builder: PanelOptionsEditorBuilder<Options>) {
87114
builder.addCustomEditor({
88115
id: 'buttons',
89116
path: 'buttons',
90-
name: 'Buttons',
91-
defaultValue: [{ text: 'click', datasource: '', query: '' }],
117+
name: '',
118+
defaultValue: [{ text: '', datasource: '', query: '' }],
92119
editor: props => <Editor buttons={props.value} onChange={props.onChange} />,
93120
});
94121
}

0 commit comments

Comments
 (0)