Commit 368f1f67 by Peter Holmberg

render a value mapping row

parent 4f39df90
import React, { PureComponent } from 'react';
import { Label } from 'app/core/components/Label/Label';
import ToggleButtonGroup, { ToggleButton } from 'app/core/components/ToggleButtonGroup/ToggleButtonGroup';
import { RangeMap, ValueMap } from 'app/types';
enum MappingType {
ValueToText = 1,
RangeToText = 2,
}
interface Props {
mapping: ValueMap | RangeMap;
updateMapping: (mapping) => void;
}
interface State {
mapping: ValueMap | RangeMap;
mappingType: MappingType;
}
export default class MappingRow extends PureComponent<Props, State> {
constructor(props) {
super(props);
this.state = {
mappingType: MappingType.ValueToText,
mapping: props.mapping,
};
}
onMappingValueChange = event => {
const { mapping } = this.state;
const updatedMapping = { ...mapping, value: event.target.value };
this.setState({ mapping: updatedMapping });
};
onMappingFromChange = event => {
const { mapping } = this.state;
const updatedMapping = { ...mapping, from: event.target.value };
this.setState({ mapping: updatedMapping });
};
onMappingToChange = event => {
const { mapping } = this.state;
const updatedMapping = { ...mapping, to: event.target.value };
this.setState({ mapping: updatedMapping });
};
onMappingTextChange = event => {
const { mapping } = this.state;
const updatedMapping = { ...mapping, text: event.target.value };
this.setState({ mapping: updatedMapping });
};
onMappingTypeChange = mappingType => this.setState({ mappingType });
renderRow() {
const { mapping, mappingType } = this.state;
if (mappingType === MappingType.RangeToText) {
const rangeMap = mapping as RangeMap;
return (
<div className="gf-form-inline">
<div className="gf-form-group">
<Label>From</Label>
<input value={rangeMap.from} onChange={this.onMappingFromChange} />
</div>
<div className="gf-form-group">
<Label>To</Label>
<input value={rangeMap.to} onChange={this.onMappingToChange} />
</div>
<div className="gf-form-group">
<Label>Text</Label>
<input value={rangeMap.text} onChange={this.onMappingTextChange} />
</div>
</div>
);
}
const valueMap = mapping as ValueMap;
return (
<div className="gf-form-inline">
<div className="gf-form-inline">
<Label width={4}>Value</Label>
<input className="gf-form-input" onChange={this.onMappingValueChange} value={valueMap.value} />
</div>
<div className="gf-form-inline">
<Label width={4}>Text</Label>
<input className="gf-form-input" value={valueMap.text} onChange={this.onMappingTextChange} />
</div>
</div>
);
}
render() {
const { mappingType } = this.state;
return (
<div>
<div className="gf-form-inline">
<ToggleButtonGroup
label="Mapping type"
onChange={mappingType => this.onMappingTypeChange(mappingType)}
render={({ selectedValue, onChange }) => {
return [
<ToggleButton
className="btn-small"
key="value"
onChange={onChange}
selected={selectedValue === mappingType}
value="Value"
>
Value
</ToggleButton>,
<ToggleButton
className="btn-small"
key="range"
onChange={onChange}
selected={selectedValue === mappingType}
value="Range"
>
Range
</ToggleButton>,
];
}}
/>
<div>{this.renderRow()}</div>
</div>
</div>
);
}
}
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { Label } from 'app/core/components/Label/Label'; import MappingRow from './MappingRow';
import SimplePicker from 'app/core/components/Picker/SimplePicker';
import { OptionModuleProps } from './module'; import { OptionModuleProps } from './module';
import { RangeMap, ValueMap } from 'app/types'; import { RangeMap, ValueMap } from 'app/types';
interface State { interface State {
combinedMappings: any[];
valueMaps: ValueMap[]; valueMaps: ValueMap[];
rangeMaps: RangeMap[]; rangeMaps: RangeMap[];
} }
enum MappingType {
ValueToText = 1,
RangeToText = 2,
}
const mappingOptions = [
{ name: 'Value to text', value: MappingType.ValueToText },
{ name: 'Range to text', value: MappingType.RangeToText },
];
export default class ValueMappings extends PureComponent<OptionModuleProps, State> { export default class ValueMappings extends PureComponent<OptionModuleProps, State> {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
valueMaps: props.options.valueMaps, combinedMappings: props.options.valueMaps.concat(props.options.rangeMaps),
rangeMaps: props.options.rangeMaps, rangeMaps: props.options.rangeMaps,
valueMaps: props.options.valueMaps,
}; };
} }
onMappingTypeChange = option => this.props.onChange({ ...this.props.options, mappingType: option.value });
addValueMap = () => addMapping = () =>
this.setState(prevState => ({ this.setState(prevState => ({
valueMaps: [...prevState.valueMaps, { op: '', value: '', text: '' }], combinedMappings: [...prevState.combinedMappings, { op: '', value: '', text: '' }],
})); }));
addRangeMap = () => { updateGauge = mapping => {
this.setState = () =>
this.setState(prevState => ({ this.setState(prevState => ({
valueMaps: [...prevState.valueMaps, { op: '', value: '', text: '', from: '', to: '' }], combinedMappings: prevState.combinedMappings.map(m => {
})); if (m === mapping) {
}; return { ...mapping };
updateGauge = () => {};
renderValueMapList() {
const { mappingType, rangeMaps, valueMaps } = this.props.options;
if (mappingType === MappingType.RangeToText) {
return (
<div>
{rangeMaps.length > 0
? rangeMaps.map((range, index) => {
return <div>{`${range.from}-${range.to}`}</div>;
})
: 'aint no ranges, add one?'}
</div>
);
} }
return ( return m;
<div> }),
{valueMaps.length > 0 }));
? valueMaps.map((value, index) => { };
return <div>{`${value}`}</div>;
})
: 'aint no values, add one?'}
</div>
);
}
render() { render() {
const { mappingType } = this.props.options; const { combinedMappings } = this.state;
return ( return (
<div className="gf-form-group"> <div className="section gf-form-group">
<div className="gf-form"> <div className="gf-form">
<Label width={5}>Type</Label> <div className="mappings">
<SimplePicker {combinedMappings.length > 0 &&
options={mappingOptions} combinedMappings.map((mapping, index) => {
defaultValue={MappingType.ValueToText} return <MappingRow key={index} mapping={mapping} updateMapping={this.updateGauge} />;
getOptionLabel={i => i.name} })}
onSelected={option => this.onMappingTypeChange(option)}
width={5}
value={mappingType}
/>
</div> </div>
<div className="section gf-form-group"> <div onClick={this.addMapping}>Add mapping</div>
<h5 className="page-heading">Set value mappings</h5>
<div className="gf-form">{this.renderValueMapList()}</div>
</div> </div>
</div> </div>
); );
......
...@@ -34,6 +34,8 @@ export const defaultProps = { ...@@ -34,6 +34,8 @@ export const defaultProps = {
showThresholdMarkers: true, showThresholdMarkers: true,
showThresholdLabels: false, showThresholdLabels: false,
suffix: '', suffix: '',
valueMaps: [],
rangeMaps: [],
}, },
}; };
......
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