VariableSelectField.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { css } from '@emotion/css';
  2. import React, { PropsWithChildren, ReactElement } from 'react';
  3. import { GrafanaTheme, SelectableValue } from '@grafana/data';
  4. import { InlineFormLabel, Select, useStyles } from '@grafana/ui';
  5. import { useUniqueId } from 'app/plugins/datasource/influxdb/components/useUniqueId';
  6. interface VariableSelectFieldProps<T> {
  7. name: string;
  8. value: SelectableValue<T>;
  9. options: Array<SelectableValue<T>>;
  10. onChange: (option: SelectableValue<T>) => void;
  11. tooltip?: string;
  12. testId?: string;
  13. width?: number;
  14. labelWidth?: number;
  15. }
  16. export function VariableSelectField({
  17. name,
  18. value,
  19. options,
  20. tooltip,
  21. onChange,
  22. testId,
  23. width,
  24. labelWidth,
  25. }: PropsWithChildren<VariableSelectFieldProps<any>>): ReactElement {
  26. const styles = useStyles(getStyles);
  27. const uniqueId = useUniqueId();
  28. const inputId = `variable-select-input-${name}-${uniqueId}`;
  29. return (
  30. <>
  31. <InlineFormLabel width={labelWidth ?? 6} tooltip={tooltip} htmlFor={inputId}>
  32. {name}
  33. </InlineFormLabel>
  34. <div data-testid={testId}>
  35. <Select
  36. inputId={inputId}
  37. onChange={onChange}
  38. value={value}
  39. width={width ?? 25}
  40. options={options}
  41. className={styles.selectContainer}
  42. />
  43. </div>
  44. </>
  45. );
  46. }
  47. function getStyles(theme: GrafanaTheme) {
  48. return {
  49. selectContainer: css`
  50. margin-right: ${theme.spacing.xs};
  51. `,
  52. };
  53. }