-
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.
- Loading branch information
Showing
2 changed files
with
108 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React, { useCallback } from 'react'; | ||
import HCaptcha from '@hcaptcha/react-hcaptcha'; | ||
Check failure on line 2 in src/components/Captcha/index.tsx GitHub Actions / Lint JS
|
||
import { | ||
InputContainer, | ||
InputContainerProps, | ||
} from '@ifrc-go/ui'; | ||
|
||
export type HCaptchaProps<T> = Omit<InputContainerProps, 'input'> & { | ||
name: T, | ||
siteKey: string | undefined; | ||
onChange: (value: string | undefined, name: T) => void; | ||
elementRef?: React.RefObject<HCaptcha>; | ||
}; | ||
|
||
function HCaptchaInput<T extends string>(props: HCaptchaProps<T>) { | ||
const { | ||
actions, | ||
actionsContainerClassName, | ||
className, | ||
disabled, | ||
error, | ||
errorContainerClassName, | ||
hint, | ||
hintContainerClassName, | ||
icons, | ||
iconsContainerClassName, | ||
inputSectionClassName, | ||
label, | ||
readOnly, | ||
|
||
name, | ||
siteKey, | ||
onChange, | ||
elementRef, | ||
} = props; | ||
|
||
const handleVerify = useCallback( | ||
(token: string) => { | ||
onChange(token, name); | ||
}, | ||
[onChange, name], | ||
); | ||
const handleError = useCallback( | ||
(err: string) => { | ||
// eslint-disable-next-line no-console | ||
console.error(err); | ||
onChange(undefined, name); | ||
}, | ||
[onChange, name], | ||
); | ||
const handleExpire = useCallback( | ||
() => { | ||
onChange(undefined, name); | ||
}, | ||
[onChange, name], | ||
); | ||
|
||
return ( | ||
<InputContainer | ||
actions={actions} | ||
actionsContainerClassName={actionsContainerClassName} | ||
className={className} | ||
disabled={disabled} | ||
error={error} | ||
errorContainerClassName={errorContainerClassName} | ||
hint={hint} | ||
hintContainerClassName={hintContainerClassName} | ||
icons={icons} | ||
iconsContainerClassName={iconsContainerClassName} | ||
inputSectionClassName={inputSectionClassName} | ||
label={label} | ||
readOnly={readOnly} | ||
input={siteKey && ( | ||
<HCaptcha | ||
ref={elementRef} | ||
// disabled={disabled || readOnly} | ||
sitekey={siteKey} | ||
onVerify={handleVerify} | ||
onError={handleError} | ||
onExpire={handleExpire} | ||
/> | ||
)} | ||
/> | ||
); | ||
} | ||
|
||
export default HCaptchaInput; |
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