PickerRenderer.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React, { FunctionComponent, PropsWithChildren, ReactElement, useMemo } from 'react';
  2. import { selectors } from '@grafana/e2e-selectors';
  3. import { Tooltip } from '@grafana/ui';
  4. import { variableAdapters } from '../adapters';
  5. import { VariableHide, VariableModel } from '../types';
  6. interface Props {
  7. variable: VariableModel;
  8. }
  9. export const PickerRenderer: FunctionComponent<Props> = (props) => {
  10. const PickerToRender = useMemo(() => variableAdapters.get(props.variable.type).picker, [props.variable]);
  11. if (!props.variable) {
  12. return <div>Couldn&apos;t load variable</div>;
  13. }
  14. return (
  15. <div className="gf-form">
  16. <PickerLabel variable={props.variable} />
  17. {props.variable.hide !== VariableHide.hideVariable && PickerToRender && (
  18. <PickerToRender variable={props.variable} />
  19. )}
  20. </div>
  21. );
  22. };
  23. function PickerLabel({ variable }: PropsWithChildren<Props>): ReactElement | null {
  24. const labelOrName = useMemo(() => variable.label || variable.name, [variable]);
  25. if (variable.hide !== VariableHide.dontHide) {
  26. return null;
  27. }
  28. const elementId = `var-${variable.id}`;
  29. if (variable.description) {
  30. return (
  31. <Tooltip content={variable.description} placement={'bottom'}>
  32. <label
  33. className="gf-form-label gf-form-label--variable"
  34. data-testid={selectors.pages.Dashboard.SubMenu.submenuItemLabels(labelOrName)}
  35. htmlFor={elementId}
  36. >
  37. {labelOrName}
  38. </label>
  39. </Tooltip>
  40. );
  41. }
  42. return (
  43. <label
  44. className="gf-form-label gf-form-label--variable"
  45. data-testid={selectors.pages.Dashboard.SubMenu.submenuItemLabels(labelOrName)}
  46. htmlFor={elementId}
  47. >
  48. {labelOrName}
  49. </label>
  50. );
  51. }