Add a query editor help component

By adding a help component to your plugin, you can for example create “cheat sheets” with commonly used queries. When the user clicks on one of the examples, it automatically updates the query editor. It’s a great way to increase productivity for your users.

  1. Create a file QueryEditorHelp.tsx in the src directory of your plugin, with the following content:

    1. import React from 'react';
    2. import { QueryEditorHelpProps } from '@grafana/data';
    3. export default (props: QueryEditorHelpProps) => {
    4. return <h2>My cheat sheet</h2>;
    5. };
  2. Configure the plugin to use the QueryEditorHelp.

    1. import QueryEditorHelp from './QueryEditorHelp';
    1. export const plugin = new DataSourcePlugin<DataSource, MyQuery, MyDataSourceOptions>(DataSource)
    2. .setConfigEditor(ConfigEditor)
    3. .setQueryEditor(QueryEditor)
    4. .setQueryEditorHelp(QueryEditorHelp);
  3. Create a few examples.

    1. import React from 'react';
    2. import { QueryEditorHelpProps, DataQuery } from '@grafana/data';
    3. const examples = [
    4. {
    5. title: 'Addition',
    6. expression: '1 + 2',
    7. label: 'Add two integers',
    8. },
    9. {
    10. title: 'Subtraction',
    11. expression: '2 - 1',
    12. label: 'Subtract an integer from another',
    13. },
    14. ];
    15. export default (props: QueryEditorHelpProps) => {
    16. return (
    17. <div>
    18. <h2>Cheat Sheet</h2>
    19. {examples.map((item, index) => (
    20. <div className="cheat-sheet-item" key={index}>
    21. <div className="cheat-sheet-item__title">{item.title}</div>
    22. {item.expression ? (
    23. <div
    24. className="cheat-sheet-item__example"
    25. onClick={(e) => props.onClickExample({ refId: 'A', queryText: item.expression } as DataQuery)}
    26. >
    27. <code>{item.expression}</code>
    28. </div>
    29. ) : null}
    30. <div className="cheat-sheet-item__label">{item.label}</div>
    31. </div>
    32. ))}
    33. </div>
    34. );
    35. };