mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Started search state refactor * Things are working * Move more to statemanger * minor tweaks * Fixed name of hook * revert yarn.lock changes * Harderning StateManagerBase * More tests and refinements * Fixed unit test * More polish * fixing tests * fixed test * Fixed test
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import React, { useState } from 'react';
|
|
|
|
import { locationService } from '@grafana/runtime';
|
|
import { FilterInput, ToolbarButton, useTheme2 } from '@grafana/ui';
|
|
import { useMediaQueryChange } from 'app/core/hooks/useMediaQueryChange';
|
|
import { t } from 'app/core/internationalization';
|
|
import { getSearchStateManager } from 'app/features/search/state/SearchStateManager';
|
|
|
|
export function TopSearchBarInput() {
|
|
const theme = useTheme2();
|
|
const stateManager = getSearchStateManager();
|
|
const state = stateManager.useState();
|
|
const breakpoint = theme.breakpoints.values.sm;
|
|
|
|
const [isSmallScreen, setIsSmallScreen] = useState(window.matchMedia(`(max-width: ${breakpoint}px)`).matches);
|
|
|
|
useMediaQueryChange({
|
|
breakpoint,
|
|
onChange: (e) => {
|
|
setIsSmallScreen(e.matches);
|
|
},
|
|
});
|
|
|
|
const onOpenSearch = () => {
|
|
locationService.partial({ search: 'open' });
|
|
};
|
|
|
|
const onSearchChange = (value: string) => {
|
|
stateManager.onQueryChange(value);
|
|
if (value) {
|
|
onOpenSearch();
|
|
}
|
|
};
|
|
|
|
if (isSmallScreen) {
|
|
return <ToolbarButton iconOnly icon="search" aria-label="Search Grafana" onClick={onOpenSearch} />;
|
|
}
|
|
|
|
return (
|
|
<FilterInput
|
|
onClick={onOpenSearch}
|
|
placeholder={t('nav.search.placeholder', 'Search Grafana')}
|
|
value={state.query ?? ''}
|
|
onChange={onSearchChange}
|
|
/>
|
|
);
|
|
}
|