-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from dribble-njr/feature/common-comps
feat: captcha input and password input
- Loading branch information
Showing
3 changed files
with
111 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ThemedView } from '@/components/ThemedView'; | ||
import { useField } from 'formik'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { HelperText, TextInput, TextInputProps } from 'react-native-paper'; | ||
import { useState, useEffect, useRef } from 'react'; | ||
|
||
interface CaptchaInputProps extends TextInputProps { | ||
i18nKey: string; | ||
name: string; | ||
onSendCaptcha: () => Promise<void>; | ||
} | ||
|
||
export default function CaptchaInput({ i18nKey, name, onSendCaptcha, ...textInputProps }: CaptchaInputProps) { | ||
const [field, meta, helpers] = useField(name); | ||
const hasError = meta.touched && Boolean(meta.error); | ||
const { t } = useTranslation(); | ||
const [countdown, setCountdown] = useState(0); | ||
const timerRef = useRef<ReturnType<typeof setTimeout>>(); | ||
|
||
useEffect(() => { | ||
if (countdown > 0) { | ||
timerRef.current = setTimeout(() => { | ||
setCountdown((prev) => prev - 1); | ||
}, 1000); | ||
} | ||
return () => { | ||
if (timerRef.current) { | ||
clearTimeout(timerRef.current); | ||
} | ||
}; | ||
}, [countdown]); | ||
|
||
const handleSendCaptcha = async () => { | ||
await onSendCaptcha(); | ||
setCountdown(60); | ||
}; | ||
|
||
return ( | ||
<ThemedView> | ||
<TextInput | ||
mode="outlined" | ||
label={t(`${i18nKey}.${name}`)} | ||
value={field.value} | ||
error={hasError} | ||
placeholder={`${t('common.enter')}${t(`${i18nKey}.${name}`)}...`} | ||
onChangeText={(value) => helpers.setValue(value)} | ||
onBlur={() => helpers.setTouched(true)} | ||
right={ | ||
<TextInput.Affix | ||
text={countdown > 0 ? `${countdown}s` : t('common.sendCaptcha')} | ||
onPress={countdown === 0 ? handleSendCaptcha : undefined} | ||
/> | ||
} | ||
{...textInputProps} | ||
/> | ||
{hasError && <HelperText type="error">{meta.error}</HelperText>} | ||
</ThemedView> | ||
); | ||
} |
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,41 @@ | ||
import { ThemedView } from '@/components/ThemedView'; | ||
import { useField } from 'formik'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { HelperText, TextInput, TextInputProps } from 'react-native-paper'; | ||
import { useState } from 'react'; | ||
import { Keyboard } from 'react-native'; | ||
|
||
interface PasswordInputProps extends TextInputProps { | ||
i18nKey: string; | ||
name: string; | ||
} | ||
|
||
export default function PasswordInput({ i18nKey, name, ...textInputProps }: PasswordInputProps) { | ||
const [field, meta, helpers] = useField(name); | ||
const hasError = meta.touched && Boolean(meta.error); | ||
const { t } = useTranslation(); | ||
const [showPassword, setShowPassword] = useState(false); | ||
|
||
const handleTogglePassword = () => { | ||
Keyboard.dismiss(); | ||
setShowPassword(!showPassword); | ||
}; | ||
|
||
return ( | ||
<ThemedView> | ||
<TextInput | ||
mode="outlined" | ||
label={t(`${i18nKey}.${name}`)} | ||
value={field.value} | ||
error={hasError} | ||
placeholder={`${t('common.enter')}${t(`${i18nKey}.${name}`)}...`} | ||
onChangeText={(value) => helpers.setValue(value)} | ||
onBlur={() => helpers.setTouched(true)} | ||
secureTextEntry={!showPassword} | ||
right={<TextInput.Icon icon={showPassword ? 'eye-off' : 'eye'} onPress={handleTogglePassword} />} | ||
{...textInputProps} | ||
/> | ||
{hasError && <HelperText type="error">{meta.error}</HelperText>} | ||
</ThemedView> | ||
); | ||
} |