Files
grafana/public/app/plugins/datasource/prometheus/components/monaco-query-field/MonacoQueryFieldWrapper.tsx
Gábor Farkas 844d2c8621 prometheus: monaco: handle in-dashboard and in-explore use cases (#40922)
* prometheus: monaco: handle in-dashboard and in-explore use cases

* refactor is-explore handling

* improved comment

* removed unnecessary comment

* reordered props

* simplify code

* refactor: better prop-name

* fixed test snapshot
2021-10-27 11:45:32 +02:00

34 lines
1.0 KiB
TypeScript

import React, { useRef } from 'react';
import { MonacoQueryFieldLazy } from './MonacoQueryFieldLazy';
import { Props as MonacoProps } from './MonacoQueryFieldProps';
type Props = Omit<MonacoProps, 'onRunQuery' | 'onBlur'> & {
onChange: (query: string) => void;
onRunQuery: () => void;
runQueryOnBlur: boolean;
};
export const MonacoQueryFieldWrapper = (props: Props) => {
const lastRunValueRef = useRef<string | null>(null);
const { runQueryOnBlur, onRunQuery, onChange, ...rest } = props;
const handleRunQuery = (value: string) => {
lastRunValueRef.current = value;
onChange(value);
onRunQuery();
};
const handleBlur = (value: string) => {
if (runQueryOnBlur) {
// run handleRunQuery only if the current value is different from the last-time-executed value
if (value !== lastRunValueRef.current) {
handleRunQuery(value);
}
} else {
onChange(value);
}
};
return <MonacoQueryFieldLazy onRunQuery={handleRunQuery} onBlur={handleBlur} {...rest} />;
};