SingleStatValueEditor.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. console.log('SELECTED', stats);
  16. const stat = stats[0] || StatID.mean;
  17. this.props.onChange({ ...this.props.options, stat });
  18. };
  19. onDecimalChange = event => {
  20. if (!isNaN(event.target.value)) {
  21. this.props.onChange({
  22. ...this.props.options,
  23. decimals: parseInt(event.target.value, 10),
  24. });
  25. } else {
  26. this.props.onChange({
  27. ...this.props.options,
  28. decimals: null,
  29. });
  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. let decimalsString = '';
  37. if (Number.isFinite(decimals)) {
  38. decimalsString = decimals.toString();
  39. }
  40. console.log('xxx', stat);
  41. return (
  42. <PanelOptionsGroup title="Value">
  43. <div className="gf-form">
  44. <FormLabel width={labelWidth}>Stat</FormLabel>
  45. <StatsPicker
  46. width={12}
  47. placeholder="Choose Stat"
  48. defaultStat={StatID.mean}
  49. allowMultiple={false}
  50. stats={[stat]}
  51. onChange={this.onStatsChange}
  52. />
  53. </div>
  54. <div className="gf-form">
  55. <FormLabel width={labelWidth}>Unit</FormLabel>
  56. <UnitPicker defaultValue={unit} onChange={this.onUnitChange} />
  57. </div>
  58. <FormField
  59. label="Decimals"
  60. labelWidth={labelWidth}
  61. placeholder="auto"
  62. onChange={this.onDecimalChange}
  63. value={decimalsString}
  64. type="number"
  65. />
  66. <FormField label="Prefix" labelWidth={labelWidth} onChange={this.onPrefixChange} value={prefix || ''} />
  67. <FormField label="Suffix" labelWidth={labelWidth} onChange={this.onSuffixChange} value={suffix || ''} />
  68. </PanelOptionsGroup>
  69. );
  70. }
  71. }