// Libraries import React, { PureComponent } from 'react'; // Types import { AbsoluteTimeRange, QueryEditorProps, PanelData } from '@grafana/data'; import { InlineFormLabel } from '@grafana/ui'; import { LokiDatasource } from '../datasource'; import { LokiQuery } from '../types'; import { LokiQueryField } from './LokiQueryField'; type Props = QueryEditorProps; interface State { legendFormat: string; } export class LokiQueryEditor extends PureComponent { // Query target to be modified and used for queries query: LokiQuery; constructor(props: Props) { super(props); // Use default query to prevent undefined input values const defaultQuery: Partial = { expr: '', legendFormat: '' }; const query = Object.assign({}, defaultQuery, props.query); this.query = query; // Query target properties that are fully controlled inputs this.state = { // Fully controlled text inputs legendFormat: query.legendFormat, }; } calcAbsoluteRange = (data: PanelData): AbsoluteTimeRange => { if (data && data.request) { const { range } = data.request; return { from: range.from.valueOf(), to: range.to.valueOf(), }; } return { from: Date.now() - 10000, to: Date.now(), }; }; onFieldChange = (query: LokiQuery, override?: any) => { this.query.expr = query.expr; }; onLegendChange = (e: React.SyntheticEvent) => { const legendFormat = e.currentTarget.value; this.query.legendFormat = legendFormat; this.setState({ legendFormat }); }; onRunQuery = () => { const { query } = this; this.props.onChange(query); this.props.onRunQuery(); }; render() { const { datasource, query, data } = this.props; const { legendFormat } = this.state; return (
Legend
); } } export default LokiQueryEditor;