SpanNullsEditor.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React from 'react';
  2. import { FieldOverrideEditorProps, rangeUtil, SelectableValue } from '@grafana/data';
  3. import { HorizontalGroup, Input, RadioButtonGroup } from '@grafana/ui';
  4. const GAPS_OPTIONS: Array<SelectableValue<boolean | number>> = [
  5. {
  6. label: 'Never',
  7. value: false,
  8. },
  9. {
  10. label: 'Always',
  11. value: true,
  12. },
  13. {
  14. label: 'Threshold',
  15. value: 3600000, // 1h
  16. },
  17. ];
  18. export const SpanNullsEditor: React.FC<FieldOverrideEditorProps<boolean | number, any>> = ({ value, onChange }) => {
  19. const isThreshold = typeof value === 'number';
  20. const formattedTime = isThreshold ? rangeUtil.secondsToHms((value as number) / 1000) : undefined;
  21. GAPS_OPTIONS[2].value = isThreshold ? (value as number) : 3600000; // 1h
  22. const checkAndUpdate = (txt: string) => {
  23. let val: boolean | number = false;
  24. if (txt) {
  25. try {
  26. val = rangeUtil.intervalToSeconds(txt) * 1000;
  27. } catch (err) {
  28. console.warn('ERROR', err);
  29. }
  30. }
  31. onChange(val);
  32. };
  33. const handleEnterKey = (e: React.KeyboardEvent<HTMLInputElement>) => {
  34. if (e.key !== 'Enter') {
  35. return;
  36. }
  37. checkAndUpdate((e.target as any).value);
  38. };
  39. const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
  40. checkAndUpdate(e.target.value);
  41. };
  42. return (
  43. <HorizontalGroup>
  44. <RadioButtonGroup value={value} options={GAPS_OPTIONS} onChange={onChange} />
  45. {isThreshold && (
  46. <Input
  47. autoFocus={false}
  48. placeholder="never"
  49. width={10}
  50. defaultValue={formattedTime}
  51. onKeyDown={handleEnterKey}
  52. onBlur={handleBlur}
  53. prefix={<div>&lt;</div>}
  54. spellCheck={false}
  55. />
  56. )}
  57. </HorizontalGroup>
  58. );
  59. };