FieldDisplayEditor.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Libraries
  2. import React, { PureComponent, ChangeEvent } from 'react';
  3. // Components
  4. import { PanelOptionsGroup } from '../PanelOptionsGroup/PanelOptionsGroup';
  5. import { FormLabel } from '../FormLabel/FormLabel';
  6. import { FormField } from '../FormField/FormField';
  7. import { StatsPicker } from '../StatsPicker/StatsPicker';
  8. // Types
  9. import { FieldDisplayOptions, DEFAULT_FIELD_DISPLAY_VALUES_LIMIT } from '../../utils/fieldDisplay';
  10. import { Field } from '../../types/data';
  11. import Select, { SelectOptionItem } from '../Select/Select';
  12. import { toNumberString, toIntegerOrUndefined } from '../../utils';
  13. import { ReducerID } from '../../utils/fieldReducer';
  14. const showOptions: Array<SelectOptionItem<boolean>> = [
  15. {
  16. value: true,
  17. label: 'All Values',
  18. description: 'Each row in the response data',
  19. },
  20. {
  21. value: false,
  22. label: 'Calculation',
  23. description: 'Calculate a value based on the response',
  24. },
  25. ];
  26. export interface Props {
  27. options: FieldDisplayOptions;
  28. onChange: (valueOptions: FieldDisplayOptions) => void;
  29. labelWidth?: number;
  30. children?: JSX.Element[];
  31. }
  32. export class FieldDisplayEditor extends PureComponent<Props> {
  33. onShowValuesChange = (item: SelectOptionItem<boolean>) => {
  34. const val = item.value === true;
  35. this.props.onChange({ ...this.props.options, values: val });
  36. };
  37. onCalcsChange = (calcs: string[]) => {
  38. this.props.onChange({ ...this.props.options, calcs });
  39. };
  40. onDefaultsChange = (value: Partial<Field>) => {
  41. this.props.onChange({ ...this.props.options, defaults: value });
  42. };
  43. onLimitChange = (event: ChangeEvent<HTMLInputElement>) => {
  44. this.props.onChange({
  45. ...this.props.options,
  46. limit: toIntegerOrUndefined(event.target.value),
  47. });
  48. };
  49. render() {
  50. const { options, children } = this.props;
  51. const { calcs, values, limit } = options;
  52. const labelWidth = this.props.labelWidth || 5;
  53. return (
  54. <PanelOptionsGroup title="Display">
  55. <>
  56. <div className="gf-form">
  57. <FormLabel width={labelWidth}>Show</FormLabel>
  58. <Select
  59. options={showOptions}
  60. value={values ? showOptions[0] : showOptions[1]}
  61. onChange={this.onShowValuesChange}
  62. />
  63. </div>
  64. {values ? (
  65. <FormField
  66. label="Limit"
  67. labelWidth={labelWidth}
  68. placeholder={`${DEFAULT_FIELD_DISPLAY_VALUES_LIMIT}`}
  69. onChange={this.onLimitChange}
  70. value={toNumberString(limit)}
  71. type="number"
  72. />
  73. ) : (
  74. <div className="gf-form">
  75. <FormLabel width={labelWidth}>Calc</FormLabel>
  76. <StatsPicker
  77. width={12}
  78. placeholder="Choose Stat"
  79. defaultStat={ReducerID.mean}
  80. allowMultiple={false}
  81. stats={calcs}
  82. onChange={this.onCalcsChange}
  83. />
  84. </div>
  85. )}
  86. {children}
  87. </>
  88. </PanelOptionsGroup>
  89. );
  90. }
  91. }