MonacoQueryFieldWrapper.tsx 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import React, { useRef } from 'react';
  2. import { MonacoQueryFieldLazy } from './MonacoQueryFieldLazy';
  3. import { Props as MonacoProps } from './MonacoQueryFieldProps';
  4. type Props = Omit<MonacoProps, 'onRunQuery' | 'onBlur'> & {
  5. onChange: (query: string) => void;
  6. onRunQuery: () => void;
  7. runQueryOnBlur: boolean;
  8. };
  9. export const MonacoQueryFieldWrapper = (props: Props) => {
  10. const lastRunValueRef = useRef<string | null>(null);
  11. const { runQueryOnBlur, onRunQuery, onChange, ...rest } = props;
  12. const handleRunQuery = (value: string) => {
  13. lastRunValueRef.current = value;
  14. onChange(value);
  15. onRunQuery();
  16. };
  17. const handleBlur = (value: string) => {
  18. if (runQueryOnBlur) {
  19. // run handleRunQuery only if the current value is different from the last-time-executed value
  20. if (value !== lastRunValueRef.current) {
  21. handleRunQuery(value);
  22. }
  23. } else {
  24. onChange(value);
  25. }
  26. };
  27. return <MonacoQueryFieldLazy onRunQuery={handleRunQuery} onBlur={handleBlur} {...rest} />;
  28. };