button.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { PureComponent } from 'react';
  2. import { Button } from '@grafana/ui';
  3. import { DimensionContext } from 'app/features/dimensions/context';
  4. import { TextDimensionEditor } from 'app/features/dimensions/editors/TextDimensionEditor';
  5. import { TextDimensionConfig } from 'app/features/dimensions/types';
  6. import { APIEditor, APIEditorConfig, callApi } from 'app/plugins/panel/canvas/editor/APIEditor';
  7. import { CanvasElementItem, CanvasElementProps } from '../element';
  8. interface ButtonData {
  9. text?: string;
  10. api?: APIEditorConfig;
  11. }
  12. interface ButtonConfig {
  13. text?: TextDimensionConfig;
  14. api?: APIEditorConfig;
  15. }
  16. class ButtonDisplay extends PureComponent<CanvasElementProps<ButtonConfig, ButtonData>> {
  17. render() {
  18. const { data } = this.props;
  19. const onClick = () => {
  20. if (data?.api) {
  21. callApi(data.api);
  22. }
  23. };
  24. return (
  25. <Button type="submit" onClick={onClick}>
  26. {data?.text}
  27. </Button>
  28. );
  29. }
  30. }
  31. export const buttonItem: CanvasElementItem<ButtonConfig, ButtonData> = {
  32. id: 'button',
  33. name: 'Button',
  34. description: 'Button',
  35. display: ButtonDisplay,
  36. defaultSize: {
  37. width: 200,
  38. height: 50,
  39. },
  40. getNewOptions: (options) => ({
  41. ...options,
  42. }),
  43. // Called when data changes
  44. prepareData: (ctx: DimensionContext, cfg: ButtonConfig) => {
  45. const data: ButtonData = {
  46. text: cfg?.text ? ctx.getText(cfg.text).value() : '',
  47. api: cfg?.api ?? undefined,
  48. };
  49. return data;
  50. },
  51. // Heatmap overlay options
  52. registerOptionsUI: (builder) => {
  53. const category = ['Button'];
  54. builder
  55. .addCustomEditor({
  56. category,
  57. id: 'textSelector',
  58. path: 'config.text',
  59. name: 'Text',
  60. editor: TextDimensionEditor,
  61. })
  62. .addCustomEditor({
  63. category,
  64. id: 'apiSelector',
  65. path: 'config.api',
  66. name: 'API',
  67. editor: APIEditor,
  68. });
  69. },
  70. };