| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import React, { PureComponent } from 'react';
- import { MappingType, RangeMap, Select, ValueMap } from '@grafana/ui';
- import { Label } from 'app/core/components/Label/Label';
- interface Props {
- mapping: ValueMap | RangeMap;
- updateMapping: (mapping) => void;
- removeMapping: () => void;
- }
- interface State {
- from: string;
- id: number;
- operator: string;
- text: string;
- to: string;
- type: MappingType;
- value: string;
- }
- const mappingOptions = [
- { value: MappingType.ValueToText, label: 'Value' },
- { value: MappingType.RangeToText, label: 'Range' },
- ];
- export default class MappingRow extends PureComponent<Props, State> {
- constructor(props) {
- super(props);
- this.state = {
- ...props.mapping,
- };
- }
- onMappingValueChange = event => {
- this.setState({ value: event.target.value });
- };
- onMappingFromChange = event => {
- this.setState({ from: event.target.value });
- };
- onMappingToChange = event => {
- this.setState({ to: event.target.value });
- };
- onMappingTextChange = event => {
- this.setState({ text: event.target.value });
- };
- onMappingTypeChange = mappingType => {
- this.setState({ type: mappingType });
- };
- updateMapping = () => {
- this.props.updateMapping({ ...this.state });
- };
- renderRow() {
- const { from, text, to, type, value } = this.state;
- if (type === MappingType.RangeToText) {
- return (
- <>
- <div className="gf-form">
- <Label width={4}>From</Label>
- <input
- className="gf-form-input width-8"
- value={from}
- onBlur={this.updateMapping}
- onChange={this.onMappingFromChange}
- />
- </div>
- <div className="gf-form">
- <Label width={4}>To</Label>
- <input
- className="gf-form-input width-8"
- value={to}
- onBlur={this.updateMapping}
- onChange={this.onMappingToChange}
- />
- </div>
- <div className="gf-form">
- <Label width={4}>Text</Label>
- <input
- className="gf-form-input width-10"
- value={text}
- onBlur={this.updateMapping}
- onChange={this.onMappingTextChange}
- />
- </div>
- </>
- );
- }
- return (
- <>
- <div className="gf-form">
- <Label width={4}>Value</Label>
- <input
- className="gf-form-input width-8"
- onBlur={this.updateMapping}
- onChange={this.onMappingValueChange}
- value={value}
- />
- </div>
- <div className="gf-form gf-form--grow">
- <Label width={4}>Text</Label>
- <input
- className="gf-form-input"
- onBlur={this.updateMapping}
- value={text}
- onChange={this.onMappingTextChange}
- />
- </div>
- </>
- );
- }
- render() {
- const { type } = this.state;
- return (
- <div className="gf-form-inline">
- <div className="gf-form">
- <Label width={5}>Type</Label>
- <Select
- placeholder="Choose type"
- isSearchable={false}
- options={mappingOptions}
- value={mappingOptions.find(o => o.value === type)}
- onChange={type => this.onMappingTypeChange(type.value)}
- width={7}
- />
- </div>
- {this.renderRow()}
- <div className="gf-form">
- <button onClick={this.props.removeMapping} className="gf-form-label gf-form-label--btn">
- <i className="fa fa-times" />
- </button>
- </div>
- </div>
- );
- }
- }
|