MappingRow.tsx 4.0 KB

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