IntervalVariableEditor.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { ChangeEvent, FormEvent, PureComponent } from 'react';
  2. import { SelectableValue } from '@grafana/data';
  3. import { selectors } from '@grafana/e2e-selectors';
  4. import { InlineFieldRow, VerticalGroup } from '@grafana/ui';
  5. import { VariableSectionHeader } from '../editor/VariableSectionHeader';
  6. import { VariableSelectField } from '../editor/VariableSelectField';
  7. import { VariableSwitchField } from '../editor/VariableSwitchField';
  8. import { VariableTextField } from '../editor/VariableTextField';
  9. import { VariableEditorProps } from '../editor/types';
  10. import { IntervalVariableModel } from '../types';
  11. export interface Props extends VariableEditorProps<IntervalVariableModel> {}
  12. export class IntervalVariableEditor extends PureComponent<Props> {
  13. onAutoChange = (event: ChangeEvent<HTMLInputElement>) => {
  14. this.props.onPropChange({
  15. propName: 'auto',
  16. propValue: event.target.checked,
  17. updateOptions: true,
  18. });
  19. };
  20. onQueryChanged = (event: FormEvent<HTMLInputElement>) => {
  21. this.props.onPropChange({
  22. propName: 'query',
  23. propValue: event.currentTarget.value,
  24. });
  25. };
  26. onQueryBlur = (event: FormEvent<HTMLInputElement>) => {
  27. this.props.onPropChange({
  28. propName: 'query',
  29. propValue: event.currentTarget.value,
  30. updateOptions: true,
  31. });
  32. };
  33. onAutoCountChanged = (option: SelectableValue<number>) => {
  34. this.props.onPropChange({
  35. propName: 'auto_count',
  36. propValue: option.value,
  37. updateOptions: true,
  38. });
  39. };
  40. onAutoMinChanged = (event: FormEvent<HTMLInputElement>) => {
  41. this.props.onPropChange({
  42. propName: 'auto_min',
  43. propValue: event.currentTarget.value,
  44. updateOptions: true,
  45. });
  46. };
  47. render() {
  48. const { variable } = this.props;
  49. const stepOptions = [1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 100, 200, 300, 400, 500].map((count) => ({
  50. label: `${count}`,
  51. value: count,
  52. }));
  53. const stepValue = stepOptions.find((o) => o.value === variable.auto_count) ?? stepOptions[0];
  54. return (
  55. <VerticalGroup spacing="xs">
  56. <VariableSectionHeader name="Interval options" />
  57. <VerticalGroup spacing="none">
  58. <VariableTextField
  59. value={this.props.variable.query}
  60. name="Values"
  61. placeholder="1m,10m,1h,6h,1d,7d"
  62. onChange={this.onQueryChanged}
  63. onBlur={this.onQueryBlur}
  64. labelWidth={20}
  65. testId={selectors.pages.Dashboard.Settings.Variables.Edit.IntervalVariable.intervalsValueInput}
  66. grow
  67. required
  68. />
  69. <InlineFieldRow>
  70. <VariableSwitchField
  71. value={this.props.variable.auto}
  72. name="Auto option"
  73. tooltip="Dynamically calculates interval by dividing time range by the count specified."
  74. onChange={this.onAutoChange}
  75. />
  76. {this.props.variable.auto ? (
  77. <>
  78. <VariableSelectField
  79. name="Step count"
  80. value={stepValue}
  81. options={stepOptions}
  82. onChange={this.onAutoCountChanged}
  83. tooltip="How many times the current time range should be divided to calculate the value."
  84. labelWidth={7}
  85. width={9}
  86. />
  87. <VariableTextField
  88. value={this.props.variable.auto_min}
  89. name="Min interval"
  90. placeholder="10s"
  91. onChange={this.onAutoMinChanged}
  92. tooltip="The calculated value will not go below this threshold."
  93. labelWidth={13}
  94. width={11}
  95. />
  96. </>
  97. ) : null}
  98. </InlineFieldRow>
  99. </VerticalGroup>
  100. </VerticalGroup>
  101. );
  102. }
  103. }