generated from morewings/react-library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(input-validation): add validationState prop
fix #373
- Loading branch information
Showing
6 changed files
with
68 additions
and
6 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,28 @@ | ||
import type {Dispatch, SetStateAction, MutableRefObject} from 'react'; | ||
import {useEffect} from 'react'; | ||
|
||
import {ValidationState} from '@/internal/inputs'; | ||
|
||
export type Props = { | ||
validationState?: keyof typeof ValidationState; | ||
setValidity: Dispatch<SetStateAction<keyof typeof ValidationState | null>>; | ||
inputRef: MutableRefObject<HTMLInputElement | HTMLTextAreaElement | null>; | ||
}; | ||
|
||
/** | ||
* React hook designed to contain effects which synchronize input validation | ||
* with external validation state via prop or context (TODO). | ||
* NB! On change validation takes preference. | ||
* @see ValidationState | ||
*/ | ||
export const useSyncValidation = ({validationState, inputRef, setValidity}: Props) => { | ||
useEffect(() => { | ||
if (validationState === ValidationState.error && inputRef.current) { | ||
inputRef.current.setCustomValidity(ValidationState.error); | ||
setValidity(ValidationState.error); | ||
} else if (validationState && inputRef.current) { | ||
inputRef.current.setCustomValidity(''); | ||
setValidity(validationState); | ||
} | ||
}, [inputRef, setValidity, validationState]); | ||
}; |
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