diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-15 16:36:58 +0200 | 
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-15 16:36:58 +0200 | 
| commit | 235fe67d770f83131c9ec10b99012319440db690 (patch) | |
| tree | 3b96e2c8a5877fe15a9cfa6bff46130fa7a04a65 /src/components/organisms | |
| parent | fe2252ced2bb895e26179640553b5a6c02957d54 (diff) | |
chore: add Search page
Diffstat (limited to 'src/components/organisms')
| -rw-r--r-- | src/components/organisms/forms/search-form.stories.tsx | 9 | ||||
| -rw-r--r-- | src/components/organisms/forms/search-form.test.tsx | 4 | ||||
| -rw-r--r-- | src/components/organisms/forms/search-form.tsx | 14 | ||||
| -rw-r--r-- | src/components/organisms/layout/header.stories.tsx | 9 | ||||
| -rw-r--r-- | src/components/organisms/layout/header.test.tsx | 12 | ||||
| -rw-r--r-- | src/components/organisms/layout/header.tsx | 21 | ||||
| -rw-r--r-- | src/components/organisms/modals/search-modal.stories.tsx | 11 | ||||
| -rw-r--r-- | src/components/organisms/modals/search-modal.test.tsx | 2 | ||||
| -rw-r--r-- | src/components/organisms/modals/search-modal.tsx | 8 | ||||
| -rw-r--r-- | src/components/organisms/toolbar/search.stories.tsx | 10 | ||||
| -rw-r--r-- | src/components/organisms/toolbar/search.test.tsx | 6 | ||||
| -rw-r--r-- | src/components/organisms/toolbar/search.tsx | 12 | ||||
| -rw-r--r-- | src/components/organisms/toolbar/toolbar.stories.tsx | 11 | ||||
| -rw-r--r-- | src/components/organisms/toolbar/toolbar.test.tsx | 2 | ||||
| -rw-r--r-- | src/components/organisms/toolbar/toolbar.tsx | 7 | 
15 files changed, 67 insertions, 71 deletions
| diff --git a/src/components/organisms/forms/search-form.stories.tsx b/src/components/organisms/forms/search-form.stories.tsx index 7f4c7c0..6ea6122 100644 --- a/src/components/organisms/forms/search-form.stories.tsx +++ b/src/components/organisms/forms/search-form.stories.tsx @@ -1,5 +1,4 @@  import { ComponentMeta, ComponentStory } from '@storybook/react'; -import { IntlProvider } from 'react-intl';  import SearchForm from './search-form';  /** @@ -10,6 +9,7 @@ export default {    component: SearchForm,    args: {      hideLabel: false, +    searchPage: '#',    },    argTypes: {      className: { @@ -40,13 +40,6 @@ export default {        },      },    }, -  decorators: [ -    (Story) => ( -      <IntlProvider locale="en"> -        <Story /> -      </IntlProvider> -    ), -  ],  } as ComponentMeta<typeof SearchForm>;  const Template: ComponentStory<typeof SearchForm> = (args) => ( diff --git a/src/components/organisms/forms/search-form.test.tsx b/src/components/organisms/forms/search-form.test.tsx index 4e3d285..59a2f68 100644 --- a/src/components/organisms/forms/search-form.test.tsx +++ b/src/components/organisms/forms/search-form.test.tsx @@ -3,14 +3,14 @@ import SearchForm from './search-form';  describe('SearchForm', () => {    it('renders a search input', () => { -    render(<SearchForm />); +    render(<SearchForm searchPage="#" />);      expect(        screen.getByRole('searchbox', { name: 'Search for:' })      ).toBeInTheDocument();    });    it('renders a submit button', () => { -    render(<SearchForm />); +    render(<SearchForm searchPage="#" />);      expect(screen.getByRole('button', { name: 'Search' })).toBeInTheDocument();    });  }); diff --git a/src/components/organisms/forms/search-form.tsx b/src/components/organisms/forms/search-form.tsx index 18b7c08..56d3895 100644 --- a/src/components/organisms/forms/search-form.tsx +++ b/src/components/organisms/forms/search-form.tsx @@ -4,18 +4,24 @@ import MagnifyingGlass from '@components/atoms/icons/magnifying-glass';  import LabelledField, {    type LabelledFieldProps,  } from '@components/molecules/forms/labelled-field'; +import { useRouter } from 'next/router';  import { FC, useState } from 'react';  import { useIntl } from 'react-intl';  import styles from './search-form.module.scss'; -export type SearchFormProps = Pick<LabelledFieldProps, 'hideLabel'>; +export type SearchFormProps = Pick<LabelledFieldProps, 'hideLabel'> & { +  /** +   * The search page url. +   */ +  searchPage: string; +};  /**   * SearchForm component   *   * Render a search form.   */ -const SearchForm: FC<SearchFormProps> = ({ hideLabel }) => { +const SearchForm: FC<SearchFormProps> = ({ hideLabel, searchPage }) => {    const intl = useIntl();    const fieldLabel = intl.formatMessage({      defaultMessage: 'Search for:', @@ -28,10 +34,12 @@ const SearchForm: FC<SearchFormProps> = ({ hideLabel }) => {      id: 'WMqQrv',    }); +  const router = useRouter();    const [value, setValue] = useState<string>('');    const submitHandler = () => { -    return; +    router.push({ pathname: searchPage, query: { s: value } }); +    setValue('');    };    return ( diff --git a/src/components/organisms/layout/header.stories.tsx b/src/components/organisms/layout/header.stories.tsx index c58c344..98d6377 100644 --- a/src/components/organisms/layout/header.stories.tsx +++ b/src/components/organisms/layout/header.stories.tsx @@ -1,5 +1,4 @@  import { ComponentMeta, ComponentStory } from '@storybook/react'; -import { IntlProvider } from 'react-intl';  import HeaderComponent from './header';  /** @@ -10,6 +9,7 @@ export default {    component: HeaderComponent,    args: {      isHome: false, +    searchPage: '#',      withLink: false,    },    argTypes: { @@ -95,13 +95,6 @@ export default {        },      },    }, -  decorators: [ -    (Story) => ( -      <IntlProvider locale="en"> -        <Story /> -      </IntlProvider> -    ), -  ],    parameters: {      layout: 'fullscreen',    }, diff --git a/src/components/organisms/layout/header.test.tsx b/src/components/organisms/layout/header.test.tsx index 05baaec..a9896f8 100644 --- a/src/components/organisms/layout/header.test.tsx +++ b/src/components/organisms/layout/header.test.tsx @@ -14,14 +14,22 @@ const title = 'Assumenda quis quod';  describe('Header', () => {    it('renders the website title', () => { -    render(<Header title={title} photo={photo} nav={nav} isHome={true} />); +    render( +      <Header +        searchPage="#" +        title={title} +        photo={photo} +        nav={nav} +        isHome={true} +      /> +    );      expect(        screen.getByRole('heading', { level: 1, name: title })      ).toBeInTheDocument();    });    it('renders the main nav', () => { -    render(<Header title={title} photo={photo} nav={nav} />); +    render(<Header searchPage="#" title={title} photo={photo} nav={nav} />);      expect(screen.getByRole('navigation')).toBeInTheDocument();    });  }); diff --git a/src/components/organisms/layout/header.tsx b/src/components/organisms/layout/header.tsx index f6ebc9c..18ebb31 100644 --- a/src/components/organisms/layout/header.tsx +++ b/src/components/organisms/layout/header.tsx @@ -5,28 +5,25 @@ import { FC } from 'react';  import Toolbar, { type ToolbarProps } from '../toolbar/toolbar';  import styles from './header.module.scss'; -export type HeaderProps = BrandingProps & { -  /** -   * Set additional classnames to the header element. -   */ -  className?: string; -  /** -   * The main nav items. -   */ -  nav: ToolbarProps['nav']; -}; +export type HeaderProps = BrandingProps & +  Pick<ToolbarProps, 'nav' | 'searchPage'> & { +    /** +     * Set additional classnames to the header element. +     */ +    className?: string; +  };  /**   * Header component   *   * Render the website header.   */ -const Header: FC<HeaderProps> = ({ className, nav, ...props }) => { +const Header: FC<HeaderProps> = ({ className, nav, searchPage, ...props }) => {    return (      <header className={`${styles.wrapper} ${className}`}>        <div className={styles.body}>          <Branding {...props} /> -        <Toolbar nav={nav} className={styles.toolbar} /> +        <Toolbar nav={nav} searchPage={searchPage} className={styles.toolbar} />        </div>      </header>    ); diff --git a/src/components/organisms/modals/search-modal.stories.tsx b/src/components/organisms/modals/search-modal.stories.tsx index 3ad6abd..f40696c 100644 --- a/src/components/organisms/modals/search-modal.stories.tsx +++ b/src/components/organisms/modals/search-modal.stories.tsx @@ -1,5 +1,4 @@  import { ComponentMeta, ComponentStory } from '@storybook/react'; -import { IntlProvider } from 'react-intl';  import SearchModal from './search-modal';  /** @@ -8,6 +7,9 @@ import SearchModal from './search-modal';  export default {    title: 'Organisms/Modals',    component: SearchModal, +  args: { +    searchPage: '#', +  },    argTypes: {      className: {        control: { @@ -23,13 +25,6 @@ export default {        },      },    }, -  decorators: [ -    (Story) => ( -      <IntlProvider locale="en"> -        <Story /> -      </IntlProvider> -    ), -  ],  } as ComponentMeta<typeof SearchModal>;  const Template: ComponentStory<typeof SearchModal> = (args) => ( diff --git a/src/components/organisms/modals/search-modal.test.tsx b/src/components/organisms/modals/search-modal.test.tsx index 249c523..7ba08c0 100644 --- a/src/components/organisms/modals/search-modal.test.tsx +++ b/src/components/organisms/modals/search-modal.test.tsx @@ -3,7 +3,7 @@ import SearchModal from './search-modal';  describe('SearchModal', () => {    it('renders a search modal', () => { -    render(<SearchModal />); +    render(<SearchModal searchPage="#" />);      expect(screen.getByText('Search')).toBeInTheDocument();    });  }); diff --git a/src/components/organisms/modals/search-modal.tsx b/src/components/organisms/modals/search-modal.tsx index 0e0ceed..866bc25 100644 --- a/src/components/organisms/modals/search-modal.tsx +++ b/src/components/organisms/modals/search-modal.tsx @@ -1,10 +1,10 @@  import Modal, { type ModalProps } from '@components/molecules/modals/modal';  import { FC } from 'react';  import { useIntl } from 'react-intl'; -import SearchForm from '../forms/search-form'; +import SearchForm, { SearchFormProps } from '../forms/search-form';  import styles from './search-modal.module.scss'; -export type SearchModalProps = { +export type SearchModalProps = Pick<SearchFormProps, 'searchPage'> & {    /**     * Set additional classnames to modal wrapper.     */ @@ -16,7 +16,7 @@ export type SearchModalProps = {   *   * Render a search form modal.   */ -const SearchModal: FC<SearchModalProps> = ({ className }) => { +const SearchModal: FC<SearchModalProps> = ({ className, searchPage }) => {    const intl = useIntl();    const modalTitle = intl.formatMessage({      defaultMessage: 'Search', @@ -26,7 +26,7 @@ const SearchModal: FC<SearchModalProps> = ({ className }) => {    return (      <Modal title={modalTitle} className={`${styles.wrapper} ${className}`}> -      <SearchForm hideLabel={true} /> +      <SearchForm hideLabel={true} searchPage={searchPage} />      </Modal>    );  }; diff --git a/src/components/organisms/toolbar/search.stories.tsx b/src/components/organisms/toolbar/search.stories.tsx index 0421c8c..c6063a0 100644 --- a/src/components/organisms/toolbar/search.stories.tsx +++ b/src/components/organisms/toolbar/search.stories.tsx @@ -9,6 +9,9 @@ import Search from './search';  export default {    title: 'Organisms/Toolbar/Search',    component: Search, +  args: { +    searchPage: '#', +  },    argTypes: {      className: {        control: { @@ -44,13 +47,6 @@ export default {        },      },    }, -  decorators: [ -    (Story) => ( -      <IntlProvider locale="en"> -        <Story /> -      </IntlProvider> -    ), -  ],  } as ComponentMeta<typeof Search>;  const Template: ComponentStory<typeof Search> = ({ diff --git a/src/components/organisms/toolbar/search.test.tsx b/src/components/organisms/toolbar/search.test.tsx index 0ce09d8..a18b679 100644 --- a/src/components/organisms/toolbar/search.test.tsx +++ b/src/components/organisms/toolbar/search.test.tsx @@ -3,17 +3,17 @@ import Search from './search';  describe('Search', () => {    it('renders a button to open search modal', () => { -    render(<Search isActive={false} setIsActive={() => null} />); +    render(<Search searchPage="#" isActive={false} setIsActive={() => null} />);      expect(screen.getByRole('checkbox')).toHaveAccessibleName('Open search');    });    it('renders a button to close search modal', () => { -    render(<Search isActive={true} setIsActive={() => null} />); +    render(<Search searchPage="#" isActive={true} setIsActive={() => null} />);      expect(screen.getByRole('checkbox')).toHaveAccessibleName('Close search');    });    it('renders a search form', () => { -    render(<Search isActive={true} setIsActive={() => null} />); +    render(<Search searchPage="#" isActive={true} setIsActive={() => null} />);      expect(screen.getByRole('searchbox')).toBeInTheDocument();    });  }); diff --git a/src/components/organisms/toolbar/search.tsx b/src/components/organisms/toolbar/search.tsx index 72cd576..a1471ef 100644 --- a/src/components/organisms/toolbar/search.tsx +++ b/src/components/organisms/toolbar/search.tsx @@ -17,12 +17,21 @@ export type SearchProps = {     */    isActive: CheckboxProps['value'];    /** +   * A callback function to execute search. +   */ +  searchPage: SearchModalProps['searchPage']; +  /**     * A callback function to handle button state.     */    setIsActive: CheckboxProps['setValue'];  }; -const Search: FC<SearchProps> = ({ className = '', isActive, setIsActive }) => { +const Search: FC<SearchProps> = ({ +  className = '', +  isActive, +  searchPage, +  setIsActive, +}) => {    const intl = useIntl();    const label = isActive      ? intl.formatMessage({ @@ -53,6 +62,7 @@ const Search: FC<SearchProps> = ({ className = '', isActive, setIsActive }) => {          <MagnifyingGlass />        </Label>        <SearchModal +        searchPage={searchPage}          className={`${sharedStyles.modal} ${searchStyles.modal} ${className}`}        />      </div> diff --git a/src/components/organisms/toolbar/toolbar.stories.tsx b/src/components/organisms/toolbar/toolbar.stories.tsx index 4f9a3de..477cb55 100644 --- a/src/components/organisms/toolbar/toolbar.stories.tsx +++ b/src/components/organisms/toolbar/toolbar.stories.tsx @@ -1,5 +1,4 @@  import { ComponentMeta, ComponentStory } from '@storybook/react'; -import { IntlProvider } from 'react-intl';  import ToolbarComponent from './toolbar';  /** @@ -8,6 +7,9 @@ import ToolbarComponent from './toolbar';  export default {    title: 'Organisms/Toolbar',    component: ToolbarComponent, +  args: { +    searchPage: '#', +  },    argTypes: {      className: {        control: { @@ -31,13 +33,6 @@ export default {        },      },    }, -  decorators: [ -    (Story) => ( -      <IntlProvider locale="en"> -        <Story /> -      </IntlProvider> -    ), -  ],    parameters: {      layout: 'fullscreen',    }, diff --git a/src/components/organisms/toolbar/toolbar.test.tsx b/src/components/organisms/toolbar/toolbar.test.tsx index 4bfe8a8..05e84ff 100644 --- a/src/components/organisms/toolbar/toolbar.test.tsx +++ b/src/components/organisms/toolbar/toolbar.test.tsx @@ -10,7 +10,7 @@ const nav = [  describe('Toolbar', () => {    it('renders a navigation menu', () => { -    render(<Toolbar nav={nav} />); +    render(<Toolbar nav={nav} searchPage="#" />);      expect(screen.getByRole('navigation')).toBeInTheDocument();    });  }); diff --git a/src/components/organisms/toolbar/toolbar.tsx b/src/components/organisms/toolbar/toolbar.tsx index f1ce530..6593055 100644 --- a/src/components/organisms/toolbar/toolbar.tsx +++ b/src/components/organisms/toolbar/toolbar.tsx @@ -1,10 +1,10 @@  import { FC, useState } from 'react';  import MainNav, { type MainNavProps } from '../toolbar/main-nav'; -import Search from '../toolbar/search'; +import Search, { type SearchProps } from '../toolbar/search';  import Settings from '../toolbar/settings';  import styles from './toolbar.module.scss'; -export type ToolbarProps = { +export type ToolbarProps = Pick<SearchProps, 'searchPage'> & {    /**     * Set additional classnames to the toolbar wrapper.     */ @@ -20,7 +20,7 @@ export type ToolbarProps = {   *   * Render the website toolbar.   */ -const Toolbar: FC<ToolbarProps> = ({ className = '', nav }) => { +const Toolbar: FC<ToolbarProps> = ({ className = '', nav, searchPage }) => {    const [isNavOpened, setIsNavOpened] = useState<boolean>(false);    const [isSettingsOpened, setIsSettingsOpened] = useState<boolean>(false);    const [isSearchOpened, setIsSearchOpened] = useState<boolean>(false); @@ -34,6 +34,7 @@ const Toolbar: FC<ToolbarProps> = ({ className = '', nav }) => {          className={styles.modal}        />        <Search +        searchPage={searchPage}          isActive={isSearchOpened}          setIsActive={setIsSearchOpened}          className={`${styles.modal} ${styles['modal--search']}`} | 
