MappingRow.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React, { PureComponent } from 'react';
  2. import { MappingType, RangeMap, Select, ValueMap } from '@grafana/ui';
  3. import { Label } from 'app/core/components/Label/Label';
  4. interface Props {
  5. mapping: ValueMap | RangeMap;
  6. updateMapping: (mapping) => void;
  7. removeMapping: () => void;
  8. }
  9. interface State {
  10. from: string;
  11. id: number;
  12. operator: string;
  13. text: string;
  14. to: string;
  15. type: MappingType;
  16. value: string;
  17. }
  18. const mappingOptions = [
  19. { value: MappingType.ValueToText, label: 'Value' },
  20. { value: MappingType.RangeToText, label: 'Range' },
  21. ];
  22. export default class MappingRow extends PureComponent<Props, State> {
  23. constructor(props) {
  24. super(props);
  25. this.state = {
  26. ...props.mapping,
  27. };
  28. }
  29. onMappingValueChange = event => {
  30. this.setState({ value: event.target.value });
  31. };
  32. onMappingFromChange = event => {
  33. this.setState({ from: event.target.value });
  34. };
  35. onMappingToChange = event => {
  36. this.setState({ to: event.target.value });
  37. };
  38. onMappingTextChange = event => {
  39. this.setState({ text: event.target.value });
  40. };
  41. onMappingTypeChange = mappingType => {
  42. this.setState({ type: mappingType });
  43. };
  44. updateMapping = () => {
  45. this.props.updateMapping({ ...this.state });
  46. };
  47. renderRow() {
  48. const { from, text, to, type, value } = this.state;
  49. if (type === MappingType.RangeToText) {
  50. return (
  51. <>
  52. <div className="gf-form">
  53. <Label width={4}>From</Label>
  54. <input
  55. className="gf-form-input width-8"
  56. value={from}
  57. onBlur={this.updateMapping}
  58. onChange={this.onMappingFromChange}
  59. />
  60. </div>
  61. <div className="gf-form">
  62. <Label width={4}>To</Label>
  63. <input
  64. className="gf-form-input width-8"
  65. value={to}
  66. onBlur={this.updateMapping}
  67. onChange={this.onMappingToChange}
  68. />
  69. </div>
  70. <div className="gf-form">
  71. <Label width={4}>Text</Label>
  72. <input
  73. className="gf-form-input width-10"
  74. value={text}
  75. onBlur={this.updateMapping}
  76. onChange={this.onMappingTextChange}
  77. />
  78. </div>
  79. </>
  80. );
  81. }
  82. return (
  83. <>
  84. <div className="gf-form">
  85. <Label width={4}>Value</Label>
  86. <input
  87. className="gf-form-input width-8"
  88. onBlur={this.updateMapping}
  89. onChange={this.onMappingValueChange}
  90. value={value}
  91. />
  92. </div>
  93. <div className="gf-form gf-form--grow">
  94. <Label width={4}>Text</Label>
  95. <input
  96. className="gf-form-input"
  97. onBlur={this.updateMapping}
  98. value={text}
  99. onChange={this.onMappingTextChange}
  100. />
  101. </div>
  102. </>
  103. );
  104. }
  105. render() {
  106. const { type } = this.state;
  107. return (
  108. <div className="gf-form-inline">
  109. <div className="gf-form">
  110. <Label width={5}>Type</Label>
  111. <Select
  112. placeholder="Choose type"
  113. isSearchable={false}
  114. options={mappingOptions}
  115. value={mappingOptions.find(o => o.value === type)}
  116. onChange={type => this.onMappingTypeChange(type.value)}
  117. width={7}
  118. />
  119. </div>
  120. {this.renderRow()}
  121. <div className="gf-form">
  122. <button onClick={this.props.removeMapping} className="gf-form-label gf-form-label--btn">
  123. <i className="fa fa-times" />
  124. </button>
  125. </div>
  126. </div>
  127. );
  128. }
  129. }