fix(HomePageSearchBar): don't require SearchContext
Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-search': patch
|
||||
---
|
||||
|
||||
Fix issue with `HomePageSearchBar` requiring `SearchContext`
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-search-react': patch
|
||||
---
|
||||
|
||||
Export `useSearchContextCheck` hook to check if the search context is available
|
||||
@@ -74,4 +74,7 @@ export type SearchContextValue = {
|
||||
|
||||
// @public
|
||||
export const useSearch: () => SearchContextValue;
|
||||
|
||||
// @public
|
||||
export const useSearchContextCheck: () => boolean;
|
||||
```
|
||||
|
||||
@@ -18,7 +18,11 @@ import { useApi } from '@backstage/core-plugin-api';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import { act, renderHook } from '@testing-library/react-hooks';
|
||||
import React from 'react';
|
||||
import { SearchContextProvider, useSearch } from './SearchContext';
|
||||
import {
|
||||
SearchContextProvider,
|
||||
useSearch,
|
||||
useSearchContextCheck,
|
||||
} from './SearchContext';
|
||||
|
||||
jest.mock('@backstage/core-plugin-api', () => ({
|
||||
...jest.requireActual('@backstage/core-plugin-api'),
|
||||
@@ -71,6 +75,26 @@ describe('SearchContext', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('Checks whether context is set', async () => {
|
||||
const hook = renderHook(() => useSearchContextCheck());
|
||||
|
||||
expect(hook.result.current).toEqual(false);
|
||||
|
||||
const { result, waitForNextUpdate } = renderHook(
|
||||
() => useSearchContextCheck(),
|
||||
{
|
||||
wrapper,
|
||||
initialProps: {
|
||||
initialState,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
await waitForNextUpdate();
|
||||
|
||||
expect(result.current).toEqual(true);
|
||||
});
|
||||
|
||||
it('Uses initial state values', async () => {
|
||||
const { result, waitForNextUpdate } = renderHook(() => useSearch(), {
|
||||
wrapper,
|
||||
|
||||
@@ -79,6 +79,16 @@ export const useSearch = () => {
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* @public
|
||||
*
|
||||
* React hook which checks for an existing search context
|
||||
*/
|
||||
export const useSearchContextCheck = () => {
|
||||
const context = useContext(SearchContext);
|
||||
return context !== undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* The initial state of `SearchContextProvider`.
|
||||
*
|
||||
|
||||
@@ -14,7 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { SearchContextProvider, useSearch } from './SearchContext';
|
||||
export {
|
||||
SearchContextProvider,
|
||||
useSearch,
|
||||
useSearchContextCheck,
|
||||
} from './SearchContext';
|
||||
export type {
|
||||
SearchContextProviderProps,
|
||||
SearchContextState,
|
||||
|
||||
@@ -25,6 +25,7 @@ export type { SearchApi } from './api';
|
||||
export {
|
||||
SearchContextProvider,
|
||||
useSearch,
|
||||
useSearchContextCheck,
|
||||
SearchContextProviderForStorybook,
|
||||
SearchApiProviderForStorybook,
|
||||
} from './context';
|
||||
|
||||
@@ -35,6 +35,7 @@ import ClearButton from '@material-ui/icons/Clear';
|
||||
import {
|
||||
SearchContextProvider,
|
||||
useSearch,
|
||||
useSearchContextCheck,
|
||||
} from '@backstage/plugin-search-react';
|
||||
import { TrackSearch } from '../SearchTracker';
|
||||
|
||||
@@ -72,7 +73,7 @@ export const SearchBarBase = ({
|
||||
}: SearchBarBaseProps) => {
|
||||
const configApi = useApi(configApiRef);
|
||||
const [value, setValue] = useState<string>(defaultValue as string);
|
||||
const hasSearchContext = useSearch();
|
||||
const hasSearchContext = useSearchContextCheck();
|
||||
|
||||
useEffect(() => {
|
||||
setValue(prevValue =>
|
||||
|
||||
Reference in New Issue
Block a user