Skip to content

Commit 44f4f91

Browse files
author
Damian Sznajder
committed
refactor: lint and cleanup code
1 parent 088849d commit 44f4f91

File tree

8 files changed

+1751
-1378
lines changed

8 files changed

+1751
-1378
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
coverage/
33
dist/
44
lib/
5+
Example/

.eslintrc.js

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,41 @@ module.exports = {
1111
version: 'detect',
1212
},
1313
'import/core-modules': ['react-native-otp-inputs'],
14-
'import/resolver': {
15-
node: {
16-
extensions: ['.js', '.jsx', '.ts', '.tsx'],
17-
},
18-
},
1914
},
2015

16+
plugins: ['simple-import-sort'],
2117
extends: ['satya164', 'plugin:react-native/all'],
2218

2319
rules: {
2420
'babel/no-unused-expressions': 'off',
21+
2522
'import/extensions': 'off',
2623
'import/named': 'off',
24+
'import/no-unresolved': 'error',
2725
'jest/consistent-test-it': ['error', { fn: 'test' }],
2826
'jest/no-truthy-falsy': 'off',
29-
'react-hooks/exhaustive-deps': 'warn',
30-
'react-hooks/rules-of-hooks': 'error',
31-
'react/no-unused-prop-types': 'off',
27+
'jest/expect-expect': ['error', { assertFunctionNames: ['expect', 'element'] }],
3228

33-
'import/no-unresolved': [
29+
'simple-import-sort/exports': 'error',
30+
'simple-import-sort/imports': [
3431
'error',
3532
{
36-
caseSensitive: false,
37-
ignore: ['^(~|jest)/'],
33+
groups: [
34+
// Side effect imports.
35+
['^\\u0000'],
36+
// Packages.
37+
['^@?\\w'],
38+
['^../'],
39+
['^./'],
40+
],
3841
},
3942
],
40-
41-
'jest/expect-expect': [
42-
'error',
43-
{ assertFunctionNames: ['expect', 'element'] },
44-
],
45-
4643
'prettier/prettier': [
4744
'error',
4845
{
4946
bracketSpacing: true,
50-
jsxBracketSameLine: false,
51-
parser: 'typescript',
52-
printWidth: 80,
47+
bracketSameLine: false,
48+
printWidth: 100,
5349
semi: true,
5450
singleQuote: true,
5551
tabWidth: 2,
@@ -58,27 +54,14 @@ module.exports = {
5854
},
5955
],
6056

61-
'@typescript-eslint/array-type': [
62-
'error',
63-
{ default: 'generic', readonly: 'generic' },
64-
],
57+
'react/jsx-sort-props': ['error', { shorthandFirst: true }],
58+
'react-hooks/exhaustive-deps': 'warn',
59+
'react-hooks/rules-of-hooks': 'error',
60+
'react-native/no-raw-text': 'off',
61+
'react-native/no-color-literals': 'off',
62+
'react/no-unused-prop-types': 'off',
6563

66-
'import/order': [
67-
'error',
68-
{
69-
'newlines-between': 'always-and-inside-groups',
70-
groups: [
71-
'internal',
72-
'external',
73-
'unknown',
74-
'index',
75-
'sibling',
76-
'parent',
77-
'builtin',
78-
],
79-
alphabetize: { order: 'ignore' },
80-
},
81-
],
64+
'@typescript-eslint/array-type': ['error', { default: 'generic', readonly: 'generic' }],
8265
},
8366
globals: {
8467
__DEV__: true,

babel.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
module.exports = {
2-
presets: [
3-
'module:metro-react-native-babel-preset',
4-
'@babel/preset-typescript',
5-
],
2+
presets: ['module:metro-react-native-babel-preset', '@babel/preset-typescript'],
63
};

package.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,29 @@
3232
"inputs"
3333
],
3434
"devDependencies": {
35-
"@babel/core": "7.13.14",
36-
"@babel/preset-typescript": "7.13.0",
37-
"@commitlint/config-conventional": "12.0.1",
35+
"@babel/core": "7.15.8",
36+
"@babel/preset-typescript": "7.15.0",
37+
"@commitlint/config-conventional": "13.2.0",
38+
"@react-native-clipboard/clipboard": "1.9.0",
3839
"@react-native-community/bob": "0.17.1",
39-
"@react-native-clipboard/clipboard": "1.7.0",
40-
"@release-it/conventional-changelog": "2.0.1",
41-
"@types/jest": "26.0.22",
42-
"@types/react": "17.0.3",
43-
"@types/react-native": "0.64.2",
44-
"commitlint": "12.0.1",
40+
"@release-it/conventional-changelog": "3.3.0",
41+
"@types/jest": "27.0.2",
42+
"@types/react": "17.0.28",
43+
"@types/react-native": "0.65.5",
44+
"commitlint": "13.2.1",
4545
"eslint": "7.23.0",
4646
"eslint-config-satya164": "3.1.10",
47-
"husky": "6.0.0",
48-
"jest": "26.6.3",
49-
"metro-react-native-babel-preset": "0.65.2",
50-
"prettier": "2.2.1",
51-
"react": "17.0.1",
52-
"react-native": "0.64.0",
53-
"react-native-builder-bob": "0.18.1",
54-
"react-test-renderer": "17.0.1",
55-
"release-it": "14.5.0",
56-
"typescript": "4.2.3"
47+
"eslint-plugin-simple-import-sort": "7.0.0",
48+
"husky": "7.0.2",
49+
"jest": "27.2.5",
50+
"metro-react-native-babel-preset": "0.66.2",
51+
"prettier": "2.4.1",
52+
"react": "17.0.2",
53+
"react-native": "0.66.0",
54+
"react-native-builder-bob": "0.18.2",
55+
"react-test-renderer": "17.0.2",
56+
"release-it": "14.11.6",
57+
"typescript": "4.4.3"
5758
},
5859
"peerDependencies": {
5960
"@react-native-clipboard/clipboard": "*",
@@ -70,9 +71,8 @@
7071
},
7172
"prettier": {
7273
"bracketSpacing": true,
73-
"jsxBracketSameLine": false,
74-
"parser": "typescript",
75-
"printWidth": 80,
74+
"bracketSameLine": false,
75+
"printWidth": 100,
7676
"semi": true,
7777
"singleQuote": true,
7878
"tabWidth": 2,

src/OtpInput.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import React, { useState, useEffect, forwardRef, RefObject } from 'react';
1+
import React, { forwardRef, RefObject, useEffect, useMemo, useState } from 'react';
22
import {
3+
NativeSyntheticEvent,
34
Platform,
45
StyleProp,
56
TextInput,
7+
TextInputKeyPressEventData,
8+
TextInputProps,
69
TextStyle,
710
View,
811
ViewStyle,
9-
TextInputProps,
10-
NativeSyntheticEvent,
11-
TextInputKeyPressEventData,
1212
} from 'react-native';
1313

1414
type Props = TextInputProps & {
@@ -19,9 +19,7 @@ type Props = TextInputProps & {
1919
numberOfInputs: number;
2020
handleTextChange: (text: string) => void;
2121
inputValue: string;
22-
handleKeyPress: (
23-
keyPressEvent: NativeSyntheticEvent<TextInputKeyPressEventData>,
24-
) => void;
22+
handleKeyPress: (keyPressEvent: NativeSyntheticEvent<TextInputKeyPressEventData>) => void;
2523
};
2624

2725
const majorVersionIOS: number = parseInt(`${Platform.Version}`, 10);
@@ -30,6 +28,7 @@ const isOTPSupported: boolean = Platform.OS === 'ios' && majorVersionIOS >= 12;
3028
const OtpInput = forwardRef<TextInput, Props>(
3129
(
3230
{
31+
autoFocus,
3332
focusStyles,
3433
handleKeyPress,
3534
handleTextChange,
@@ -52,15 +51,26 @@ const OtpInput = forwardRef<TextInput, Props>(
5251
});
5352
}, [ref, inputValue]);
5453

54+
const restProps = useMemo(
55+
() =>
56+
Platform.select({
57+
default: rest,
58+
web: { value: inputValue, ...rest },
59+
}),
60+
[inputValue, rest],
61+
);
62+
5563
return (
5664
<View style={[inputContainerStyles, focused && focusStyles]}>
5765
<TextInput
66+
autoFocus={autoFocus}
5867
onBlur={() => setFocused(false)}
5968
onChangeText={handleTextChange}
6069
onFocus={() => setFocused(true)}
6170
onKeyPress={handleKeyPress}
6271
placeholder={placeholder}
6372
ref={ref}
73+
secureTextEntry={secureTextEntry}
6474
// https://github.com/facebook/react-native/issues/18339
6575
selectTextOnFocus={Platform.select({
6676
ios: selectTextOnFocus,
@@ -69,8 +79,7 @@ const OtpInput = forwardRef<TextInput, Props>(
6979
style={inputStyles}
7080
textContentType={isOTPSupported ? 'oneTimeCode' : 'none'}
7181
underlineColorAndroid="transparent"
72-
secureTextEntry={secureTextEntry}
73-
{...rest}
82+
{...restProps}
7483
/>
7584
</View>
7685
);

src/index.tsx

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Clipboard from '@react-native-clipboard/clipboard';
22
import React, {
3-
RefObject,
43
forwardRef,
4+
RefObject,
55
useCallback,
66
useEffect,
77
useImperativeHandle,
@@ -22,10 +22,10 @@ import {
2222
ViewStyle,
2323
} from 'react-native';
2424

25-
import OtpInput from './OtpInput';
26-
import { OtpInputsRef, SupportedKeyboardType } from './types';
2725
import { fillOtpCode } from './helpers';
26+
import OtpInput from './OtpInput';
2827
import reducer from './reducer';
28+
import { OtpInputsRef, SupportedKeyboardType } from './types';
2929

3030
const supportAutofillFromClipboard =
3131
Platform.OS === 'android' || parseInt(Platform.Version as string, 10) < 14;
@@ -47,16 +47,17 @@ type Props = TextInputProps & {
4747

4848
const styles = StyleSheet.create({
4949
container: {
50+
alignItems: 'center',
5051
flex: 1,
5152
flexDirection: 'row',
52-
alignItems: 'center',
5353
justifyContent: 'space-between',
5454
},
5555
});
5656

5757
const OtpInputs = forwardRef<OtpInputsRef, Props>(
5858
(
5959
{
60+
autoFocus,
6061
autofillFromClipboard = supportAutofillFromClipboard,
6162
autofillListenerIntervalMS = 1000,
6263
autoCapitalize = 'none',
@@ -80,15 +81,11 @@ const OtpInputs = forwardRef<OtpInputsRef, Props>(
8081
) => {
8182
const previousCopiedText = useRef<string>('');
8283
const inputs = useRef<Array<RefObject<TextInput>>>([]);
83-
const [{ otpCode, hasKeySupport }, dispatch] = useReducer(
84-
reducer,
85-
{},
86-
() => ({
87-
otpCode: fillOtpCode(numberOfInputs, defaultValue),
88-
handleChange,
89-
hasKeySupport: Platform.OS === 'ios',
90-
}),
91-
);
84+
const [{ otpCode, hasKeySupport }, dispatch] = useReducer(reducer, {}, () => ({
85+
otpCode: fillOtpCode(numberOfInputs, defaultValue),
86+
handleChange,
87+
hasKeySupport: Platform.OS === 'ios',
88+
}));
9289

9390
useEffect(() => {
9491
dispatch({ type: 'setHandleChange', payload: handleChange });
@@ -149,12 +146,11 @@ const OtpInputs = forwardRef<OtpInputsRef, Props>(
149146
};
150147

151148
const handleKeyPress = (
152-
{
153-
nativeEvent: { key },
154-
}: NativeSyntheticEvent<TextInputKeyPressEventData>,
149+
{ nativeEvent: { key } }: NativeSyntheticEvent<TextInputKeyPressEventData>,
155150
index: number,
156151
) => {
157-
handleInputTextChange(key === 'Backspace' ? '' : key, index);
152+
const text = key === 'Backspace' || key.length > 1 ? '' : key;
153+
handleInputTextChange(text, index);
158154

159155
if (Platform.OS === 'android' && !hasKeySupport && !isNaN(parseInt(key)))
160156
dispatch({ type: 'setHasKeySupport', payload: true });
@@ -223,12 +219,7 @@ const OtpInputs = forwardRef<OtpInputsRef, Props>(
223219
return () => {
224220
clearInterval(interval);
225221
};
226-
}, [
227-
autofillFromClipboard,
228-
autofillListenerIntervalMS,
229-
listenOnCopiedText,
230-
numberOfInputs,
231-
]);
222+
}, [autofillFromClipboard, autofillListenerIntervalMS, listenOnCopiedText, numberOfInputs]);
232223

233224
const renderInputs = (): Array<JSX.Element> => {
234225
const iterationArray = Array<number>(numberOfInputs).fill(0);
@@ -246,16 +237,17 @@ const OtpInputs = forwardRef<OtpInputsRef, Props>(
246237

247238
return (
248239
<OtpInput
240+
accessible
241+
accessibilityLabel={`${testIDPrefix}-${inputIndex}`}
249242
autoCapitalize={autoCapitalize}
243+
autoFocus={index === 0 && autoFocus}
250244
clearTextOnFocus={clearTextOnFocus}
251245
firstInput={index === 0}
252246
focusStyles={focusStyles}
253-
handleKeyPress={(
254-
keyPressEvent: NativeSyntheticEvent<TextInputKeyPressEventData>,
255-
) => handleKeyPress(keyPressEvent, inputIndex)}
256-
handleTextChange={(text: string) =>
257-
handleTextChange(text, inputIndex)
247+
handleKeyPress={(keyPressEvent: NativeSyntheticEvent<TextInputKeyPressEventData>) =>
248+
handleKeyPress(keyPressEvent, inputIndex)
258249
}
250+
handleTextChange={(text: string) => handleTextChange(text, inputIndex)}
259251
inputContainerStyles={inputContainerStyles}
260252
inputStyles={inputStyles}
261253
inputValue={inputValue}
@@ -270,9 +262,7 @@ const OtpInputs = forwardRef<OtpInputsRef, Props>(
270262
ref={inputs.current[inputIndex]}
271263
secureTextEntry={secureTextEntry}
272264
selectTextOnFocus={selectTextOnFocus}
273-
accessible
274265
testID={`${testIDPrefix}-${inputIndex}`}
275-
accessibilityLabel={`${testIDPrefix}-${inputIndex}`}
276266
{...restProps}
277267
/>
278268
);

src/reducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fillOtpCode } from './helpers';
2-
import { ActionTypes, ReducerState, Actions } from './types';
2+
import { Actions, ActionTypes, ReducerState } from './types';
33

44
const ACTION_TYPES: ActionTypes = {
55
setHandleChange: 'setHandleChange',

0 commit comments

Comments
 (0)