Skip to content

Commit 802908c

Browse files
committed
button panel
1 parent 7ec8385 commit 802908c

File tree

5 files changed

+93
-69
lines changed

5 files changed

+93
-69
lines changed

src/SimplePanel.tsx

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/buttonPanel.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { PanelProps } from '@grafana/data';
2+
import { Button, HorizontalGroup } from '@grafana/ui';
3+
import React from 'react';
4+
import { ButtonOptions, Options } from 'types';
5+
6+
interface Props extends PanelProps<Options> {}
7+
8+
export const ButtonPanel: React.FC<Props> = ({ options }) => {
9+
return (
10+
<HorizontalGroup justify="center">
11+
{options.buttons.map((b: ButtonOptions, index: number) => (
12+
<Button key={index} variant="primary">
13+
{b.text} {b.query} {b.datasource}
14+
</Button>
15+
))}
16+
</HorizontalGroup>
17+
);
18+
};

src/editor.tsx

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

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 }) => {
813
const [elems, setElems] = React.useState<SelectableValue<string>[]>();
914
React.useEffect(() => {
1015
let isSubscribed = true;
@@ -24,15 +29,66 @@ const MyEditor: React.FC = () => {
2429
isSubscribed = false;
2530
};
2631
}, []);
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+
);
2884
};
2985

30-
export function addEditor(builder: PanelOptionsEditorBuilder<SimpleOptions>) {
86+
export function addEditor(builder: PanelOptionsEditorBuilder<Options>) {
3187
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} />,
3793
});
3894
}

src/module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { PanelPlugin } from '@grafana/data';
2+
import { ButtonPanel } from './buttonPanel';
23
import { addEditor } from './editor';
3-
import { SimplePanel } from './SimplePanel';
4-
import { SimpleOptions } from './types';
4+
import { Options } from './types';
55

6-
export const plugin = new PanelPlugin<SimpleOptions>(SimplePanel);
6+
export const plugin = new PanelPlugin<Options>(ButtonPanel);
77

88
plugin.setPanelOptions(builder => addEditor(builder));

src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export interface SimpleOptions {
1+
export interface ButtonOptions {
22
text: string;
3+
query: string;
34
datasource: string;
45
}
6+
7+
export interface Options {
8+
buttons: ButtonOptions[];
9+
}

0 commit comments

Comments
 (0)