-
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
1 parent
8a9bf6c
commit f217e68
Showing
8 changed files
with
226 additions
and
26 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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import React from 'react'; | ||
import { PaperProvider } from 'react-native-paper'; | ||
import App from '../src/App'; | ||
export default function RootLayout() { | ||
return <App />; | ||
return ( | ||
<PaperProvider> | ||
<App /> | ||
</PaperProvider> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
{ | ||
"extends": "../tsconfig", | ||
"compilerOptions": { | ||
// Avoid expo-cli auto-generating a tsconfig | ||
} | ||
"compilerOptions": {}, | ||
"include": [ | ||
"**/*.ts", | ||
"**/*.tsx", | ||
".expo/types/**/*.ts", | ||
"expo-env.d.ts" | ||
] | ||
} |
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,121 @@ | ||
import React from 'react'; | ||
import { | ||
View, | ||
StyleSheet, | ||
Pressable, | ||
TextInput as RNTextInput, | ||
} from 'react-native'; | ||
import { TextInput, Text } from 'react-native-paper'; | ||
|
||
type OtpInputProps = { | ||
maxLength: number; | ||
autoFocus?: boolean; | ||
onPinReady?: (pin: string) => void; | ||
}; | ||
|
||
export default function OtpInput({ | ||
maxLength, | ||
onPinReady, | ||
autoFocus = true, | ||
}: OtpInputProps) { | ||
const [isInputBoxFocused, setIsInputBoxFocused] = React.useState(autoFocus); | ||
const [otp, setOtp] = React.useState(''); | ||
const [isPinReady, setIsPinReady] = React.useState(false); | ||
const ref = React.useRef<RNTextInput>(null); | ||
|
||
React.useEffect(() => { | ||
setIsPinReady(otp.length === maxLength); | ||
return () => { | ||
setIsPinReady(false); | ||
}; | ||
}, [maxLength, otp, setIsPinReady]); | ||
|
||
React.useEffect(() => { | ||
if (isPinReady) { | ||
onPinReady && onPinReady(otp); | ||
} | ||
}, [isPinReady, onPinReady, otp]); | ||
|
||
const boxArray = new Array(maxLength).fill(0); | ||
const handleOnPress = () => { | ||
setIsInputBoxFocused(true); | ||
ref?.current?.focus(); | ||
}; | ||
|
||
const handleOnBlur = () => { | ||
setIsInputBoxFocused(false); | ||
}; | ||
return ( | ||
<View style={styles.container}> | ||
<TextInput | ||
mode="outlined" | ||
style={styles.textInput} | ||
theme={{ | ||
roundness: 10, | ||
}} | ||
value={otp} | ||
onChangeText={setOtp} | ||
maxLength={maxLength} | ||
ref={ref} | ||
onBlur={handleOnBlur} | ||
keyboardType="numeric" | ||
autoFocus={autoFocus} | ||
/> | ||
<Pressable style={styles.otpContainer} onPress={handleOnPress}> | ||
{boxArray.map((_, index) => { | ||
const isCurrentValue = index === otp.length; | ||
const isLastValue = index === maxLength - 1; | ||
const isCodeComplete = otp.length === maxLength; | ||
|
||
const isValueFocused = | ||
isCurrentValue || (isLastValue && isCodeComplete); | ||
return ( | ||
<View | ||
key={index} | ||
style={{ | ||
...styles.otpBox, | ||
borderColor: | ||
isInputBoxFocused && isValueFocused ? '#6200EE' : '#F6F6F6', | ||
}} | ||
> | ||
<Text style={styles.otpText}>{otp[index] || ''}</Text> | ||
</View> | ||
); | ||
})} | ||
</Pressable> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
paddingHorizontal: 0, | ||
}, | ||
textInput: { | ||
position: 'absolute', | ||
opacity: 0, | ||
}, | ||
otpContainer: { | ||
width: '100%', | ||
flexDirection: 'row', | ||
justifyContent: 'space-evenly', | ||
}, | ||
otpBox: { | ||
backgroundColor: '#F6F6F6', | ||
borderWidth: 2, | ||
borderRadius: 5, | ||
padding: 12, | ||
maxWidth: 45, | ||
minWidth: 45, | ||
maxHeight: 45, | ||
minHeight: 45, | ||
justifyContent: 'center', | ||
}, | ||
otpText: { | ||
fontSize: 15, | ||
color: 'black', | ||
textAlign: 'center', | ||
}, | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export function multiply(a: number, b: number): Promise<number> { | ||
return Promise.resolve(a * b); | ||
} | ||
import PaperOtpInput from './components/OtpInput'; | ||
|
||
export { PaperOtpInput }; |
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