TimeRangeOptions.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, { PureComponent } from 'react';
  2. import { Switch } from 'app/core/components/Switch/Switch';
  3. import { Input } from 'app/core/components/Form';
  4. import { isValidTimeSpan } from 'app/core/utils/rangeutil';
  5. import { ValidationEvents } from 'app/types';
  6. import { EventsWithValidation } from 'app/core/components/Form/Input';
  7. import { PanelModel } from '../panel_model';
  8. import { InputStatus } from 'app/core/components/Form/Input';
  9. const timeRangeValidationEvents: ValidationEvents = {
  10. [EventsWithValidation.onBlur]: [
  11. {
  12. rule: value => {
  13. if (!value) {
  14. return true;
  15. }
  16. return isValidTimeSpan(value);
  17. },
  18. errorMessage: 'Not a valid timespan',
  19. },
  20. ],
  21. };
  22. const emptyToNull = (value: string) => {
  23. return value === '' ? null : value;
  24. };
  25. interface Props {
  26. panel: PanelModel;
  27. }
  28. export class TimeRangeOptions extends PureComponent<Props> {
  29. onOverrideTime = (evt, status: InputStatus) => {
  30. const { value } = evt.target;
  31. const { panel } = this.props;
  32. const emptyToNullValue = emptyToNull(value);
  33. if (status === InputStatus.Valid && panel.timeFrom !== emptyToNullValue) {
  34. panel.timeFrom = emptyToNullValue;
  35. panel.refresh();
  36. }
  37. };
  38. onTimeShift = (evt, status: InputStatus) => {
  39. const { value } = evt.target;
  40. const { panel } = this.props;
  41. const emptyToNullValue = emptyToNull(value);
  42. if (status === InputStatus.Valid && panel.timeShift !== emptyToNullValue) {
  43. panel.timeShift = emptyToNullValue;
  44. panel.refresh();
  45. }
  46. };
  47. onToggleTimeOverride = () => {
  48. const { panel } = this.props;
  49. panel.hideTimeOverride = !panel.hideTimeOverride;
  50. panel.refresh();
  51. };
  52. render = () => {
  53. const hideTimeOverride = this.props.panel.hideTimeOverride;
  54. return (
  55. <>
  56. <h5 className="section-heading">Time Range</h5>
  57. <div className="gf-form-group">
  58. <div className="gf-form">
  59. <span className="gf-form-label width-12">Override relative time</span>
  60. <Input
  61. type="text"
  62. className="gf-form-input max-width-8"
  63. placeholder="1h"
  64. onBlur={this.onOverrideTime}
  65. validationEvents={timeRangeValidationEvents}
  66. hideErrorMessage={true}
  67. />
  68. </div>
  69. <div className="gf-form">
  70. <span className="gf-form-label width-12">Add time shift</span>
  71. <Input
  72. type="text"
  73. className="gf-form-input max-width-8"
  74. placeholder="1h"
  75. onBlur={this.onTimeShift}
  76. validationEvents={timeRangeValidationEvents}
  77. hideErrorMessage={true}
  78. />
  79. </div>
  80. <div className="gf-form-inline">
  81. <Switch label="Hide time override info" checked={hideTimeOverride} onChange={this.onToggleTimeOverride} />
  82. </div>
  83. </div>
  84. </>
  85. );
  86. };
  87. }