-
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: 자동완성창 관리하는 로직을 useFocusSearchInput 커스텀 훅으로 분리하고 불필요한 form 형…
…식 제거(#120)
- Loading branch information
Showing
5 changed files
with
89 additions
and
80 deletions.
There are no files selected for viewing
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
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,33 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
interface UseFocusSearchInputProps { | ||
isFocused: boolean; | ||
searchInput: string; | ||
handleBlur: () => void; | ||
} | ||
|
||
const useFocusSearchInput = ({ isFocused, searchInput, handleBlur }: UseFocusSearchInputProps) => { | ||
const inputRef = useRef<HTMLInputElement | null>(null); | ||
const autoCompleteRef = useRef<HTMLDivElement | null>(null); | ||
const isSearching = isFocused && searchInput.length > 0; | ||
|
||
useEffect(() => { | ||
const handleOutsideClose = (e: MouseEvent) => { | ||
const isOutsideArea = | ||
isFocused && | ||
!autoCompleteRef.current?.contains(e.target as Node) && | ||
!inputRef.current?.contains(e.target as Node); | ||
|
||
if (isOutsideArea) { | ||
handleBlur(); | ||
} | ||
}; | ||
document.addEventListener('click', handleOutsideClose); | ||
|
||
return () => document.removeEventListener('click', handleOutsideClose); | ||
}, [isFocused, handleBlur]); | ||
|
||
return { inputRef, autoCompleteRef, isSearching }; | ||
}; | ||
|
||
export default useFocusSearchInput; |
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,31 @@ | ||
import { useRouter } from 'next/navigation'; | ||
import { ChangeEvent, useCallback, useState } from 'react'; | ||
|
||
const useInput = (initialValue: string) => { | ||
const router = useRouter(); | ||
const [value, setValue] = useState(initialValue); | ||
const [isFocused, setIsFocused] = useState(false); | ||
|
||
const handleChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
setValue(e.target.value); | ||
}; | ||
|
||
const handleFocus = () => { | ||
setIsFocused(true); | ||
}; | ||
|
||
const handleBlur = useCallback(() => { | ||
setIsFocused(false); | ||
}, []); | ||
|
||
const handleKeydown = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.code === 'Enter') { | ||
handleBlur(); | ||
router.push(`search/?search=${value}`); | ||
} | ||
}; | ||
|
||
return { value, isFocused, handleChange, handleFocus, handleBlur, handleKeydown }; | ||
}; | ||
|
||
export default useInput; |
This file was deleted.
Oops, something went wrong.