Files
grafana/docs/sources/developers/plugins/add-query-editor-help.md
Joseph Perez 49a18bc8e2 Plugins doc review chunk 2 (#67691)
* Doc style edit for 7 topics

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Proofread topics

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Prettier

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Doc fix

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Update docs/sources/developers/plugins/add-query-editor-help.md

Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>

* Doc fixes

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Changes from doc review

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Incorporate review feedback

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* More fixes

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* More doc fixes

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

---------

Signed-off-by: Joe Perez <joseph.perez@grafana.com>
Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
2023-05-12 13:33:49 -05:00

2.1 KiB

title
Add query editor help

Add query editor help

Query editors support the addition of a help component to display examples of potential queries. When the user clicks on one of the examples, the query editor is automatically updated. This helps the user to make faster queries.

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

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

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

    import React from 'react';
    import { QueryEditorHelpProps, DataQuery } from '@grafana/data';
    
    const examples = [
      {
        title: 'Addition',
        expression: '1 + 2',
        label: 'Add two integers',
      },
      {
        title: 'Subtraction',
        expression: '2 - 1',
        label: 'Subtract an integer from another',
      },
    ];
    
    export default (props: QueryEditorHelpProps) => {
      return (
        <div>
          <h2>Cheat Sheet</h2>
          {examples.map((item, index) => (
            <div className="cheat-sheet-item" key={index}>
              <div className="cheat-sheet-item__title">{item.title}</div>
              {item.expression ? (
                <div
                  className="cheat-sheet-item__example"
                  onClick={(e) => props.onClickExample({ refId: 'A', queryText: item.expression } as DataQuery)}
                >
                  <code>{item.expression}</code>
                </div>
              ) : null}
              <div className="cheat-sheet-item__label">{item.label}</div>
            </div>
          ))}
        </div>
      );
    };