feat(search-react): add use parent context prop

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2022-08-26 12:42:38 +02:00
parent af7594d587
commit 412825d9eb
3 changed files with 49 additions and 37 deletions
@@ -55,7 +55,7 @@ describe('SearchModal', () => {
);
expect(screen.getByRole('dialog')).toBeInTheDocument();
expect(query).toHaveBeenCalledTimes(1);
expect(query).toHaveBeenCalledTimes(2);
});
it('Should use parent search context if defined', async () => {
@@ -133,7 +133,7 @@ describe('SearchModal', () => {
},
);
expect(query).toHaveBeenCalledTimes(1);
expect(query).toHaveBeenCalledTimes(2);
await userEvent.keyboard('{Escape}');
expect(toggleModal).toHaveBeenCalledTimes(1);
});
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import React, { PropsWithChildren } from 'react';
import React from 'react';
import {
Dialog,
DialogActions,
@@ -35,7 +35,6 @@ import {
SearchResult,
SearchResultPager,
useSearch,
useSearchContextCheck,
} from '@backstage/plugin-search-react';
import { useRouteRef } from '@backstage/core-plugin-api';
import { Link, useContent } from '@backstage/core-components';
@@ -171,15 +170,6 @@ export const Modal = ({ toggleModal }: SearchModalProps) => {
);
};
const Context = ({ children }: PropsWithChildren<{}>) => {
// Checks if there is a parent context already defined and, if not, creates a new local context.
const hasParentContext = useSearchContextCheck();
if (hasParentContext) {
return <>{children}</>;
}
return <SearchContextProvider>{children}</SearchContextProvider>;
};
/**
* @public
*/
@@ -204,11 +194,11 @@ export const SearchModal = ({
hidden={hidden}
>
{open && (
<Context>
<SearchContextProvider useParentContext>
{(children && children({ toggleModal })) ?? (
<Modal toggleModal={toggleModal} />
)}
</Context>
</SearchContextProvider>
)}
</Dialog>
);