SingleStatValueEditor.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Components
  4. import { FormField, FormLabel, PanelOptionsGroup, StatsPicker, UnitPicker, StatID } from '@grafana/ui';
  5. // Types
  6. import { SingleStatValueOptions } from './types';
  7. const labelWidth = 6;
  8. export interface Props {
  9. options: SingleStatValueOptions;
  10. onChange: (valueOptions: SingleStatValueOptions) => void;
  11. }
  12. export class SingleStatValueEditor extends PureComponent<Props> {
  13. onUnitChange = unit => this.props.onChange({ ...this.props.options, unit: unit.value });
  14. onStatsChange = stats => {
  15. const stat = stats[0] || StatID.mean;
  16. this.props.onChange({ ...this.props.options, stat });
  17. };
  18. onDecimalChange = event => {
  19. if (!isNaN(event.target.value)) {
  20. this.props.onChange({
  21. ...this.props.options,
  22. decimals: parseInt(event.target.value, 10),
  23. });
  24. } else {
  25. this.props.onChange({
  26. ...this.props.options,
  27. decimals: null,
  28. });
  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. let decimalsString = '';
  36. if (Number.isFinite(decimals)) {
  37. decimalsString = decimals.toString();
  38. }
  39. return (
  40. <PanelOptionsGroup title="Value">
  41. <div className="gf-form">
  42. <FormLabel width={labelWidth}>Show</FormLabel>
  43. <StatsPicker
  44. width={12}
  45. placeholder="Choose Stat"
  46. defaultStat={StatID.mean}
  47. allowMultiple={false}
  48. stats={[stat]}
  49. onChange={this.onStatsChange}
  50. />
  51. </div>
  52. <div className="gf-form">
  53. <FormLabel width={labelWidth}>Unit</FormLabel>
  54. <UnitPicker defaultValue={unit} onChange={this.onUnitChange} />
  55. </div>
  56. <FormField
  57. label="Decimals"
  58. labelWidth={labelWidth}
  59. placeholder="auto"
  60. onChange={this.onDecimalChange}
  61. value={decimalsString}
  62. type="number"
  63. />
  64. <FormField label="Prefix" labelWidth={labelWidth} onChange={this.onPrefixChange} value={prefix || ''} />
  65. <FormField label="Suffix" labelWidth={labelWidth} onChange={this.onSuffixChange} value={suffix || ''} />
  66. </PanelOptionsGroup>
  67. );
  68. }
  69. }