ViewControls.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { css } from '@emotion/css';
  2. import React, { useState } from 'react';
  3. import { Button, HorizontalGroup, useStyles, VerticalGroup } from '@grafana/ui';
  4. function getStyles() {
  5. return {
  6. wrapper: css`
  7. label: wrapper;
  8. pointer-events: all;
  9. `,
  10. };
  11. }
  12. interface Props<Config> {
  13. config: Config;
  14. onConfigChange: (config: Config) => void;
  15. onPlus: () => void;
  16. onMinus: () => void;
  17. scale: number;
  18. disableZoomOut?: boolean;
  19. disableZoomIn?: boolean;
  20. }
  21. /**
  22. * Control buttons for zoom but also some layout config inputs mainly for debugging.
  23. */
  24. export function ViewControls<Config extends Record<string, any>>(props: Props<Config>) {
  25. const { config, onConfigChange, onPlus, onMinus, disableZoomOut, disableZoomIn } = props;
  26. const [showConfig, setShowConfig] = useState(false);
  27. // For debugging the layout, should be removed here and maybe moved to panel config later on
  28. const allowConfiguration = false;
  29. const styles = useStyles(getStyles);
  30. return (
  31. <div className={styles.wrapper}>
  32. <VerticalGroup spacing="sm">
  33. <HorizontalGroup spacing="xs">
  34. <Button
  35. icon={'plus-circle'}
  36. onClick={onPlus}
  37. size={'md'}
  38. title={'Zoom in'}
  39. variant="secondary"
  40. disabled={disableZoomIn}
  41. />
  42. <Button
  43. icon={'minus-circle'}
  44. onClick={onMinus}
  45. size={'md'}
  46. title={'Zoom out'}
  47. variant="secondary"
  48. disabled={disableZoomOut}
  49. />
  50. </HorizontalGroup>
  51. <HorizontalGroup spacing="xs">
  52. <Button
  53. icon={'code-branch'}
  54. onClick={() => onConfigChange({ ...config, gridLayout: false })}
  55. size={'md'}
  56. title={'Default layout'}
  57. variant="secondary"
  58. disabled={!config.gridLayout}
  59. />
  60. <Button
  61. icon={'apps'}
  62. onClick={() => onConfigChange({ ...config, gridLayout: true })}
  63. size={'md'}
  64. title={'Grid layout'}
  65. variant="secondary"
  66. disabled={config.gridLayout}
  67. />
  68. </HorizontalGroup>
  69. </VerticalGroup>
  70. {allowConfiguration && (
  71. <Button size={'xs'} fill="text" onClick={() => setShowConfig((showConfig) => !showConfig)}>
  72. {showConfig ? 'Hide config' : 'Show config'}
  73. </Button>
  74. )}
  75. {allowConfiguration &&
  76. showConfig &&
  77. Object.keys(config)
  78. .filter((k) => k !== 'show')
  79. .map((k) => (
  80. <div key={k}>
  81. {k}
  82. <input
  83. style={{ width: 50 }}
  84. type={'number'}
  85. value={config[k]}
  86. onChange={(e) => {
  87. onConfigChange({ ...config, [k]: parseFloat(e.target.value) });
  88. }}
  89. />
  90. </div>
  91. ))}
  92. </div>
  93. );
  94. }