SingleStatValueEditor.tsx 2.7 KB

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