MappingRow.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import React, { ChangeEvent, PureComponent } from 'react';
  2. import { MappingType, ValueMapping } from '../../types';
  3. import { FormField, FormLabel, Select } from '..';
  4. export interface Props {
  5. valueMapping: ValueMapping;
  6. updateValueMapping: (valueMapping: ValueMapping) => void;
  7. removeValueMapping: () => 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: Props) {
  24. super(props);
  25. this.state = { ...props.valueMapping };
  26. }
  27. onMappingValueChange = (event: ChangeEvent<HTMLInputElement>) => {
  28. this.setState({ value: event.target.value });
  29. };
  30. onMappingFromChange = (event: ChangeEvent<HTMLInputElement>) => {
  31. this.setState({ from: event.target.value });
  32. };
  33. onMappingToChange = (event: ChangeEvent<HTMLInputElement>) => {
  34. this.setState({ to: event.target.value });
  35. };
  36. onMappingTextChange = (event: ChangeEvent<HTMLInputElement>) => {
  37. this.setState({ text: event.target.value });
  38. };
  39. onMappingTypeChange = (mappingType: MappingType) => {
  40. this.setState({ type: mappingType });
  41. };
  42. updateMapping = () => {
  43. this.props.updateValueMapping({ ...this.state } as ValueMapping);
  44. };
  45. renderRow() {
  46. const { from, text, to, type, value } = this.state;
  47. if (type === MappingType.RangeToText) {
  48. return (
  49. <>
  50. <FormField
  51. label="From"
  52. labelWidth={4}
  53. inputWidth={8}
  54. onBlur={this.updateMapping}
  55. onChange={this.onMappingFromChange}
  56. value={from}
  57. />
  58. <FormField
  59. label="To"
  60. labelWidth={4}
  61. inputWidth={8}
  62. onBlur={this.updateMapping}
  63. onChange={this.onMappingToChange}
  64. value={to}
  65. />
  66. <div className="gf-form gf-form--grow">
  67. <FormLabel width={4}>Text</FormLabel>
  68. <input
  69. className="gf-form-input"
  70. onBlur={this.updateMapping}
  71. value={text}
  72. onChange={this.onMappingTextChange}
  73. />
  74. </div>
  75. </>
  76. );
  77. }
  78. return (
  79. <>
  80. <FormField
  81. label="Value"
  82. labelWidth={4}
  83. onBlur={this.updateMapping}
  84. onChange={this.onMappingValueChange}
  85. value={value}
  86. inputWidth={8}
  87. />
  88. <div className="gf-form gf-form--grow">
  89. <FormLabel width={4}>Text</FormLabel>
  90. <input
  91. className="gf-form-input"
  92. onBlur={this.updateMapping}
  93. value={text}
  94. onChange={this.onMappingTextChange}
  95. />
  96. </div>
  97. </>
  98. );
  99. }
  100. render() {
  101. const { type } = this.state;
  102. return (
  103. <div className="gf-form-inline">
  104. <div className="gf-form">
  105. <FormLabel width={5}>Type</FormLabel>
  106. <Select
  107. placeholder="Choose type"
  108. isSearchable={false}
  109. options={mappingOptions}
  110. value={mappingOptions.find(o => o.value === type)}
  111. onChange={type => this.onMappingTypeChange(type.value)}
  112. width={7}
  113. />
  114. </div>
  115. {this.renderRow()}
  116. <div className="gf-form">
  117. <button onClick={this.props.removeValueMapping} className="gf-form-label gf-form-label--btn">
  118. <i className="fa fa-times" />
  119. </button>
  120. </div>
  121. </div>
  122. );
  123. }
  124. }