Commit 1496da03 by Marcus Andersson Committed by GitHub

PanelEdit: made the transformers tab re-render on changes (#23384)

* fixed so the transformers UI is less buggy.

* changed so we don't mutate state.
parent 20246a31
import { css } from 'emotion'; import { css } from 'emotion';
import React from 'react'; import React from 'react';
import { transformersUIRegistry } from '@grafana/ui/src/components/TransformersUI/transformers'; import { transformersUIRegistry } from '@grafana/ui/src/components/TransformersUI/transformers';
import { DataTransformerID, DataTransformerConfig, DataFrame, transformDataFrame } from '@grafana/data'; import { DataTransformerConfig, DataFrame, transformDataFrame, SelectableValue } from '@grafana/data';
import { Button, Select } from '@grafana/ui'; import { Button, Select } from '@grafana/ui';
import { TransformationRow } from './TransformationRow'; import { TransformationRow } from './TransformationRow';
...@@ -12,45 +12,41 @@ interface Props { ...@@ -12,45 +12,41 @@ interface Props {
} }
interface State { interface State {
updateCounter: number; addingTransformation: boolean;
} }
export class TransformationsEditor extends React.PureComponent<Props, State> { export class TransformationsEditor extends React.PureComponent<Props, State> {
state = { updateCounter: 0 }; state = { addingTransformation: false };
onTransformationAdd = () => { onTransformationAdd = (selectable: SelectableValue<string>) => {
const { transformations, onChange } = this.props; const { transformations, onChange } = this.props;
onChange([ onChange([
...transformations, ...transformations,
{ {
id: DataTransformerID.noop, id: selectable.value as string,
options: {}, options: {},
}, },
]); ]);
this.setState({ updateCounter: this.state.updateCounter + 1 }); this.setState({ addingTransformation: false });
}; };
onTransformationChange = (idx: number, config: DataTransformerConfig) => { onTransformationChange = (idx: number, config: DataTransformerConfig) => {
const { transformations, onChange } = this.props; const { transformations, onChange } = this.props;
transformations[idx] = config; const next = Array.from(transformations);
onChange(transformations); next[idx] = config;
this.setState({ updateCounter: this.state.updateCounter + 1 }); onChange(next);
}; };
onTransformationRemove = (idx: number) => { onTransformationRemove = (idx: number) => {
const { transformations, onChange } = this.props; const { transformations, onChange } = this.props;
transformations.splice(idx, 1); const next = Array.from(transformations);
onChange(transformations); next.splice(idx, 1);
this.setState({ updateCounter: this.state.updateCounter + 1 }); onChange(next);
}; };
renderTransformationEditors = () => { renderTransformationSelector = () => {
const { transformations, dataFrames } = this.props; if (!this.state.addingTransformation) {
const hasTransformations = transformations.length > 0; return null;
const preTransformData = dataFrames;
if (!hasTransformations) {
return undefined;
} }
const availableTransformers = transformersUIRegistry.list().map(t => { const availableTransformers = transformersUIRegistry.list().map(t => {
...@@ -61,29 +57,31 @@ export class TransformationsEditor extends React.PureComponent<Props, State> { ...@@ -61,29 +57,31 @@ export class TransformationsEditor extends React.PureComponent<Props, State> {
}); });
return ( return (
<> <div
{transformations.map((t, i) => {
let editor, input;
if (t.id === DataTransformerID.noop) {
return (
<Select
className={css` className={css`
margin-bottom: 10px; margin-bottom: 10px;
`} `}
key={`${t.id}-${i}`} >
<Select
options={availableTransformers} options={availableTransformers}
placeholder="Select transformation" placeholder="Select transformation"
onChange={v => { onChange={this.onTransformationAdd}
this.onTransformationChange(i, {
id: v.value as string,
options: {},
});
}}
/> />
</div>
); );
} };
renderTransformationEditors = () => {
const { transformations, dataFrames } = this.props;
const preTransformData = dataFrames;
return (
<>
{transformations.map((t, i) => {
let editor;
const transformationUI = transformersUIRegistry.getIfExists(t.id); const transformationUI = transformersUIRegistry.getIfExists(t.id);
input = transformDataFrame(transformations.slice(0, i), preTransformData); const input = transformDataFrame(transformations.slice(0, i), preTransformData);
if (transformationUI) { if (transformationUI) {
editor = React.createElement(transformationUI.component, { editor = React.createElement(transformationUI.component, {
...@@ -121,7 +119,8 @@ export class TransformationsEditor extends React.PureComponent<Props, State> { ...@@ -121,7 +119,8 @@ export class TransformationsEditor extends React.PureComponent<Props, State> {
visualized. visualized.
</p> </p>
{this.renderTransformationEditors()} {this.renderTransformationEditors()}
<Button variant="secondary" icon="fa fa-plus" onClick={this.onTransformationAdd}> {this.renderTransformationSelector()}
<Button variant="secondary" icon="fa fa-plus" onClick={() => this.setState({ addingTransformation: true })}>
Add transformation Add transformation
</Button> </Button>
</div> </div>
......
...@@ -448,6 +448,7 @@ export class PanelModel implements DataConfigSource { ...@@ -448,6 +448,7 @@ export class PanelModel implements DataConfigSource {
setTransformations(transformations: DataTransformerConfig[]) { setTransformations(transformations: DataTransformerConfig[]) {
this.transformations = transformations; this.transformations = transformations;
this.resendLastResult();
} }
replaceVariables(value: string, extraVars?: ScopedVars, format?: string) { replaceVariables(value: string, extraVars?: ScopedVars, format?: string) {
......
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