ValueOptions.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import React, { PureComponent } from 'react';
  2. import { Label } from 'app/core/components/Label/Label';
  3. import SimplePicker from 'app/core/components/Picker/SimplePicker';
  4. import UnitPicker from 'app/core/components/Picker/Unit/UnitPicker';
  5. import { OptionsProps } from './Options';
  6. const statOptions = [
  7. { value: 'min', text: 'Min' },
  8. { value: 'max', text: 'Max' },
  9. { value: 'avg', text: 'Average' },
  10. { value: 'current', text: 'Current' },
  11. { value: 'total', text: 'Total' },
  12. { value: 'name', text: 'Name' },
  13. { value: 'first', text: 'First' },
  14. { value: 'delta', text: 'Delta' },
  15. { value: 'diff', text: 'Difference' },
  16. { value: 'range', text: 'Range' },
  17. { value: 'last_time', text: 'Time of last point' },
  18. ];
  19. const labelWidth = 6;
  20. interface Props {
  21. onChange: (arg: any) => void;
  22. options: OptionsProps;
  23. }
  24. export default class ValueOptions extends PureComponent<Props> {
  25. onUnitChange = unit => this.props.onChange({ ...this.props.options, unit: unit.value });
  26. onStatChange = stat => this.props.onChange({ ...this.props.options, stat: stat.value });
  27. onDecimalChange = event => {
  28. if (!isNaN(event.target.value)) {
  29. this.props.onChange({ ...this.props.options, decimals: event.target.value });
  30. }
  31. };
  32. onPrefixChange = event => this.props.onChange({ ...this.props.options, prefix: event.target.value });
  33. onSuffixChange = event => this.props.onChange({ ...this.props.options, suffix: event.target.value });
  34. render() {
  35. const { stat, unit, decimals, prefix, suffix } = this.props.options;
  36. return (
  37. <div className="section gf-form-group">
  38. <h5 className="page-heading">Value</h5>
  39. <div className="gf-form-inline">
  40. <Label width={labelWidth}>Stat</Label>
  41. <SimplePicker
  42. width={12}
  43. options={statOptions}
  44. getOptionLabel={i => i.text}
  45. getOptionValue={i => i.value}
  46. onSelected={this.onStatChange}
  47. value={statOptions.find(option => option.value === stat)}
  48. />
  49. </div>
  50. <div className="gf-form-inline">
  51. <Label width={labelWidth}>Unit</Label>
  52. <UnitPicker defaultValue={unit} onSelected={value => this.onUnitChange(value)} />
  53. </div>
  54. <div className="gf-form-inline">
  55. <Label width={labelWidth}>Decimals</Label>
  56. <input
  57. className="gf-form-input width-12"
  58. type="number"
  59. placeholder="auto"
  60. value={decimals || ''}
  61. onChange={this.onDecimalChange}
  62. />
  63. </div>
  64. <div className="gf-form-inline">
  65. <Label width={labelWidth}>Prefix</Label>
  66. <input className="gf-form-input width-12" type="text" value={prefix || ''} onChange={this.onPrefixChange} />
  67. </div>
  68. <div className="gf-form-inline">
  69. <Label width={labelWidth}>Suffix</Label>
  70. <input className="gf-form-input width-12" type="text" value={suffix || ''} onChange={this.onSuffixChange} />
  71. </div>
  72. </div>
  73. );
  74. }
  75. }