MappingRow.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React, { PureComponent } from 'react';
  2. import { Label } from 'app/core/components/Label/Label';
  3. import { Select } from 'app/core/components/Select/Select';
  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. <>
  53. <div className="gf-form">
  54. <Label width={4}>From</Label>
  55. <input
  56. className="gf-form-input width-8"
  57. value={from}
  58. onBlur={this.updateMapping}
  59. onChange={this.onMappingFromChange}
  60. />
  61. </div>
  62. <div className="gf-form">
  63. <Label width={4}>To</Label>
  64. <input
  65. className="gf-form-input width-8"
  66. value={to}
  67. onBlur={this.updateMapping}
  68. onChange={this.onMappingToChange}
  69. />
  70. </div>
  71. <div className="gf-form">
  72. <Label width={4}>Text</Label>
  73. <input
  74. className="gf-form-input width-10"
  75. value={text}
  76. onBlur={this.updateMapping}
  77. onChange={this.onMappingTextChange}
  78. />
  79. </div>
  80. </>
  81. );
  82. }
  83. return (
  84. <>
  85. <div className="gf-form">
  86. <Label width={4}>Value</Label>
  87. <input
  88. className="gf-form-input width-8"
  89. onBlur={this.updateMapping}
  90. onChange={this.onMappingValueChange}
  91. value={value}
  92. />
  93. </div>
  94. <div className="gf-form gf-form--grow">
  95. <Label width={4}>Text</Label>
  96. <input
  97. className="gf-form-input"
  98. onBlur={this.updateMapping}
  99. value={text}
  100. onChange={this.onMappingTextChange}
  101. />
  102. </div>
  103. </>
  104. );
  105. }
  106. render() {
  107. const { type } = this.state;
  108. return (
  109. <div className="gf-form-inline">
  110. <div className="gf-form">
  111. <Label width={5}>Type</Label>
  112. <Select
  113. placeholder="Choose type"
  114. isSearchable={false}
  115. options={mappingOptions}
  116. value={mappingOptions.find(o => o.value === type)}
  117. onChange={type => this.onMappingTypeChange(type.value)}
  118. width={7}
  119. />
  120. </div>
  121. {this.renderRow()}
  122. <div className="gf-form">
  123. <button onClick={this.props.removeMapping} className="gf-form-label gf-form-label--btn">
  124. <i className="fa fa-times" />
  125. </button>
  126. </div>
  127. </div>
  128. );
  129. }
  130. }