FillBelowToEditor.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import React, { useMemo } from 'react';
  2. import { FieldOverrideEditorProps, FieldType, getFieldDisplayName, SelectableValue } from '@grafana/data';
  3. import { Select } from '@grafana/ui';
  4. export const FillBellowToEditor: React.FC<FieldOverrideEditorProps<string, any>> = ({ value, context, onChange }) => {
  5. const names = useMemo(() => {
  6. const names: Array<SelectableValue<string>> = [];
  7. if (context.data.length) {
  8. for (const frame of context.data) {
  9. for (const field of frame.fields) {
  10. if (field.type === FieldType.number) {
  11. const label = getFieldDisplayName(field, frame, context.data);
  12. names.push({
  13. label,
  14. value: label,
  15. });
  16. }
  17. }
  18. }
  19. }
  20. return names;
  21. }, [context]);
  22. const current = useMemo(() => {
  23. const found = names.find((v) => v.value === value);
  24. if (found) {
  25. return found;
  26. }
  27. if (value) {
  28. return {
  29. label: value,
  30. value,
  31. };
  32. }
  33. return undefined;
  34. }, [names, value]);
  35. return (
  36. <Select
  37. options={names}
  38. value={current}
  39. onChange={(v) => {
  40. onChange(v.value);
  41. }}
  42. />
  43. );
  44. };