MetricPicker.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { css, cx } from '@emotion/css';
  2. import React from 'react';
  3. import { SelectableValue } from '@grafana/data';
  4. import { Segment } from '@grafana/ui';
  5. import { describeMetric } from '../utils';
  6. import { MetricAggregation } from './QueryEditor/MetricAggregationsEditor/aggregations';
  7. const noWrap = css`
  8. white-space: nowrap;
  9. `;
  10. const toOption = (metric: MetricAggregation) => ({
  11. label: describeMetric(metric),
  12. value: metric,
  13. });
  14. const toOptions = (metrics: MetricAggregation[]): Array<SelectableValue<MetricAggregation>> => metrics.map(toOption);
  15. interface Props {
  16. options: MetricAggregation[];
  17. onChange: (e: SelectableValue<MetricAggregation>) => void;
  18. className?: string;
  19. value?: string;
  20. }
  21. export const MetricPicker = ({ options, onChange, className, value }: Props) => {
  22. const selectedOption = options.find((option) => option.id === value);
  23. return (
  24. <Segment
  25. className={cx(className, noWrap)}
  26. options={toOptions(options)}
  27. onChange={onChange}
  28. placeholder="Select Metric"
  29. value={!!selectedOption ? toOption(selectedOption) : undefined}
  30. />
  31. );
  32. };