From 8f1802f333d18b89b6544b7ecfa46b86b5031d80 Mon Sep 17 00:00:00 2001 From: Damian Sznajder Date: Thu, 19 Dec 2019 15:54:45 +0000 Subject: [PATCH] Restore pasting from keyboard on iOS --- src/OtpInput.tsx | 5 ++--- src/__tests__/__snapshots__/OtpInput.test.js.snap | 1 - src/__tests__/__snapshots__/index.test.js.snap | 2 +- src/index.tsx | 10 ++++++++++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/OtpInput.tsx b/src/OtpInput.tsx index d1f48b7..0046927 100644 --- a/src/OtpInput.tsx +++ b/src/OtpInput.tsx @@ -29,13 +29,13 @@ const OtpInput = forwardRef( ( { focusStyles, + handleKeyPress, handleTextChange, inputContainerStyles, inputStyles, + inputValue, placeholder, selectTextOnFocus, - inputValue, - handleKeyPress, ...rest }, ref, @@ -50,7 +50,6 @@ const OtpInput = forwardRef( return ( setFocused(false)} onChangeText={handleTextChange} onFocus={() => setFocused(true)} diff --git a/src/__tests__/__snapshots__/OtpInput.test.js.snap b/src/__tests__/__snapshots__/OtpInput.test.js.snap index 55c9b54..fa20c04 100644 --- a/src/__tests__/__snapshots__/OtpInput.test.js.snap +++ b/src/__tests__/__snapshots__/OtpInput.test.js.snap @@ -11,7 +11,6 @@ exports[` render 1`] = ` > render 1`] = ` clearTextOnFocus={false} firstInput={true} keyboardType="phone-pad" - maxLength={1} + maxLength={4} numberOfInputs={4} onBlur={[Function]} onChangeText={[Function]} diff --git a/src/index.tsx b/src/index.tsx index 5e165ea..ac2739d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -191,6 +191,12 @@ const OtpInputs = forwardRef( handleClearInput(index); } + if (text.length > 1) { + handleClearInput(index); + Keyboard.dismiss(); + return fillInputs(text); + } + if (text) { handleInputTextChange({ text, index }); } @@ -286,6 +292,10 @@ const OtpInputs = forwardRef( handleKeyPress={(keyPressEvent: NativeSyntheticEvent) => handleKeyPress(keyPressEvent, inputIndex) } + maxLength={Platform.select({ + android: 1, + ios: index === 0 ? numberOfInputs : 1, + })} autoCapitalize={autoCapitalize} clearTextOnFocus={clearTextOnFocus} firstInput={index === 0}