From 475fd994a0e68d5aa8d207b6bb75f17cb0c0abd3 Mon Sep 17 00:00:00 2001 From: Marcus Olsson Date: Mon, 1 Mar 2021 14:12:52 +0100 Subject: [PATCH] Add guide on custom option editors (#31254) --- .../plugins/custom-panel-option-editors.md | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 docs/sources/developers/plugins/custom-panel-option-editors.md diff --git a/docs/sources/developers/plugins/custom-panel-option-editors.md b/docs/sources/developers/plugins/custom-panel-option-editors.md new file mode 100644 index 00000000000..db966d758bc --- /dev/null +++ b/docs/sources/developers/plugins/custom-panel-option-editors.md @@ -0,0 +1,120 @@ ++++ +title = "Custom panel option editors" ++++ + +# Custom panel option editors + +The Grafana plugin platform comes with a range of editors that allow your users to customize a panel. The standard editors cover the most common types of options, such as text input and boolean switches. If you don't find the editor you're looking for, you can build your own. In this guide, you'll learn how to build your own panel option editor. + +The simplest editor is a React component that accepts two props: `value` and `onChange`. `value` contains the current value of the option, and `onChange` updates it. + +The editor in the example below lets the user toggle a boolean value by clicking a button: + +**SimpleEditor.tsx** + +```ts +import React from 'react'; +import { Button } from '@grafana/ui'; +import { StandardEditorProps } from '@grafana/data'; + +export const SimpleEditor: React.FC> = ({ value, onChange }) => { + return ; +}; +``` + +To use a custom panel option editor, use the `addCustomEditor` on the `OptionsUIBuilder` object in your `module.ts` file. Configure the editor to use by setting the `editor` property to the `SimpleEditor` component. + +**module.ts** + +```ts +export const plugin = new PanelPlugin(SimplePanel).setPanelOptions(builder => { + return builder + .addCustomEditor({ + id: 'label', + path: 'label', + name: 'Label', + editor: SimpleEditor, + }); +}); +``` + +## Add settings to your panel option editor + +If you're using your custom editor to configure multiple options, you might want to be able to customize it. Add settings to your editor by setting the second template variable of `StandardEditorProps` to an interface that contains the settings you want to be able to configure. + +You can access the editor settings through the `item` prop. Here's an example of an editor that populates a drop-down with a range of numbers. The range is defined by the `from` and `to` properties in the `Settings` interface. + +**SimpleEditor.tsx** + +```ts +interface Settings { + from: number; + to: number; +} + +export const SimpleEditor: React.FC> = ({ item, value, onChange }) => { + const options: Array> = []; + + // Default values + const from = item.settings?.from ?? 1; + const to = item.settings?.from ?? 10; + + for (let i = from; i <= to; i++) { + options.push({ + label: i.toString(), + value: i, + }); + } + + return onChange(selectableValue.value)} />; +}; +``` + +Have you built a custom editor that you think would be useful to other plugin developers? Consider contributing it as a standard editor!