From 6cbaf8884cf6cad7afb94e634ceef4d5110f46fe Mon Sep 17 00:00:00 2001 From: joojjang Date: Wed, 20 Nov 2024 11:55:05 +0900 Subject: [PATCH] =?UTF-8?q?fix(SearchBar):=20=EB=B0=B8=EB=A6=AC=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 검색어 없을 때 검색 안 되도록 --- src/components/layouts/SearchBar/index.tsx | 53 ++++++++++------------ 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/src/components/layouts/SearchBar/index.tsx b/src/components/layouts/SearchBar/index.tsx index f065bf0..105575f 100644 --- a/src/components/layouts/SearchBar/index.tsx +++ b/src/components/layouts/SearchBar/index.tsx @@ -57,28 +57,30 @@ const SearchBar = ({ includeBack = true, includeFavorite = false, goBack }: Sear const activeEnter = (data: { searchWord: string }) => { const { searchWord } = data; - // 검색 기록 업데이트 - const storedData = localStorage.getItem(SEARCH_ARRAY_KEY); - let searchArray = storedData ? JSON.parse(storedData) : []; - const existingIndex = searchArray.findIndex( - (item: { key: string; keyword: string }) => item.keyword === searchWord, - ); - - if (existingIndex !== -1) { - searchArray.splice(existingIndex, 1); + if (!formState.errors.searchWord) { + // 검색 기록 업데이트 + const storedData = localStorage.getItem(SEARCH_ARRAY_KEY); + let searchArray = storedData ? JSON.parse(storedData) : []; + const existingIndex = searchArray.findIndex( + (item: { key: string; keyword: string }) => item.keyword === searchWord, + ); + + if (existingIndex !== -1) { + searchArray.splice(existingIndex, 1); + } + + const newItem = { keyword: searchWord, key: generateRandomKey() }; + searchArray = [newItem, ...searchArray]; + if (searchArray.length > MAX_RECENT_SEARCHES) { + searchArray = searchArray.slice(0, MAX_RECENT_SEARCHES); + } + + localStorage.setItem(SEARCH_ARRAY_KEY, JSON.stringify(searchArray)); + + // 검색 실행 + setSearchParams({ query: searchWord }); + navigate(`/${RouterPath.results}?query=${searchWord}`); } - - const newItem = { keyword: searchWord, key: generateRandomKey() }; - searchArray = [newItem, ...searchArray]; - if (searchArray.length > MAX_RECENT_SEARCHES) { - searchArray = searchArray.slice(0, MAX_RECENT_SEARCHES); - } - - localStorage.setItem(SEARCH_ARRAY_KEY, JSON.stringify(searchArray)); - - // 검색 실행 - setSearchParams({ query: searchWord }); - navigate(`/${RouterPath.results}?query=${searchWord}`); }; const nowSearchWord = watch('searchWord'); @@ -98,9 +100,6 @@ const SearchBar = ({ includeBack = true, includeFavorite = false, goBack }: Sear /> {nowSearchWord.trim().length > 0 && } - {formState.errors.searchWord && ( - {formState.errors.searchWord.message} - )} {includeFavorite && } ); @@ -162,9 +161,3 @@ const CancelIconButton = styled(CancelIcon)` cursor: pointer; color: var(--color-gray-dk); `; - -const ErrorMessage = styled.div` - color: red; - font-size: var(--font-size-sm); - margin-top: 4px; -`;