TextDimensionEditor.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import React, { FC, useCallback } from 'react';
  2. import {
  3. FieldNamePickerConfigSettings,
  4. StandardEditorProps,
  5. StandardEditorsRegistryItem,
  6. StringFieldConfigSettings,
  7. } from '@grafana/data';
  8. import { Button, InlineField, InlineFieldRow, RadioButtonGroup, StringValueEditor } from '@grafana/ui';
  9. import { FieldNamePicker } from '../../../../../packages/grafana-ui/src/components/MatchersUI/FieldNamePicker';
  10. import { TextDimensionConfig, TextDimensionMode, TextDimensionOptions } from '../types';
  11. const textOptions = [
  12. { label: 'Fixed', value: TextDimensionMode.Fixed, description: 'Fixed value' },
  13. { label: 'Field', value: TextDimensionMode.Field, description: 'Display field value' },
  14. // { label: 'Template', value: TextDimensionMode.Template, description: 'use template text' },
  15. ];
  16. const dummyFieldSettings: StandardEditorsRegistryItem<string, FieldNamePickerConfigSettings> = {
  17. settings: {},
  18. } as any;
  19. const dummyStringSettings: StandardEditorsRegistryItem<string, StringFieldConfigSettings> = {
  20. settings: {},
  21. } as any;
  22. export const TextDimensionEditor: FC<StandardEditorProps<TextDimensionConfig, TextDimensionOptions, any>> = (props) => {
  23. const { value, context, onChange } = props;
  24. const labelWidth = 9;
  25. const onModeChange = useCallback(
  26. (mode) => {
  27. onChange({
  28. ...value,
  29. mode,
  30. });
  31. },
  32. [onChange, value]
  33. );
  34. const onFieldChange = useCallback(
  35. (field) => {
  36. onChange({
  37. ...value,
  38. field,
  39. });
  40. },
  41. [onChange, value]
  42. );
  43. const onFixedChange = useCallback(
  44. (fixed) => {
  45. onChange({
  46. ...value,
  47. fixed,
  48. });
  49. },
  50. [onChange, value]
  51. );
  52. const onClearFixedText = () => {
  53. // Need to first change to field in order to clear fixed value in editor
  54. onChange({ mode: TextDimensionMode.Field, fixed: '', field: '' });
  55. onChange({ mode: TextDimensionMode.Fixed, fixed: '', field: '' });
  56. };
  57. const mode = value?.mode ?? TextDimensionMode.Fixed;
  58. return (
  59. <>
  60. <InlineFieldRow>
  61. <InlineField label="Source" labelWidth={labelWidth} grow={true}>
  62. <RadioButtonGroup value={mode} options={textOptions} onChange={onModeChange} fullWidth />
  63. </InlineField>
  64. </InlineFieldRow>
  65. {mode !== TextDimensionMode.Fixed && (
  66. <InlineFieldRow>
  67. <InlineField label="Field" labelWidth={labelWidth} grow={true}>
  68. <FieldNamePicker
  69. context={context}
  70. value={value.field ?? ''}
  71. onChange={onFieldChange}
  72. item={dummyFieldSettings}
  73. />
  74. </InlineField>
  75. </InlineFieldRow>
  76. )}
  77. {mode === TextDimensionMode.Fixed && (
  78. <InlineFieldRow>
  79. <InlineField label={'Value'} labelWidth={labelWidth} grow={true}>
  80. <>
  81. <StringValueEditor
  82. context={context}
  83. value={value?.fixed}
  84. onChange={onFixedChange}
  85. item={dummyStringSettings}
  86. />
  87. {value?.fixed && (
  88. <Button icon="times" variant="secondary" fill="text" size="sm" onClick={onClearFixedText} />
  89. )}
  90. </>
  91. </InlineField>
  92. </InlineFieldRow>
  93. )}
  94. {mode === TextDimensionMode.Template && (
  95. <InlineFieldRow>
  96. <InlineField label="Template" labelWidth={labelWidth} grow={true}>
  97. <StringValueEditor // This could be a code editor
  98. context={context}
  99. value={value?.fixed}
  100. onChange={onFixedChange}
  101. item={dummyStringSettings}
  102. />
  103. </InlineField>
  104. </InlineFieldRow>
  105. )}
  106. </>
  107. );
  108. };