MappingRow.tsx 4.5 KB

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