-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Выделить из Header отдельную feature - SearchProduct (#175)
* refactor: Выделить из Header отдельную feature - SearchProduct #171 * hotfix: fix CamelCase issue with SCSS module files * refactor: Заменил querySelector на useRef * documentation: add stories for SearchProduct, SearchResult, SearchItem
- Loading branch information
Showing
13 changed files
with
180 additions
and
161 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Meta, Story } from '@storybook/react' | ||
import { searchResponseData } from '@/mockData/searchData' | ||
import SearchItem from './SearchItem' | ||
import { TProduct } from '@/shared/model/types/common' | ||
import { TLinkProps } from '@/shared/ui/Link/Link' | ||
|
||
export default { | ||
title: 'Entities/SearchItem', | ||
component: SearchItem, | ||
parameters: { | ||
layout: 'centered' | ||
} | ||
} as Meta | ||
|
||
type StoryProps = TProduct & TLinkProps | ||
|
||
const Template: Story<StoryProps> = args => <SearchItem {...args} /> | ||
|
||
export const Default: Story<StoryProps> = Template.bind({}) | ||
Default.args = { | ||
...(searchResponseData.data[2] as StoryProps) | ||
} |
23 changes: 12 additions & 11 deletions
23
src/components/SearchItem/SearchItem.tsx → src/entities/SearchItem/SearchItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import SearchProduct from './ui/SearchProduct' | ||
export default SearchProduct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Meta, Story } from '@storybook/react' | ||
import SearchProduct from './SearchProduct' | ||
|
||
export default { | ||
title: 'Features/SearchProduct', | ||
component: SearchProduct | ||
} as Meta | ||
|
||
const Template: Story = () => <SearchProduct /> | ||
|
||
export const Default = Template.bind({}) | ||
Default.args = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { useRef, useEffect, useState } from 'react' | ||
import { searchResponseData } from '@/mockData/searchData' | ||
import { TResultData } from '@/shared/model/types/common' | ||
import SearchResult from '@/widgets/SearchResult/SearchResult' | ||
import { Input, InputSize, InputTheme } from '@/shared/ui/Input/Input' | ||
import { Button, ButtonDesign, ButtonSize, ButtonTheme } from '@/shared/ui/Button/Button' | ||
import styles from './SearchProduct.module.scss' | ||
|
||
// @TODO: Перевести форму на Formik + Yup | ||
// https://github.com/Studio-Yandex-Practicum/maxboom_frontend/issues/92 | ||
|
||
/** | ||
* Компонент формы для поиска катгорий/товаров по базе данных магазина | ||
*/ | ||
const SearchProduct = () => { | ||
const [visible, setVisibility] = useState(false) | ||
const [resultData, setResultData] = useState<TResultData>({ data: [], success: false }) | ||
const [query, setQuery] = useState('') | ||
const searchResultRef = useRef(null) | ||
|
||
// @TODO: Добавить интеграцию с бэком - подсказки в поиске при вводе текста | ||
// https://github.com/Studio-Yandex-Practicum/maxboom_frontend/issues/172 | ||
const inputEventHandler = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const { value } = event.target | ||
setResultData(searchResponseData) | ||
setQuery(value) | ||
} | ||
|
||
const closeContextMenuHandler = (e: Event) => { | ||
const searchResultNode = searchResultRef.current | ||
|
||
if (searchResultNode) { | ||
const withinBoundaries = e.composedPath().includes(searchResultNode) | ||
|
||
if (!withinBoundaries && visible) { | ||
setVisibility(false) | ||
} | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (resultData.success && query.length > 0) { | ||
setVisibility(true) | ||
} else { | ||
setVisibility(false) | ||
} | ||
}, [resultData, query]) | ||
|
||
useEffect(() => { | ||
document.addEventListener('click', closeContextMenuHandler) | ||
return () => { | ||
document.removeEventListener('click', closeContextMenuHandler) | ||
} | ||
}, []) | ||
|
||
return ( | ||
<form className={styles.form}> | ||
<Input | ||
name="search" | ||
placeholder="Поиск по товарам и категориям" | ||
customSize={InputSize.M} | ||
theme={InputTheme.ACCENT} | ||
onChange={inputEventHandler} | ||
/> | ||
{/* @TODO: Добавить onClick-интеграцию с бэком для отправки поискового запроса | ||
https://github.com/Studio-Yandex-Practicum/maxboom_frontend/issues/173 */} | ||
<Button | ||
theme={ButtonTheme.PRIMARY} | ||
design={ButtonDesign.SQUARE} | ||
size={ButtonSize.XS} | ||
className={styles.button}> | ||
Найти | ||
</Button> | ||
{visible && <SearchResult results={resultData.data} ref={searchResultRef} />} | ||
</form> | ||
) | ||
} | ||
|
||
export default SearchProduct |
2 changes: 2 additions & 0 deletions
2
...nts/searchResult/searchResult.module.scss → ...ets/SearchResult/SearchResult.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.