grafana/public/app/features/plugins/admin/components/SearchField.tsx
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

51 lines
1.2 KiB
TypeScript

import React, { useState, useRef } from 'react';
import { useDebounce } from 'react-use';
import { FilterInput } from '@grafana/ui';
interface Props {
value?: string;
onSearch: (value: string) => void;
}
// useDebounce has a bug which causes it to fire on first render. This wrapper prevents that.
// https://github.com/streamich/react-use/issues/759
const useDebounceWithoutFirstRender = (callBack: () => any, delay = 0, deps: React.DependencyList = []) => {
const isFirstRender = useRef(true);
const debounceDeps = [...deps, isFirstRender];
return useDebounce(
() => {
if (isFirstRender.current) {
isFirstRender.current = false;
return;
}
return callBack();
},
delay,
debounceDeps
);
};
export const SearchField = ({ value, onSearch }: Props) => {
const [query, setQuery] = useState(value);
useDebounceWithoutFirstRender(() => onSearch(query ?? ''), 500, [query]);
return (
<FilterInput
value={query}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.keyCode === 13) {
onSearch(e.currentTarget.value);
}
}}
placeholder="Search Grafana plugins"
onChange={(value) => {
setQuery(value);
}}
width={46}
/>
);
};