InputConfigEditor.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Types
  4. import { InputOptions } from './types';
  5. import { DataSourcePluginOptionsEditorProps, DataSourceSettings, SeriesData, TableInputCSV, toCSV } from '@grafana/ui';
  6. type InputSettings = DataSourceSettings<InputOptions>;
  7. interface Props extends DataSourcePluginOptionsEditorProps<InputSettings> {}
  8. interface State {
  9. text: string;
  10. }
  11. export class InputConfigEditor extends PureComponent<Props, State> {
  12. state = {
  13. text: '',
  14. };
  15. componentDidMount() {
  16. const { options } = this.props;
  17. if (options.jsonData.data) {
  18. const text = toCSV(options.jsonData.data);
  19. this.setState({ text });
  20. }
  21. }
  22. onSeriesParsed = (data: SeriesData[], text: string) => {
  23. const { options, onOptionsChange } = this.props;
  24. if (!data) {
  25. data = [
  26. {
  27. fields: [],
  28. rows: [],
  29. },
  30. ];
  31. }
  32. // data is a property on 'jsonData'
  33. const jsonData = {
  34. ...options.jsonData,
  35. data,
  36. };
  37. onOptionsChange({
  38. ...options,
  39. jsonData,
  40. });
  41. this.setState({ text });
  42. };
  43. render() {
  44. const { text } = this.state;
  45. return (
  46. <div>
  47. <div className="gf-form-group">
  48. <h4>Shared Data:</h4>
  49. <span>Enter CSV</span>
  50. <TableInputCSV text={text} onSeriesParsed={this.onSeriesParsed} width={'100%'} height={200} />
  51. </div>
  52. <div className="grafana-info-box">
  53. This data is stored in the datasource json and is returned to every user in the initial request for any
  54. datasource. This is an appropriate place to enter a few values. Large datasets will perform better in other
  55. datasources.
  56. <br />
  57. <br />
  58. <b>NOTE:</b> Changes to this data will only be reflected after a browser refresh.
  59. </div>
  60. </div>
  61. );
  62. }
  63. }