ValueOptions.tsx 2.4 KB

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