From 911bb133c8222ce7c348887f3fa5c9ef2df598d4 Mon Sep 17 00:00:00 2001 From: Damian Sznajder Date: Fri, 28 Feb 2020 14:14:14 +0100 Subject: [PATCH] fix: android glitch which closed keyboard on focus --- docs/API.md | 14 ++++++------- package.json | 5 +++-- src/OtpInput.tsx | 2 +- src/index.tsx | 52 +++++++++++++++++++++--------------------------- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/docs/API.md b/docs/API.md index f130146..1deaa7c 100644 --- a/docs/API.md +++ b/docs/API.md @@ -14,10 +14,10 @@ | testIDPrefix | string | false | otpInput-\${inputIndex} | Prefix for testID. | | isRTL | boolean | false | false | Preferably I18nManager.isRTL. | | placeholder | string | false | | Placeholder for the input boxes. | -| style | style (object) | false | [default](./src/index.tsx) | Applied to whole container. | -| focusStyles | style (object) | false | [default](./src/index.tsx) | Applied to the input on focus. | -| inputStyles | style(object) | false | [default](./src/index.tsx) | Applied to single input. | -| inputContainerStyles | style (object) | false | [default](./src/index.tsx) | Applied to each input container. | +| style | style (object) | false | | Applied to whole container. | +| focusStyles | style (object) | false | | Applied to the input on focus. | +| inputStyles | style(object) | false | | Applied to single input. | +| inputContainerStyles | style (object) | false | | Applied to each input container. | | ...restTextInputProps | | | | [TextInput](https://facebook.github.io/react-native/docs/textinput) | # Methods @@ -32,9 +32,9 @@ Those can be called on ref: ## Example ```tsx -import React, { Component } from "react"; -import { Button, View } from "react-native"; -import OtpInputs from "react-native-otp-inputs"; +import React, { Component } from 'react'; +import { Button, View } from 'react-native'; +import OtpInputs from 'react-native-otp-inputs'; export default class App extends Component { otpRef = React.createRef(); diff --git a/package.json b/package.json index 5371b1c..0e9987d 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,9 @@ }, "jest": { "preset": "react-native", - "setupFiles": ["/jest/setup.ts"], + "setupFiles": [ + "/jest/setup.ts" + ], "modulePathIgnorePatterns": [ "/Example/node_modules", "/lib/" @@ -80,7 +82,6 @@ "prettier": { "bracketSpacing": true, "jsxBracketSameLine": false, - "parser": "typescript", "printWidth": 80, "semi": true, "singleQuote": true, diff --git a/src/OtpInput.tsx b/src/OtpInput.tsx index 9d58152..f436761 100644 --- a/src/OtpInput.tsx +++ b/src/OtpInput.tsx @@ -75,4 +75,4 @@ const OtpInput = forwardRef( }, ); -export default OtpInput; +export default React.memo(OtpInput); diff --git a/src/index.tsx b/src/index.tsx index bf62a98..9ef6a71 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -10,15 +10,15 @@ import React, { import { Clipboard, Keyboard, - StyleProp, + NativeSyntheticEvent, + Platform, + StyleSheet, TextInput, + TextInputKeyPressEventData, TextInputProps, TextStyle, View, ViewStyle, - Platform, - TextInputKeyPressEventData, - NativeSyntheticEvent, } from 'react-native'; import OtpInput from './OtpInput'; @@ -38,12 +38,12 @@ type Props = TextInputProps & { | 'twitter' | 'web-search' | undefined; - style?: StyleProp; - focusStyles?: StyleProp; + style?: ViewStyle; + focusStyles?: ViewStyle; defaultValue?: string; handleChange: (otpCode: string) => void; - inputContainerStyles?: StyleProp; - inputStyles?: StyleProp; + inputContainerStyles?: ViewStyle; + inputStyles?: TextStyle; isRTL?: boolean; numberOfInputs: number; testIDPrefix?: string; @@ -103,39 +103,32 @@ const reducer = (state: ReducerState, { type, payload }: Actions) => { } }; +const styles = StyleSheet.create({ + container: { + flex: 1, + flexDirection: 'row', + alignItems: 'center', + justifyContent: 'space-between', + }, +}); + const OtpInputs = forwardRef( ( { autoCapitalize = 'none', clearTextOnFocus = false, defaultValue, - focusStyles = { - borderColor: '#00f', - }, + focusStyles, handleChange = console.log, - inputContainerStyles = { - borderBottomWidth: 1, - height: 53, - margin: 10, - }, - inputStyles = { - fontSize: 24, - paddingTop: 10, - textAlign: 'center', - width: 40, - }, + inputContainerStyles, + inputStyles, isRTL = false, keyboardType = 'phone-pad', numberOfInputs = 4, placeholder = '', secureTextEntry = false, selectTextOnFocus = true, - style = { - flex: 1, - flexDirection: 'row', - alignItems: 'center', - justifyContent: 'space-between', - }, + style, testIDPrefix = 'otpInput', ...restProps }, @@ -321,6 +314,7 @@ const OtpInputs = forwardRef( })} numberOfInputs={numberOfInputs} placeholder={placeholder} + // @ts-ignore ref={inputs.current[inputIndex]} secureTextEntry={secureTextEntry} selectTextOnFocus={selectTextOnFocus} @@ -331,7 +325,7 @@ const OtpInputs = forwardRef( }); }; - return {renderInputs()}; + return {renderInputs()}; }, );