SingleStatValueEditor.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Components
  4. import { FormField, FormLabel, PanelOptionsGroup, Select, UnitPicker } from '@grafana/ui';
  5. // Types
  6. import { SingleStatValueOptions } from './types';
  7. const statOptions = [
  8. { value: 'min', label: 'Min' },
  9. { value: 'max', label: 'Max' },
  10. { value: 'avg', label: 'Average' },
  11. { value: 'current', label: 'Current' },
  12. { value: 'total', label: 'Total' },
  13. { value: 'name', label: 'Name' },
  14. { value: 'first', label: 'First' },
  15. { value: 'delta', label: 'Delta' },
  16. { value: 'diff', label: 'Difference' },
  17. { value: 'range', label: 'Range' },
  18. { value: 'last_time', label: 'Time of last point' },
  19. ];
  20. const labelWidth = 6;
  21. export interface Props {
  22. options: SingleStatValueOptions;
  23. onChange: (valueOptions: SingleStatValueOptions) => void;
  24. }
  25. export class SingleStatValueEditor extends PureComponent<Props> {
  26. onUnitChange = unit => this.props.onChange({ ...this.props.options, unit: unit.value });
  27. onStatChange = stat => this.props.onChange({ ...this.props.options, stat: stat.value });
  28. onDecimalChange = event => {
  29. if (!isNaN(event.target.value)) {
  30. this.props.onChange({
  31. ...this.props.options,
  32. decimals: parseInt(event.target.value, 10),
  33. });
  34. } else {
  35. this.props.onChange({
  36. ...this.props.options,
  37. decimals: null,
  38. });
  39. }
  40. };
  41. onPrefixChange = event => this.props.onChange({ ...this.props.options, prefix: event.target.value });
  42. onSuffixChange = event => this.props.onChange({ ...this.props.options, suffix: event.target.value });
  43. render() {
  44. const { stat, unit, decimals, prefix, suffix } = this.props.options;
  45. let decimalsString = '';
  46. if (Number.isFinite(decimals)) {
  47. decimalsString = decimals.toString();
  48. }
  49. return (
  50. <PanelOptionsGroup title="Value">
  51. <div className="gf-form">
  52. <FormLabel width={labelWidth}>Stat</FormLabel>
  53. <Select
  54. width={12}
  55. options={statOptions}
  56. onChange={this.onStatChange}
  57. value={statOptions.find(option => option.value === stat)}
  58. />
  59. </div>
  60. <div className="gf-form">
  61. <FormLabel width={labelWidth}>Unit</FormLabel>
  62. <UnitPicker defaultValue={unit} onChange={this.onUnitChange} />
  63. </div>
  64. <FormField
  65. label="Decimals"
  66. labelWidth={labelWidth}
  67. placeholder="auto"
  68. onChange={this.onDecimalChange}
  69. value={decimalsString}
  70. type="number"
  71. />
  72. <FormField label="Prefix" labelWidth={labelWidth} onChange={this.onPrefixChange} value={prefix || ''} />
  73. <FormField label="Suffix" labelWidth={labelWidth} onChange={this.onSuffixChange} value={suffix || ''} />
  74. </PanelOptionsGroup>
  75. );
  76. }
  77. }