TimeRangeOptions.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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">
  60. <i className="fa fa-clock-o" />
  61. </span>
  62. <span className="gf-form-label width-12">Override relative time</span>
  63. <span className="gf-form-label width-6">Last</span>
  64. <Input
  65. type="text"
  66. className="gf-form-input max-width-8"
  67. placeholder="1h"
  68. onBlur={this.onOverrideTime}
  69. validationEvents={timeRangeValidationEvents}
  70. hideErrorMessage={true}
  71. />
  72. </div>
  73. <div className="gf-form">
  74. <span className="gf-form-label">
  75. <i className="fa fa-clock-o" />
  76. </span>
  77. <span className="gf-form-label width-12">Add time shift</span>
  78. <span className="gf-form-label width-6">Amount</span>
  79. <Input
  80. type="text"
  81. className="gf-form-input max-width-8"
  82. placeholder="1h"
  83. onBlur={this.onTimeShift}
  84. validationEvents={timeRangeValidationEvents}
  85. hideErrorMessage={true}
  86. />
  87. </div>
  88. <div className="gf-form-inline">
  89. <div className="gf-form">
  90. <span className="gf-form-label">
  91. <i className="fa fa-clock-o" />
  92. </span>
  93. </div>
  94. <Switch label="Hide time override info" checked={hideTimeOverride} onChange={this.onToggleTimeOverride} />
  95. </div>
  96. </div>
  97. </>
  98. );
  99. };
  100. }