@r5n/otp-react-native 0.0.0-stage → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +104 -0
- package/LICENSE +21 -0
- package/README.md +227 -2
- package/lib/commonjs/Cell.js +140 -0
- package/lib/commonjs/Cell.js.map +1 -0
- package/lib/commonjs/OtpInput.js +129 -0
- package/lib/commonjs/OtpInput.js.map +1 -0
- package/lib/commonjs/accessibility.js +90 -0
- package/lib/commonjs/accessibility.js.map +1 -0
- package/lib/commonjs/clipboard.js +197 -0
- package/lib/commonjs/clipboard.js.map +1 -0
- package/lib/commonjs/dev.js +63 -0
- package/lib/commonjs/dev.js.map +1 -0
- package/lib/commonjs/hiddenInput.js +95 -0
- package/lib/commonjs/hiddenInput.js.map +1 -0
- package/lib/commonjs/index.js +53 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/legacy.js +217 -0
- package/lib/commonjs/legacy.js.map +1 -0
- package/lib/commonjs/managedProps.js +73 -0
- package/lib/commonjs/managedProps.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/types.js +45 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/useOtpController.js +70 -0
- package/lib/commonjs/useOtpController.js.map +1 -0
- package/lib/commonjs/useOtpInput.js +251 -0
- package/lib/commonjs/useOtpInput.js.map +1 -0
- package/lib/module/Cell.js +103 -0
- package/lib/module/Cell.js.map +1 -0
- package/lib/module/OtpInput.js +94 -0
- package/lib/module/OtpInput.js.map +1 -0
- package/lib/module/accessibility.js +49 -0
- package/lib/module/accessibility.js.map +1 -0
- package/lib/module/clipboard.js +156 -0
- package/lib/module/clipboard.js.map +1 -0
- package/lib/module/dev.js +22 -0
- package/lib/module/dev.js.map +1 -0
- package/lib/module/hiddenInput.js +54 -0
- package/lib/module/hiddenInput.js.map +1 -0
- package/lib/module/index.js +14 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/legacy.js +187 -0
- package/lib/module/legacy.js.map +1 -0
- package/lib/module/managedProps.js +32 -0
- package/lib/module/managedProps.js.map +1 -0
- package/lib/module/types.js +3 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/useOtpController.js +29 -0
- package/lib/module/useOtpController.js.map +1 -0
- package/lib/module/useOtpInput.js +216 -0
- package/lib/module/useOtpInput.js.map +1 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/src/Cell.d.ts +39 -0
- package/lib/typescript/commonjs/src/Cell.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/OtpInput.d.ts +12 -0
- package/lib/typescript/commonjs/src/OtpInput.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/accessibility.d.ts +39 -0
- package/lib/typescript/commonjs/src/accessibility.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/clipboard.d.ts +9 -0
- package/lib/typescript/commonjs/src/clipboard.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/dev.d.ts +13 -0
- package/lib/typescript/commonjs/src/dev.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/hiddenInput.d.ts +31 -0
- package/lib/typescript/commonjs/src/hiddenInput.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/index.d.ts +11 -0
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/legacy.d.ts +78 -0
- package/lib/typescript/commonjs/src/legacy.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/managedProps.d.ts +5 -0
- package/lib/typescript/commonjs/src/managedProps.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/types.d.ts +262 -0
- package/lib/typescript/commonjs/src/types.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/useOtpController.d.ts +28 -0
- package/lib/typescript/commonjs/src/useOtpController.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/useOtpInput.d.ts +22 -0
- package/lib/typescript/commonjs/src/useOtpInput.d.ts.map +1 -0
- package/lib/typescript/module/src/Cell.d.ts +39 -0
- package/lib/typescript/module/src/Cell.d.ts.map +1 -0
- package/lib/typescript/module/src/OtpInput.d.ts +12 -0
- package/lib/typescript/module/src/OtpInput.d.ts.map +1 -0
- package/lib/typescript/module/src/accessibility.d.ts +39 -0
- package/lib/typescript/module/src/accessibility.d.ts.map +1 -0
- package/lib/typescript/module/src/clipboard.d.ts +9 -0
- package/lib/typescript/module/src/clipboard.d.ts.map +1 -0
- package/lib/typescript/module/src/dev.d.ts +13 -0
- package/lib/typescript/module/src/dev.d.ts.map +1 -0
- package/lib/typescript/module/src/hiddenInput.d.ts +31 -0
- package/lib/typescript/module/src/hiddenInput.d.ts.map +1 -0
- package/lib/typescript/module/src/index.d.ts +11 -0
- package/lib/typescript/module/src/index.d.ts.map +1 -0
- package/lib/typescript/module/src/legacy.d.ts +78 -0
- package/lib/typescript/module/src/legacy.d.ts.map +1 -0
- package/lib/typescript/module/src/managedProps.d.ts +5 -0
- package/lib/typescript/module/src/managedProps.d.ts.map +1 -0
- package/lib/typescript/module/src/types.d.ts +262 -0
- package/lib/typescript/module/src/types.d.ts.map +1 -0
- package/lib/typescript/module/src/useOtpController.d.ts +28 -0
- package/lib/typescript/module/src/useOtpController.d.ts.map +1 -0
- package/lib/typescript/module/src/useOtpInput.d.ts +22 -0
- package/lib/typescript/module/src/useOtpInput.d.ts.map +1 -0
- package/package.json +102 -4
- package/src/Cell.tsx +146 -0
- package/src/OtpInput.tsx +120 -0
- package/src/accessibility.ts +86 -0
- package/src/clipboard.ts +232 -0
- package/src/dev.ts +33 -0
- package/src/hiddenInput.ts +80 -0
- package/src/index.ts +38 -0
- package/src/legacy.tsx +317 -0
- package/src/managedProps.ts +29 -0
- package/src/types.ts +299 -0
- package/src/useOtpController.ts +58 -0
- package/src/useOtpInput.ts +313 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { ResolvedOtpOptions } from '@r5n/otp-core';
|
|
2
|
+
import { Platform, type TextInputProps, type TextStyle } from 'react-native';
|
|
3
|
+
import type { OtpInputAutoComplete } from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Functional style of the hidden `TextInput` (orchestrator decision P1, `rn-platform.md` Q2).
|
|
7
|
+
* - Literal absolute positioning with zero insets: `StyleSheet.absoluteFillObject` no longer
|
|
8
|
+
* exists in React Native 0.88.
|
|
9
|
+
* - `opacity: 0.02`: iOS ignores touches on views below 0.01 (no tap focus, no long-press Paste),
|
|
10
|
+
* and Android treats alpha 0 as invisible (no autofill session, no TalkBack).
|
|
11
|
+
* - Transparent colours hide the text at that opacity; `fontSize: 1` makes taps land after the
|
|
12
|
+
* text, so the native caret stays at the end.
|
|
13
|
+
* - No `lineHeight`, `letterSpacing` or `fontFamily`: fewer attribute writes on the focused iOS
|
|
14
|
+
* field, which can drop fast keystrokes (`rn-platform.md` Q5, RN #58904).
|
|
15
|
+
*/
|
|
16
|
+
export const HIDDEN_INPUT_STYLE: TextStyle = Object.freeze({
|
|
17
|
+
position: 'absolute',
|
|
18
|
+
top: 0,
|
|
19
|
+
right: 0,
|
|
20
|
+
bottom: 0,
|
|
21
|
+
left: 0,
|
|
22
|
+
opacity: 0.02,
|
|
23
|
+
color: 'transparent',
|
|
24
|
+
backgroundColor: 'transparent',
|
|
25
|
+
fontSize: 1,
|
|
26
|
+
includeFontPadding: false,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
interface HiddenInputOptions {
|
|
30
|
+
options: ResolvedOtpOptions;
|
|
31
|
+
autoComplete: OtpInputAutoComplete;
|
|
32
|
+
keyboardType: TextInputProps['keyboardType'];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Platform props of the hidden input (P2, `rn-platform.md` Q1). Deliberately absent:
|
|
37
|
+
* `maxLength` (native truncation cuts pastes and autofill before JavaScript sees them, G1),
|
|
38
|
+
* `inputMode` (it would override `keyboardType`), `caretHidden` and `contextMenuHidden` (they
|
|
39
|
+
* remove the long-press Paste/AutoFill menu), `secureTextEntry` (masking is visual; on iOS it can
|
|
40
|
+
* bring up the Strong Password UI) and a controlled `selection` (it races fast typing, P3).
|
|
41
|
+
* Predictive text and the keyboard's learning are off through `autoCorrect`/`spellCheck`.
|
|
42
|
+
*/
|
|
43
|
+
export const hiddenInputProps = ({
|
|
44
|
+
options,
|
|
45
|
+
autoComplete,
|
|
46
|
+
keyboardType,
|
|
47
|
+
}: HiddenInputOptions): TextInputProps => {
|
|
48
|
+
const autofill = autoComplete !== 'off';
|
|
49
|
+
const shared: TextInputProps = {
|
|
50
|
+
// React Native maps 'one-time-code' to Android `sms-otp` and iOS `oneTimeCode`.
|
|
51
|
+
autoComplete: autofill ? 'one-time-code' : 'off',
|
|
52
|
+
keyboardType: keyboardType ?? (options.charset === 'numeric' ? 'number-pad' : 'default'),
|
|
53
|
+
autoCorrect: false,
|
|
54
|
+
spellCheck: false,
|
|
55
|
+
// With `casing: 'upper'` the keyboard already sends upper case, so core's transform is a
|
|
56
|
+
// no-op: transforming text in `onChangeText` duplicates characters on Android (RN #23578).
|
|
57
|
+
autoCapitalize: options.casing === 'upper' ? 'characters' : 'none',
|
|
58
|
+
allowFontScaling: false,
|
|
59
|
+
enterKeyHint: 'done',
|
|
60
|
+
// Hides the caret and selection highlight without `caretHidden` (iOS uses it for the caret).
|
|
61
|
+
selectionColor: 'transparent',
|
|
62
|
+
};
|
|
63
|
+
if (Platform.OS === 'ios') {
|
|
64
|
+
return {
|
|
65
|
+
...shared,
|
|
66
|
+
textContentType: autofill ? 'oneTimeCode' : 'none',
|
|
67
|
+
smartInsertDelete: false,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
if (Platform.OS === 'android') {
|
|
71
|
+
return {
|
|
72
|
+
...shared,
|
|
73
|
+
cursorColor: 'transparent',
|
|
74
|
+
// The landscape full-screen editor would show the raw hidden text instead of the cells.
|
|
75
|
+
disableFullscreenUI: true,
|
|
76
|
+
...(autofill ? {} : { importantForAutofill: 'no' as const }),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return shared;
|
|
80
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@r5n/otp-react-native`: one-time code input for React Native, built on `@r5n/otp-core`.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type {
|
|
8
|
+
OtpCasing,
|
|
9
|
+
OtpCharset,
|
|
10
|
+
OtpController,
|
|
11
|
+
OtpSelection,
|
|
12
|
+
OtpSlot,
|
|
13
|
+
OtpSnapshot,
|
|
14
|
+
} from '@r5n/otp-core';
|
|
15
|
+
export {
|
|
16
|
+
OtpInputs as default,
|
|
17
|
+
type OtpInputsProps,
|
|
18
|
+
type OtpInputsRef,
|
|
19
|
+
type OtpLegacyClipboard,
|
|
20
|
+
} from './legacy';
|
|
21
|
+
export { OtpInput } from './OtpInput';
|
|
22
|
+
export type {
|
|
23
|
+
OtpCellProps,
|
|
24
|
+
OtpClipboardProvider,
|
|
25
|
+
OtpDecorationProps,
|
|
26
|
+
OtpHiddenInputProps,
|
|
27
|
+
OtpInputAccessibilityTexts,
|
|
28
|
+
OtpInputAutoComplete,
|
|
29
|
+
OtpInputCellState,
|
|
30
|
+
OtpInputDirection,
|
|
31
|
+
OtpInputProps,
|
|
32
|
+
OtpInputRef,
|
|
33
|
+
OtpTextInputProps,
|
|
34
|
+
OtpTextInputRef,
|
|
35
|
+
UseOtpInputOptions,
|
|
36
|
+
UseOtpInputResult,
|
|
37
|
+
} from './types';
|
|
38
|
+
export { useOtpInput } from './useOtpInput';
|
package/src/legacy.tsx
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { resolveOptions } from '@r5n/otp-core';
|
|
2
|
+
import {
|
|
3
|
+
forwardRef,
|
|
4
|
+
type ReactElement,
|
|
5
|
+
useEffect,
|
|
6
|
+
useImperativeHandle,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useRef,
|
|
9
|
+
} from 'react';
|
|
10
|
+
import {
|
|
11
|
+
I18nManager,
|
|
12
|
+
Keyboard,
|
|
13
|
+
type KeyboardTypeOptions,
|
|
14
|
+
Platform,
|
|
15
|
+
type StyleProp,
|
|
16
|
+
StyleSheet,
|
|
17
|
+
type TextInputProps,
|
|
18
|
+
type TextStyle,
|
|
19
|
+
type ViewStyle,
|
|
20
|
+
} from 'react-native';
|
|
21
|
+
import { CellAppearance } from './Cell';
|
|
22
|
+
import { warnOnce } from './dev';
|
|
23
|
+
import { OtpInput } from './OtpInput';
|
|
24
|
+
import type { OtpClipboardProvider, OtpInputRef } from './types';
|
|
25
|
+
|
|
26
|
+
/** Clipboard module for the v7 wrapper, for example `@react-native-clipboard/clipboard`. */
|
|
27
|
+
export type OtpLegacyClipboard = Pick<OtpClipboardProvider, 'getString' | 'hasString'>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Props of the deprecated v7-compatible default export.
|
|
31
|
+
* @deprecated Use `OtpInputProps` with the named `OtpInput` export.
|
|
32
|
+
*/
|
|
33
|
+
export interface OtpInputsProps
|
|
34
|
+
extends Omit<TextInputProps, 'style' | 'keyboardType' | 'defaultValue'> {
|
|
35
|
+
/** Maps to `length`. Default `4`. */
|
|
36
|
+
numberOfInputs?: number | undefined;
|
|
37
|
+
/** Maps to `onChange`. */
|
|
38
|
+
handleChange?: ((otpCode: string) => void) | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Fills the code, as in v7: shown at mount (and reported through `handleChange`), and every
|
|
41
|
+
* later non-empty change is applied as if entered (`handleChange`, then `onComplete` behaviour).
|
|
42
|
+
*/
|
|
43
|
+
defaultValue?: string | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Reads the clipboard through `clipboard`, which is required since v8 (no native dependency).
|
|
46
|
+
* Default: on when `clipboard` is given.
|
|
47
|
+
*/
|
|
48
|
+
autofillFromClipboard?: boolean | undefined;
|
|
49
|
+
/** Also read the clipboard at this interval while focused and incomplete. */
|
|
50
|
+
autofillListenerIntervalMS?: number | undefined;
|
|
51
|
+
/** New in v8: the clipboard module to read from (only read, never written). */
|
|
52
|
+
clipboard?: OtpLegacyClipboard | undefined;
|
|
53
|
+
/** Default `'phone-pad'`, as in v7. */
|
|
54
|
+
keyboardType?: KeyboardTypeOptions | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Hides the code. The cells always show `•` (`mask`).
|
|
57
|
+
*
|
|
58
|
+
* - Android: the hidden input is also a real secure field (`secureTextEntry`), as in v7.4.0, so
|
|
59
|
+
* TalkBack and other accessibility services do not get the code. Android treats only text and
|
|
60
|
+
* number fields as passwords, so `keyboardType` `'phone-pad'` (the default) becomes
|
|
61
|
+
* `'number-pad'`.
|
|
62
|
+
* - iOS: mask only. A secure iOS field clears itself when typing resumes after a refocus (with
|
|
63
|
+
* one input that would erase the whole code), and it can offer the Strong Password UI.
|
|
64
|
+
*
|
|
65
|
+
* The named `OtpInput` export does not do this; it takes `secureTextEntry` as an opt-in through
|
|
66
|
+
* `textInputProps`.
|
|
67
|
+
*/
|
|
68
|
+
secureTextEntry?: boolean | undefined;
|
|
69
|
+
/** Row of cells. */
|
|
70
|
+
style?: StyleProp<ViewStyle>;
|
|
71
|
+
/** Maps to `focusedCellStyle`. */
|
|
72
|
+
focusStyles?: StyleProp<ViewStyle>;
|
|
73
|
+
/** Maps to `cellStyle`. */
|
|
74
|
+
inputContainerStyles?: StyleProp<ViewStyle>;
|
|
75
|
+
/** Maps to `cellTextStyle`. */
|
|
76
|
+
inputStyles?: StyleProp<TextStyle>;
|
|
77
|
+
/**
|
|
78
|
+
* Reverses the cells, as in v7: the first character is in the right-most cell when `isRTL`
|
|
79
|
+
* differs from `I18nManager.isRTL` (v7 reversed the cells inside a row that React Native mirrors
|
|
80
|
+
* in right-to-left apps). So in a right-to-left app `isRTL` puts the first character on the left.
|
|
81
|
+
* Maps to `direction: 'rtl'` or `'ltr'`.
|
|
82
|
+
*/
|
|
83
|
+
isRTL?: boolean | undefined;
|
|
84
|
+
/** Test ID of the hidden input (default `'otpInput'`); cells get `${testIDPrefix}-cell-<index>`. */
|
|
85
|
+
testIDPrefix?: string | undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Ref of the deprecated default export.
|
|
90
|
+
* @deprecated Use `OtpInputRef`.
|
|
91
|
+
*/
|
|
92
|
+
export interface OtpInputsRef {
|
|
93
|
+
/** Empties the value and calls `handleChange('')`, also when already empty. Never touches the clipboard. */
|
|
94
|
+
reset(): void;
|
|
95
|
+
focus(): void;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** v7 props that cannot work with a single hidden input (or would break it). */
|
|
99
|
+
const IGNORED = [
|
|
100
|
+
'selectTextOnFocus',
|
|
101
|
+
'clearTextOnFocus',
|
|
102
|
+
'caretHidden',
|
|
103
|
+
'contextMenuHidden',
|
|
104
|
+
'maxLength',
|
|
105
|
+
'value',
|
|
106
|
+
'selection',
|
|
107
|
+
'pointerEvents',
|
|
108
|
+
'inputMode',
|
|
109
|
+
] as const;
|
|
110
|
+
|
|
111
|
+
const styles = StyleSheet.create({
|
|
112
|
+
// v7's default container style.
|
|
113
|
+
container: {
|
|
114
|
+
alignItems: 'center',
|
|
115
|
+
flex: 1,
|
|
116
|
+
flexDirection: 'row',
|
|
117
|
+
justifyContent: 'space-between',
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const dismissKeyboard = (): void => Keyboard.dismiss();
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* v8 accepts an integer from 1 to 32 for `numberOfInputs` (core's `length`), and core throws a
|
|
125
|
+
* `RangeError` that names `length`. Rethrow it naming the v7 prop the app actually set (P:S4-7).
|
|
126
|
+
*/
|
|
127
|
+
const checkNumberOfInputs = (numberOfInputs: number): void => {
|
|
128
|
+
try {
|
|
129
|
+
resolveOptions({ length: numberOfInputs });
|
|
130
|
+
} catch (error) {
|
|
131
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
132
|
+
throw new RangeError(
|
|
133
|
+
`@r5n/otp-react-native: invalid \`numberOfInputs\` (${String(numberOfInputs)}), which v8 ` +
|
|
134
|
+
`passes to OtpInput as \`length\`: ${reason}`,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const toProvider = (
|
|
140
|
+
clipboard: OtpLegacyClipboard,
|
|
141
|
+
intervalMs: number | undefined,
|
|
142
|
+
): OtpClipboardProvider => ({
|
|
143
|
+
// Bound, so class-based clipboard modules keep their `this`.
|
|
144
|
+
getString: clipboard.getString.bind(clipboard),
|
|
145
|
+
hasString: clipboard.hasString?.bind(clipboard),
|
|
146
|
+
intervalMs,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* v7-compatible wrapper around {@link OtpInput}, kept for one major version: accepts any
|
|
151
|
+
* character (`charset: 'any'`) and dismisses the keyboard on completion, as v7 did.
|
|
152
|
+
*
|
|
153
|
+
* @deprecated Use the named export: `import { OtpInput } from '@r5n/otp-react-native'`.
|
|
154
|
+
*/
|
|
155
|
+
export const OtpInputs = forwardRef<OtpInputsRef, OtpInputsProps>(function OtpInputs(
|
|
156
|
+
{
|
|
157
|
+
numberOfInputs = 4,
|
|
158
|
+
handleChange,
|
|
159
|
+
defaultValue,
|
|
160
|
+
autofillFromClipboard,
|
|
161
|
+
autofillListenerIntervalMS,
|
|
162
|
+
clipboard,
|
|
163
|
+
keyboardType = 'phone-pad',
|
|
164
|
+
style,
|
|
165
|
+
focusStyles,
|
|
166
|
+
inputContainerStyles,
|
|
167
|
+
inputStyles,
|
|
168
|
+
isRTL = false,
|
|
169
|
+
testIDPrefix = 'otpInput',
|
|
170
|
+
autoFocus,
|
|
171
|
+
placeholder,
|
|
172
|
+
secureTextEntry,
|
|
173
|
+
editable,
|
|
174
|
+
readOnly,
|
|
175
|
+
onFocus,
|
|
176
|
+
onBlur,
|
|
177
|
+
accessibilityLabel,
|
|
178
|
+
accessibilityHint,
|
|
179
|
+
autoComplete,
|
|
180
|
+
testID,
|
|
181
|
+
placeholderTextColor,
|
|
182
|
+
maxFontSizeMultiplier,
|
|
183
|
+
allowFontScaling,
|
|
184
|
+
selectTextOnFocus,
|
|
185
|
+
clearTextOnFocus,
|
|
186
|
+
caretHidden,
|
|
187
|
+
contextMenuHidden,
|
|
188
|
+
maxLength,
|
|
189
|
+
value,
|
|
190
|
+
selection,
|
|
191
|
+
pointerEvents,
|
|
192
|
+
inputMode,
|
|
193
|
+
...textInputProps
|
|
194
|
+
},
|
|
195
|
+
ref,
|
|
196
|
+
): ReactElement {
|
|
197
|
+
checkNumberOfInputs(numberOfInputs);
|
|
198
|
+
const inner = useRef<OtpInputRef>(null);
|
|
199
|
+
const onChange = useRef(handleChange);
|
|
200
|
+
useLayoutEffect(() => {
|
|
201
|
+
onChange.current = handleChange;
|
|
202
|
+
});
|
|
203
|
+
useImperativeHandle(
|
|
204
|
+
ref,
|
|
205
|
+
() => ({
|
|
206
|
+
reset: () => {
|
|
207
|
+
const input = inner.current;
|
|
208
|
+
// v7 always reported the reset; `clear()` is silent when nothing changes (C:S4-3).
|
|
209
|
+
if (input?.getValue() === '') onChange.current?.('');
|
|
210
|
+
else input?.clear();
|
|
211
|
+
},
|
|
212
|
+
focus: () => inner.current?.focus(),
|
|
213
|
+
}),
|
|
214
|
+
[],
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
// v7 re-applied every new non-empty `defaultValue` and reported it, which is how v7 apps filled a
|
|
218
|
+
// code from an SMS listener (C:S2-1). `null` until mount.
|
|
219
|
+
const appliedDefault = useRef<string | null>(null);
|
|
220
|
+
useEffect(() => {
|
|
221
|
+
const previous = appliedDefault.current;
|
|
222
|
+
appliedDefault.current = defaultValue ?? '';
|
|
223
|
+
if (!defaultValue || defaultValue === previous) return;
|
|
224
|
+
// At mount the cells already show it (initial value); v7 reported it through `handleChange`.
|
|
225
|
+
if (previous === null) onChange.current?.(inner.current?.getValue() ?? '');
|
|
226
|
+
else inner.current?.setValue(defaultValue);
|
|
227
|
+
}, [defaultValue]);
|
|
228
|
+
|
|
229
|
+
const passed = {
|
|
230
|
+
selectTextOnFocus,
|
|
231
|
+
clearTextOnFocus,
|
|
232
|
+
caretHidden,
|
|
233
|
+
contextMenuHidden,
|
|
234
|
+
maxLength,
|
|
235
|
+
value,
|
|
236
|
+
selection,
|
|
237
|
+
pointerEvents,
|
|
238
|
+
inputMode,
|
|
239
|
+
};
|
|
240
|
+
// v7 cells had no look of their own: under v7 style props the built-in one would add borders,
|
|
241
|
+
// padding and sizes the app never asked for (C:S3-7).
|
|
242
|
+
const v7Styled =
|
|
243
|
+
inputContainerStyles !== undefined || inputStyles !== undefined || focusStyles !== undefined;
|
|
244
|
+
const ignored = IGNORED.filter((name) => passed[name] !== undefined).join(', ');
|
|
245
|
+
// A real secure field on Android only; iOS is mask only (see `secureTextEntry`). Android's
|
|
246
|
+
// `TextView.isPasswordInputType` accepts only the text and number classes, and a phone-class
|
|
247
|
+
// field with the password variation stays readable (device-android finding 3).
|
|
248
|
+
const secureAndroid = secureTextEntry === true && Platform.OS === 'android';
|
|
249
|
+
const wantsClipboard = autofillFromClipboard === true || autofillListenerIntervalMS !== undefined;
|
|
250
|
+
const missingClipboard = wantsClipboard && clipboard === undefined;
|
|
251
|
+
|
|
252
|
+
useEffect(() => {
|
|
253
|
+
if (!__DEV__) return;
|
|
254
|
+
warnOnce(
|
|
255
|
+
'deprecated',
|
|
256
|
+
'the default export (the v7 API) is deprecated and will be removed in the next major version. ' +
|
|
257
|
+
"Use `import { OtpInput } from '@r5n/otp-react-native'`; see the migration guide. " +
|
|
258
|
+
'Clipboard autofill is now off unless you pass a `clipboard` provider.',
|
|
259
|
+
);
|
|
260
|
+
}, []);
|
|
261
|
+
useEffect(() => {
|
|
262
|
+
if (!__DEV__ || ignored === '') return;
|
|
263
|
+
warnOnce(`ignored:${ignored}`, `ignored v7 props (no effect in v8): ${ignored}.`);
|
|
264
|
+
}, [ignored]);
|
|
265
|
+
useEffect(() => {
|
|
266
|
+
if (!__DEV__ || !missingClipboard) return;
|
|
267
|
+
warnOnce(
|
|
268
|
+
'clipboard',
|
|
269
|
+
'autofillFromClipboard needs a clipboard provider since v8, which has no native dependencies. ' +
|
|
270
|
+
'Pass `clipboard={Clipboard}` (from @react-native-clipboard/clipboard) or ' +
|
|
271
|
+
'`clipboard={{ getString: Clipboard.getStringAsync, hasString: Clipboard.hasStringAsync }}` (expo-clipboard).',
|
|
272
|
+
);
|
|
273
|
+
}, [missingClipboard]);
|
|
274
|
+
|
|
275
|
+
return (
|
|
276
|
+
<CellAppearance.Provider value={v7Styled ? 'none' : 'default'}>
|
|
277
|
+
<OtpInput
|
|
278
|
+
ref={inner}
|
|
279
|
+
length={numberOfInputs}
|
|
280
|
+
charset="any"
|
|
281
|
+
defaultValue={defaultValue}
|
|
282
|
+
onChange={handleChange}
|
|
283
|
+
onComplete={dismissKeyboard}
|
|
284
|
+
autoFocus={autoFocus}
|
|
285
|
+
disabled={editable === false || readOnly === true}
|
|
286
|
+
mask={secureTextEntry === true}
|
|
287
|
+
placeholder={placeholder}
|
|
288
|
+
keyboardType={secureAndroid && keyboardType === 'phone-pad' ? 'number-pad' : keyboardType}
|
|
289
|
+
autoComplete={autoComplete === 'off' ? 'off' : 'auto'}
|
|
290
|
+
direction={(isRTL === true) !== I18nManager.isRTL ? 'rtl' : 'ltr'}
|
|
291
|
+
clipboardAutofill={
|
|
292
|
+
autofillFromClipboard === false || clipboard === undefined
|
|
293
|
+
? false
|
|
294
|
+
: toProvider(clipboard, autofillListenerIntervalMS)
|
|
295
|
+
}
|
|
296
|
+
onFocus={onFocus}
|
|
297
|
+
onBlur={onBlur}
|
|
298
|
+
accessibilityLabel={accessibilityLabel}
|
|
299
|
+
accessibilityHint={accessibilityHint}
|
|
300
|
+
textInputProps={
|
|
301
|
+
secureAndroid ? { ...textInputProps, secureTextEntry: true } : textInputProps
|
|
302
|
+
}
|
|
303
|
+
testID={testID ?? testIDPrefix}
|
|
304
|
+
style={style ?? styles.container}
|
|
305
|
+
cellStyle={inputContainerStyles}
|
|
306
|
+
focusedCellStyle={focusStyles}
|
|
307
|
+
cellTextStyle={inputStyles}
|
|
308
|
+
placeholderTextColor={placeholderTextColor}
|
|
309
|
+
// The types of React Native 0.86 also allow `null` ("inherit") here, which `OtpInput` does
|
|
310
|
+
// not declare: the value is forwarded unchanged, as the strict types of later releases say.
|
|
311
|
+
maxFontSizeMultiplier={
|
|
312
|
+
allowFontScaling === false ? 1 : (maxFontSizeMultiplier as number | undefined)
|
|
313
|
+
}
|
|
314
|
+
/>
|
|
315
|
+
</CellAppearance.Provider>
|
|
316
|
+
);
|
|
317
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { OtpCellManagedProp, OtpTextInputManagedProp } from './types';
|
|
2
|
+
|
|
3
|
+
/** Runtime list of `OtpTextInputManagedProp` (kept equal by the public API type test). */
|
|
4
|
+
export const TEXT_INPUT_MANAGED_PROPS = [
|
|
5
|
+
'value',
|
|
6
|
+
'defaultValue',
|
|
7
|
+
'maxLength',
|
|
8
|
+
'selection',
|
|
9
|
+
'editable',
|
|
10
|
+
'readOnly',
|
|
11
|
+
'caretHidden',
|
|
12
|
+
'contextMenuHidden',
|
|
13
|
+
'pointerEvents',
|
|
14
|
+
'selectTextOnFocus',
|
|
15
|
+
'clearTextOnFocus',
|
|
16
|
+
'inputMode',
|
|
17
|
+
] as const satisfies readonly OtpTextInputManagedProp[];
|
|
18
|
+
|
|
19
|
+
/** Runtime list of `OtpCellManagedProp` (kept equal by the public API type test). */
|
|
20
|
+
export const CELL_MANAGED_PROPS = [
|
|
21
|
+
'children',
|
|
22
|
+
'style',
|
|
23
|
+
'testID',
|
|
24
|
+
'pointerEvents',
|
|
25
|
+
'aria-hidden',
|
|
26
|
+
'accessible',
|
|
27
|
+
'accessibilityElementsHidden',
|
|
28
|
+
'importantForAccessibility',
|
|
29
|
+
] as const satisfies readonly OtpCellManagedProp[];
|