Commit 76f296e2 by Peter Holmberg

fixed typings and remove

parent 4a57d594
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { Label } from 'app/core/components/Label/Label'; import { Label } from 'app/core/components/Label/Label';
import ToggleButtonGroup, { ToggleButton } from 'app/core/components/ToggleButtonGroup/ToggleButtonGroup'; import ToggleButtonGroup, { ToggleButton } from 'app/core/components/ToggleButtonGroup/ToggleButtonGroup';
import { RangeMap, ValueMap } from 'app/types'; import { MappingType, RangeMap, ValueMap } from 'app/types';
enum MappingType {
ValueToText = 1,
RangeToText = 2,
}
interface Props { interface Props {
mapping: ValueMap | RangeMap; mapping: ValueMap | RangeMap;
updateMapping: (mapping) => void; updateMapping: (mapping) => void;
removeMapping: () => void;
} }
interface State { interface State {
mapping: ValueMap | RangeMap; mapping: ValueMap | RangeMap;
mappingType: MappingType;
} }
export default class MappingRow extends PureComponent<Props, State> { export default class MappingRow extends PureComponent<Props, State> {
...@@ -23,7 +18,6 @@ export default class MappingRow extends PureComponent<Props, State> { ...@@ -23,7 +18,6 @@ export default class MappingRow extends PureComponent<Props, State> {
super(props); super(props);
this.state = { this.state = {
mappingType: MappingType.ValueToText,
mapping: props.mapping, mapping: props.mapping,
}; };
} }
...@@ -59,12 +53,23 @@ export default class MappingRow extends PureComponent<Props, State> { ...@@ -59,12 +53,23 @@ export default class MappingRow extends PureComponent<Props, State> {
this.setState({ mapping: updatedMapping }); this.setState({ mapping: updatedMapping });
}; };
onMappingTypeChange = mappingType => this.setState({ mappingType }); updateMapping = () => {
const { mapping } = this.state;
this.props.updateMapping(mapping);
};
onMappingTypeChange = mappingType => {
const { mapping } = this.state;
const updatedMapping = { ...mapping, type: mappingType };
this.setState({ mapping: updatedMapping });
};
renderRow() { renderRow() {
const { mapping, mappingType } = this.state; const { mapping } = this.state;
if (mappingType === MappingType.RangeToText) { if (mapping.type === MappingType.RangeToText) {
const rangeMap = mapping as RangeMap; const rangeMap = mapping as RangeMap;
return ( return (
...@@ -72,19 +77,34 @@ export default class MappingRow extends PureComponent<Props, State> { ...@@ -72,19 +77,34 @@ export default class MappingRow extends PureComponent<Props, State> {
<div className="gf-form-inline"> <div className="gf-form-inline">
<Label width={4}>From</Label> <Label width={4}>From</Label>
<div> <div>
<input className="gf-form-input" value={rangeMap.from} onChange={this.onMappingFromChange} /> <input
className="gf-form-input"
value={rangeMap.from}
onBlur={this.updateMapping}
onChange={this.onMappingFromChange}
/>
</div> </div>
</div> </div>
<div className="gf-form-inline"> <div className="gf-form-inline">
<Label width={4}>To</Label> <Label width={4}>To</Label>
<div> <div>
<input className="gf-form-input" value={rangeMap.to} onChange={this.onMappingToChange} /> <input
className="gf-form-input"
value={rangeMap.to}
onBlur={this.updateMapping}
onChange={this.onMappingToChange}
/>
</div> </div>
</div> </div>
<div className="gf-form-inline"> <div className="gf-form-inline">
<Label width={4}>Text</Label> <Label width={4}>Text</Label>
<div> <div>
<input className="gf-form-input" value={rangeMap.text} onChange={this.onMappingTextChange} /> <input
className="gf-form-input"
value={rangeMap.text}
onBlur={this.updateMapping}
onChange={this.onMappingTextChange}
/>
</div> </div>
</div> </div>
</div> </div>
...@@ -98,13 +118,23 @@ export default class MappingRow extends PureComponent<Props, State> { ...@@ -98,13 +118,23 @@ export default class MappingRow extends PureComponent<Props, State> {
<div className="gf-form-inline"> <div className="gf-form-inline">
<Label width={4}>Value</Label> <Label width={4}>Value</Label>
<div> <div>
<input className="gf-form-input" onChange={this.onMappingValueChange} value={valueMap.value} /> <input
className="gf-form-input"
onBlur={this.updateMapping}
onChange={this.onMappingValueChange}
value={valueMap.value}
/>
</div> </div>
</div> </div>
<div className="gf-form-inline"> <div className="gf-form-inline">
<Label width={4}>Text</Label> <Label width={4}>Text</Label>
<div> <div>
<input className="gf-form-input" value={valueMap.text} onChange={this.onMappingTextChange} /> <input
className="gf-form-input"
onBlur={this.updateMapping}
value={valueMap.text}
onChange={this.onMappingTextChange}
/>
</div> </div>
</div> </div>
</div> </div>
...@@ -112,14 +142,14 @@ export default class MappingRow extends PureComponent<Props, State> { ...@@ -112,14 +142,14 @@ export default class MappingRow extends PureComponent<Props, State> {
} }
render() { render() {
const { mappingType } = this.state; const { mapping } = this.state;
return ( return (
<div className="mapping-row"> <div className="mapping-row">
<div className="mapping-row-type"> <div className="mapping-row-type">
<ToggleButtonGroup <ToggleButtonGroup
onChange={mappingType => this.onMappingTypeChange(mappingType)} onChange={mappingType => this.onMappingTypeChange(mappingType)}
value={mappingType} value={mapping.type}
stackedButtons={true} stackedButtons={true}
render={({ selectedValue, onChange }) => { render={({ selectedValue, onChange }) => {
return [ return [
...@@ -146,6 +176,9 @@ export default class MappingRow extends PureComponent<Props, State> { ...@@ -146,6 +176,9 @@ export default class MappingRow extends PureComponent<Props, State> {
/> />
</div> </div>
<div>{this.renderRow()}</div> <div>{this.renderRow()}</div>
<div onClick={this.props.removeMapping} className="threshold-row-remove">
<i className="fa fa-times" />
</div>
</div> </div>
); );
} }
......
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import MappingRow from './MappingRow'; import MappingRow from './MappingRow';
import { OptionModuleProps } from './module'; import { OptionModuleProps } from './module';
import { RangeMap, ValueMap } from 'app/types'; import { MappingType, RangeMap, ValueMap } from 'app/types';
interface State { interface State {
combinedMappings: any[]; mappings: Array<ValueMap | RangeMap>;
valueMaps: ValueMap[];
rangeMaps: RangeMap[];
} }
export default class ValueMappings extends PureComponent<OptionModuleProps, State> { export default class ValueMappings extends PureComponent<OptionModuleProps, State> {
...@@ -14,39 +12,57 @@ export default class ValueMappings extends PureComponent<OptionModuleProps, Stat ...@@ -14,39 +12,57 @@ export default class ValueMappings extends PureComponent<OptionModuleProps, Stat
super(props); super(props);
this.state = { this.state = {
combinedMappings: props.options.valueMaps.concat(props.options.rangeMaps), mappings: props.mappings || [],
rangeMaps: props.options.rangeMaps,
valueMaps: props.options.valueMaps,
}; };
} }
addMapping = () => addMapping = () =>
this.setState(prevState => ({ this.setState(prevState => ({
combinedMappings: [...prevState.combinedMappings, { op: '', value: '', text: '' }], mappings: [
...prevState.mappings,
{ op: '', value: '', text: '', type: MappingType.ValueToText, from: '', to: '' },
],
})); }));
updateGauge = mapping => { onRemoveMapping = index =>
this.setState(prevState => ({ this.setState(prevState => ({
combinedMappings: prevState.combinedMappings.map(m => { mappings: prevState.mappings.filter((m, i) => i !== index),
if (m === mapping) { }));
return { ...mapping };
} updateGauge = mapping => {
this.setState(
prevState => ({
mappings: prevState.mappings.map(m => {
if (m === mapping) {
return { ...mapping };
}
return m; return m;
}),
}), }),
})); () => {
this.props.onChange({ ...this.props.options, mappings: this.state.mappings });
}
);
}; };
render() { render() {
const { combinedMappings } = this.state; const { mappings } = this.state;
return ( return (
<div className="section gf-form-group"> <div className="section gf-form-group">
<h5 className="page-heading">Value mappings</h5> <h5 className="page-heading">Value mappings</h5>
<div> <div>
{combinedMappings.length > 0 && {mappings.length > 0 &&
combinedMappings.map((mapping, index) => { mappings.map((mapping, index) => {
return <MappingRow key={index} mapping={mapping} updateMapping={this.updateGauge} />; return (
<MappingRow
key={index}
mapping={mapping}
updateMapping={this.updateGauge}
removeMapping={() => this.onRemoveMapping(index)}
/>
);
})} })}
</div> </div>
<div className="add-mapping-row" onClick={this.addMapping}> <div className="add-mapping-row" onClick={this.addMapping}>
......
...@@ -16,8 +16,7 @@ export interface OptionsProps { ...@@ -16,8 +16,7 @@ export interface OptionsProps {
suffix: string; suffix: string;
unit: string; unit: string;
thresholds: Threshold[]; thresholds: Threshold[];
valueMaps: ValueMap[]; mappings: Array<ValueMap | RangeMap>;
rangeMaps: RangeMap[];
mappingType: number; mappingType: number;
} }
......
...@@ -21,7 +21,7 @@ import { ...@@ -21,7 +21,7 @@ import {
DataQueryOptions, DataQueryOptions,
IntervalValues, IntervalValues,
} from './series'; } from './series';
import { PanelProps, PanelOptionsProps, RangeMap, Threshold, ValueMap } from './panel'; import { MappingType, PanelProps, PanelOptionsProps, RangeMap, Threshold, ValueMap } from './panel';
import { PluginDashboard, PluginMeta, Plugin, PanelPlugin, PluginsState } from './plugins'; import { PluginDashboard, PluginMeta, Plugin, PanelPlugin, PluginsState } from './plugins';
import { Organization, OrganizationState } from './organization'; import { Organization, OrganizationState } from './organization';
import { import {
...@@ -96,6 +96,7 @@ export { ...@@ -96,6 +96,7 @@ export {
ValueMap, ValueMap,
RangeMap, RangeMap,
IntervalValues, IntervalValues,
MappingType,
}; };
export interface StoreState { export interface StoreState {
......
...@@ -37,9 +37,15 @@ export interface Threshold { ...@@ -37,9 +37,15 @@ export interface Threshold {
canRemove: boolean; canRemove: boolean;
} }
export enum MappingType {
ValueToText = 1,
RangeToText = 2,
}
interface BaseMap { interface BaseMap {
op: string; op: string;
text: string; text: string;
type: MappingType;
} }
export interface ValueMap extends BaseMap { export interface ValueMap extends BaseMap {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment