-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 공통 input 코드를 useInput hook으로 분리 (#379)
* feat: 반복적으로 사용하는 Input 코드를 useInput 훅으로 분리 * refactor: errorMessage 상수화 * fix: useInput 분리에 따라 발생한 useSearchInMemberList 에러 해결 * feat: input에 빈문자열이 들어온다면 canSubmit을 false로 invalid한 입력값은 지우고 canSubmit은 true로 변경 * refactor: Input이 하나 인 곳에서 index를 입력하지 않아도 작동되도록 수정
- Loading branch information
Showing
8 changed files
with
157 additions
and
119 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
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,93 @@ | ||
import {useEffect, useState} from 'react'; | ||
|
||
import {ValidateResult} from '@utils/validate/type'; | ||
|
||
import {ERROR_MESSAGE} from '@constants/errorMessage'; | ||
|
||
export type InputValue = { | ||
value: string; | ||
index?: number; | ||
}; | ||
|
||
export type UseInputReturn<T = InputValue> = { | ||
inputList: T[]; | ||
errorMessage: string; | ||
errorIndexList: number[]; | ||
canSubmit: boolean; | ||
handleChange: (index: number, value: string) => void; | ||
setInputList: React.Dispatch<React.SetStateAction<T[]>>; | ||
addErrorIndex: (index: number) => void; | ||
setCanSubmit: React.Dispatch<React.SetStateAction<boolean>>; | ||
}; | ||
|
||
type UseInputProps<T = InputValue> = { | ||
validateFunc: (value: string) => ValidateResult; | ||
initialInputList: T[]; | ||
}; | ||
|
||
const useInput = <T extends InputValue>({validateFunc, initialInputList}: UseInputProps<T>): UseInputReturn<T> => { | ||
const [inputList, setInputList] = useState<T[]>(initialInputList); | ||
const [errorMessage, setErrorMessage] = useState(''); | ||
const [errorIndexList, setErrorIndexList] = useState<number[]>([]); | ||
const [canSubmit, setCanSubmit] = useState(false); | ||
|
||
useEffect(() => { | ||
changeCanSubmit(); | ||
}, [errorMessage, errorIndexList]); | ||
|
||
const handleChange = (index: number = 0, value: string) => { | ||
const {isValid, errorMessage: validationResultMessage} = validateFunc(value); | ||
|
||
if (validationResultMessage === ERROR_MESSAGE.preventEmpty) { | ||
setErrorMessage(validationResultMessage); | ||
updateInputList(index, value); | ||
addErrorIndex(index); | ||
} else if (isValid && value.length !== 0) { | ||
// TODO: (@soha) 쿠키가 작업한 errorMessage를 위로 올리기 변경 추후에 merge후에 반영하기 | ||
setErrorMessage(''); | ||
updateInputList(index, value); | ||
removeErrorIndex(index); | ||
} | ||
}; | ||
|
||
const updateInputList = (index: number, value: string) => { | ||
setInputList(prev => { | ||
const newList = [...prev]; | ||
const targetInput = newList.find(input => input.index === index); | ||
if (targetInput) { | ||
targetInput.value = value; | ||
} | ||
return newList; | ||
}); | ||
}; | ||
|
||
const removeErrorIndex = (index: number) => { | ||
setErrorIndexList(prev => prev.filter(i => i !== index)); | ||
}; | ||
|
||
const addErrorIndex = (index: number) => { | ||
setErrorIndexList(prev => { | ||
if (!prev.includes(index)) { | ||
return [...prev, index]; | ||
} | ||
return prev; | ||
}); | ||
}; | ||
|
||
const changeCanSubmit = () => { | ||
setCanSubmit(errorIndexList.length || errorMessage.length ? false : true); | ||
}; | ||
|
||
return { | ||
inputList, | ||
errorMessage, | ||
errorIndexList, | ||
canSubmit, | ||
handleChange, | ||
setInputList, | ||
addErrorIndex, | ||
setCanSubmit, | ||
}; | ||
}; | ||
|
||
export default useInput; |
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.