MappingRow.tsx 3.5 KB

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