Commit 6fcc062b by David Committed by GitHub

Merge pull request #13541 from grafana/davkal/13535-fix-syntax-reload

Explore: trigger a query field render to fix highlighting
parents 4a85d012 e0b8b1b7
...@@ -158,6 +158,7 @@ interface PromQueryFieldState { ...@@ -158,6 +158,7 @@ interface PromQueryFieldState {
metrics: string[]; metrics: string[];
metricsOptions: any[]; metricsOptions: any[];
metricsByPrefix: CascaderOption[]; metricsByPrefix: CascaderOption[];
syntaxLoaded: boolean;
} }
interface PromTypeaheadInput { interface PromTypeaheadInput {
...@@ -191,6 +192,7 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF ...@@ -191,6 +192,7 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF
metrics: props.metrics || [], metrics: props.metrics || [],
metricsByPrefix: props.metricsByPrefix || [], metricsByPrefix: props.metricsByPrefix || [],
metricsOptions: [], metricsOptions: [],
syntaxLoaded: false,
}; };
} }
...@@ -266,7 +268,7 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF ...@@ -266,7 +268,7 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF
} }
// Update global prism config // Update global prism config
setPrismTokens(PRISM_SYNTAX, METRIC_MARK, this.state.metrics); setPrismTokens(PRISM_SYNTAX, METRIC_MARK, metrics);
// Build metrics tree // Build metrics tree
const histogramOptions = histogramMetrics.map(hm => ({ label: hm, value: hm })); const histogramOptions = histogramMetrics.map(hm => ({ label: hm, value: hm }));
...@@ -275,7 +277,7 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF ...@@ -275,7 +277,7 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF
...metricsByPrefix, ...metricsByPrefix,
]; ];
this.setState({ metricsOptions }); this.setState({ metricsOptions, syntaxLoaded: true });
}; };
onTypeahead = (typeahead: TypeaheadInput): TypeaheadOutput => { onTypeahead = (typeahead: TypeaheadInput): TypeaheadOutput => {
...@@ -558,8 +560,8 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF ...@@ -558,8 +560,8 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF
} }
render() { render() {
const { error, hint, supportsLogs } = this.props; const { error, hint, initialQuery, supportsLogs } = this.props;
const { logLabelOptions, metricsOptions } = this.state; const { logLabelOptions, metricsOptions, syntaxLoaded } = this.state;
return ( return (
<div className="prom-query-field"> <div className="prom-query-field">
...@@ -579,12 +581,13 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF ...@@ -579,12 +581,13 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF
<TypeaheadField <TypeaheadField
additionalPlugins={this.plugins} additionalPlugins={this.plugins}
cleanText={cleanText} cleanText={cleanText}
initialValue={this.props.initialQuery} initialValue={initialQuery}
onTypeahead={this.onTypeahead} onTypeahead={this.onTypeahead}
onWillApplySuggestion={willApplySuggestion} onWillApplySuggestion={willApplySuggestion}
onValueChanged={this.onChangeQuery} onValueChanged={this.onChangeQuery}
placeholder="Enter a PromQL query" placeholder="Enter a PromQL query"
portalPrefix="prometheus" portalPrefix="prometheus"
syntaxLoaded={syntaxLoaded}
/> />
</div> </div>
{error ? <div className="prom-query-field-info text-error">{error}</div> : null} {error ? <div className="prom-query-field-info text-error">{error}</div> : null}
......
...@@ -106,6 +106,7 @@ interface TypeaheadFieldProps { ...@@ -106,6 +106,7 @@ interface TypeaheadFieldProps {
placeholder?: string; placeholder?: string;
portalPrefix?: string; portalPrefix?: string;
syntax?: string; syntax?: string;
syntaxLoaded?: boolean;
} }
export interface TypeaheadFieldState { export interface TypeaheadFieldState {
...@@ -171,10 +172,15 @@ class QueryField extends React.PureComponent<TypeaheadFieldProps, TypeaheadField ...@@ -171,10 +172,15 @@ class QueryField extends React.PureComponent<TypeaheadFieldProps, TypeaheadField
} }
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps: TypeaheadFieldProps) {
// initialValue is null in case the user typed if (nextProps.syntaxLoaded && !this.props.syntaxLoaded) {
if (nextProps.initialValue !== null && nextProps.initialValue !== this.props.initialValue) { // Need a bogus edit to re-render the editor after syntax has fully loaded
this.setState({ value: makeValue(nextProps.initialValue, nextProps.syntax) }); this.onChange(
this.state.value
.change()
.insertText(' ')
.deleteBackward()
);
} }
} }
......
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