ValueOptions.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 { GaugeOptions } from './types';
  8. import { PanelEditorProps } from '@grafana/ui';
  9. const statOptions = [
  10. { value: 'min', label: 'Min' },
  11. { value: 'max', label: 'Max' },
  12. { value: 'avg', label: 'Average' },
  13. { value: 'current', label: 'Current' },
  14. { value: 'total', label: 'Total' },
  15. { value: 'name', label: 'Name' },
  16. { value: 'first', label: 'First' },
  17. { value: 'delta', label: 'Delta' },
  18. { value: 'diff', label: 'Difference' },
  19. { value: 'range', label: 'Range' },
  20. { value: 'last_time', label: 'Time of last point' },
  21. ];
  22. const labelWidth = 6;
  23. export class ValueOptions extends PureComponent<PanelEditorProps<GaugeOptions>> {
  24. onUnitChange = unit => this.props.onChange({ ...this.props.options, unit: unit.value });
  25. onStatChange = stat => this.props.onChange({ ...this.props.options, stat: stat.value });
  26. onDecimalChange = event => {
  27. if (!isNaN(event.target.value)) {
  28. this.props.onChange({ ...this.props.options, decimals: event.target.value });
  29. }
  30. };
  31. onPrefixChange = event => this.props.onChange({ ...this.props.options, prefix: event.target.value });
  32. onSuffixChange = event => this.props.onChange({ ...this.props.options, suffix: event.target.value });
  33. render() {
  34. const { stat, unit, decimals, prefix, suffix } = this.props.options;
  35. return (
  36. <PanelOptionsGroup title="Value">
  37. <div className="gf-form">
  38. <FormLabel width={labelWidth}>Stat</FormLabel>
  39. <Select
  40. width={12}
  41. options={statOptions}
  42. onChange={this.onStatChange}
  43. value={statOptions.find(option => option.value === stat)}
  44. />
  45. </div>
  46. <div className="gf-form">
  47. <FormLabel width={labelWidth}>Unit</FormLabel>
  48. <UnitPicker defaultValue={unit} onChange={this.onUnitChange} />
  49. </div>
  50. <FormField
  51. label="Decimals"
  52. labelWidth={labelWidth}
  53. placeholder="auto"
  54. onChange={this.onDecimalChange}
  55. value={decimals || ''}
  56. type="number"
  57. />
  58. <FormField label="Prefix" labelWidth={labelWidth} onChange={this.onPrefixChange} value={prefix || ''} />
  59. <FormField label="Suffix" labelWidth={labelWidth} onChange={this.onSuffixChange} value={suffix || ''} />
  60. </PanelOptionsGroup>
  61. );
  62. }
  63. }