Files
grafana/public/app/features/datasources/components/DataSourcePluginSettings.tsx
Ashley Harrison 47f8717149 React: Use new JSX transform (#88802)
* update eslint, tsconfig + esbuild to handle new jsx transform

* remove thing that breaks the new jsx transform

* remove react imports

* adjust grafana-icons build

* is this the correct syntax?

* try this

* well this was much easier than expected...

* change grafana-plugin-configs webpack config

* fixes

* fix lockfile

* fix 2 more violations

* use path.resolve instead of require.resolve

* remove react import

* fix react imports

* more fixes

* remove React import

* remove import React from docs

* remove another react import
2024-06-25 12:43:47 +01:00

88 lines
2.4 KiB
TypeScript

import { cloneDeep } from 'lodash';
import { createElement, PureComponent } from 'react';
import { DataSourcePluginMeta, DataSourceSettings } from '@grafana/data';
import { AngularComponent, getAngularLoader } from '@grafana/runtime';
import { GenericDataSourcePlugin } from '../types';
export interface Props {
plugin: GenericDataSourcePlugin;
dataSource: DataSourceSettings;
dataSourceMeta: DataSourcePluginMeta;
onModelChange: (dataSource: DataSourceSettings) => void;
}
export class DataSourcePluginSettings extends PureComponent<Props> {
element: HTMLDivElement | null = null;
component?: AngularComponent;
scopeProps: {
ctrl: { datasourceMeta: DataSourcePluginMeta; current: DataSourceSettings };
onModelChanged: (dataSource: DataSourceSettings) => void;
};
constructor(props: Props) {
super(props);
this.scopeProps = {
ctrl: { datasourceMeta: props.dataSourceMeta, current: cloneDeep(props.dataSource) },
onModelChanged: this.onModelChanged,
};
this.onModelChanged = this.onModelChanged.bind(this);
}
componentDidMount() {
const { plugin } = this.props;
if (!this.element) {
return;
}
if (!plugin.components.ConfigEditor) {
// React editor is not specified, let's render angular editor
// How to approach this better? Introduce ReactDataSourcePlugin interface and typeguard it here?
const loader = getAngularLoader();
const template = '<plugin-component type="datasource-config-ctrl" />';
this.component = loader.load(this.element, this.scopeProps, template);
}
}
componentDidUpdate(prevProps: Props) {
const { plugin } = this.props;
if (!plugin.components.ConfigEditor && this.props.dataSource !== prevProps.dataSource) {
this.scopeProps.ctrl.current = cloneDeep(this.props.dataSource);
this.component?.digest();
}
}
componentWillUnmount() {
if (this.component) {
this.component.destroy();
}
}
onModelChanged = (dataSource: DataSourceSettings) => {
this.props.onModelChange(dataSource);
};
render() {
const { plugin, dataSource } = this.props;
if (!plugin) {
return null;
}
return (
<div ref={(element) => (this.element = element)}>
{plugin.components.ConfigEditor &&
createElement(plugin.components.ConfigEditor, {
options: dataSource,
onOptionsChange: this.onModelChanged,
})}
</div>
);
}
}