Skip to content

Commit

Permalink
fix: search conditional rendering (freeCodeCamp#57200)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sembauke authored Nov 18, 2024
1 parent 7f830a1 commit cb36adb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
16 changes: 8 additions & 8 deletions client/src/components/Header/components/universal-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type UniversalNavProps = Omit<
> & {
fetchState: { pending: boolean };
searchBarRef?: React.RefObject<HTMLDivElement>;
pathname: string;
};
const UniversalNav = ({
displayMenu,
Expand All @@ -31,21 +32,20 @@ const UniversalNav = ({
menuButtonRef,
searchBarRef,
user,
fetchState
fetchState,
pathname
}: UniversalNavProps): JSX.Element => {
const { pending } = fetchState;
const { t } = useTranslation();
const isSearchExposedWidth = useMediaQuery({
query: `(min-width: ${SEARCH_EXPOSED_WIDTH}px)`
});

const search =
typeof window !== `undefined` && isLanding(window.location.pathname) ? (
<SearchBarOptimized innerRef={searchBarRef} />
) : (
<SearchBar innerRef={searchBarRef} />
);

const search = isLanding(pathname) ? (
<SearchBarOptimized innerRef={searchBarRef} />
) : (
<SearchBar innerRef={searchBarRef} />
);
return (
<nav
aria-label={t('aria.primary-nav')}
Expand Down
5 changes: 4 additions & 1 deletion client/src/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Props = PropsFromRedux & {
fetchState: { pending: boolean };
user: User;
skipButtonText: string;
pathname: string;
};

class Header extends React.Component<Props, { displayMenu: boolean }> {
Expand Down Expand Up @@ -79,7 +80,8 @@ class Header extends React.Component<Props, { displayMenu: boolean }> {

render(): JSX.Element {
const { displayMenu } = this.state;
const { examInProgress, fetchState, user, skipButtonText } = this.props;
const { examInProgress, fetchState, user, skipButtonText, pathname } =
this.props;
return (
<header className='site-header'>
<a
Expand All @@ -96,6 +98,7 @@ class Header extends React.Component<Props, { displayMenu: boolean }> {
displayMenu={displayMenu}
fetchState={fetchState}
hideMenu={this.hideMenu}
pathname={pathname}
menuButtonRef={this.menuButtonRef}
searchBarRef={this.searchBarRef}
showMenu={this.showMenu}
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/layouts/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function DefaultLayout({
superBlock,
theme,
user,
pathname,
fetchUser
}: DefaultLayoutProps): JSX.Element {
const { t } = useTranslation();
Expand Down Expand Up @@ -269,6 +270,7 @@ function DefaultLayout({
<Header
fetchState={fetchState}
user={user}
pathname={pathname}
skipButtonText={t('learn.skip-to-content')}
/>
<OfflineWarning
Expand Down
16 changes: 16 additions & 0 deletions e2e/search-bar-optimized.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,20 @@ test.describe('Search bar optimized', () => {

await expect(searchInput).toHaveValue('');
});

test('The optimized searchbar component should not render when not on the landing page', async ({
page,
isMobile
}) => {
// This means that the default search bar should be rendered ^.
await page.getByTestId('curriculum-map-button').nth(0).click();

if (isMobile) {
const menuButton = page.getByTestId('header-menu-button');
await expect(menuButton).toBeVisible();
await menuButton.click();
}

await expect(page.getByTestId('header-search')).toBeVisible();
});
});

0 comments on commit cb36adb

Please sign in to comment.