SingleStatValueEditor.tsx 2.8 KB

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