Reduce.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { FC } from 'react';
  2. import { SelectableValue } from '@grafana/data';
  3. import { InlineField, InlineFieldRow, Input, Select } from '@grafana/ui';
  4. import { ExpressionQuery, ExpressionQuerySettings, ReducerMode, reducerMode, reducerTypes } from '../types';
  5. interface Props {
  6. labelWidth: number;
  7. refIds: Array<SelectableValue<string>>;
  8. query: ExpressionQuery;
  9. onChange: (query: ExpressionQuery) => void;
  10. }
  11. export const Reduce: FC<Props> = ({ labelWidth, onChange, refIds, query }) => {
  12. const reducer = reducerTypes.find((o) => o.value === query.reducer);
  13. const onRefIdChange = (value: SelectableValue<string>) => {
  14. onChange({ ...query, expression: value.value });
  15. };
  16. const onSelectReducer = (value: SelectableValue<string>) => {
  17. onChange({ ...query, reducer: value.value });
  18. };
  19. const onSettingsChanged = (settings: ExpressionQuerySettings) => {
  20. onChange({ ...query, settings: settings });
  21. };
  22. const onModeChanged = (value: SelectableValue<ReducerMode>) => {
  23. let newSettings: ExpressionQuerySettings;
  24. switch (value.value) {
  25. case ReducerMode.ReplaceNonNumbers:
  26. let replaceWithNumber = 0;
  27. if (query.settings?.mode === ReducerMode.ReplaceNonNumbers) {
  28. replaceWithNumber = query.settings?.replaceWithValue ?? 0;
  29. }
  30. newSettings = {
  31. mode: ReducerMode.ReplaceNonNumbers,
  32. replaceWithValue: replaceWithNumber,
  33. };
  34. break;
  35. default:
  36. newSettings = {
  37. mode: value.value,
  38. };
  39. }
  40. onSettingsChanged(newSettings);
  41. };
  42. const onReplaceWithChanged = (e: React.FormEvent<HTMLInputElement>) => {
  43. const value = e.currentTarget.valueAsNumber;
  44. onSettingsChanged({ mode: ReducerMode.ReplaceNonNumbers, replaceWithValue: value ?? 0 });
  45. };
  46. const mode = query.settings?.mode ?? ReducerMode.Strict;
  47. const replaceWithNumber = () => {
  48. if (mode !== ReducerMode.ReplaceNonNumbers) {
  49. return;
  50. }
  51. return (
  52. <InlineField label="Replace With" labelWidth={labelWidth}>
  53. <Input type="number" width={10} onChange={onReplaceWithChanged} value={query.settings?.replaceWithValue ?? 0} />
  54. </InlineField>
  55. );
  56. };
  57. return (
  58. <InlineFieldRow>
  59. <InlineField label="Function" labelWidth={labelWidth}>
  60. <Select options={reducerTypes} value={reducer} onChange={onSelectReducer} width={25} />
  61. </InlineField>
  62. <InlineField label="Input" labelWidth={labelWidth}>
  63. <Select onChange={onRefIdChange} options={refIds} value={query.expression} width={20} />
  64. </InlineField>
  65. <InlineField label="Mode" labelWidth={labelWidth}>
  66. <Select onChange={onModeChanged} options={reducerMode} value={mode} width={25} />
  67. </InlineField>
  68. {replaceWithNumber()}
  69. </InlineFieldRow>
  70. );
  71. };