Skip to content

Commit c8ac9f2

Browse files
authored
fix: make opacity 0.02 for iOS to show context menu for pasting (#95)
1 parent f0c385b commit c8ac9f2

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

src/OtpInput/OtpInput.styles.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StyleSheet } from "react-native";
1+
import { Platform, StyleSheet } from "react-native";
22

33
export const styles = StyleSheet.create({
44
container: {
@@ -20,7 +20,15 @@ export const styles = StyleSheet.create({
2020
},
2121
hiddenInput: {
2222
...StyleSheet.absoluteFillObject,
23-
opacity: 0,
23+
...Platform.select({
24+
ios: {
25+
opacity: 0.02,
26+
color: "transparent",
27+
},
28+
default: {
29+
opacity: 0,
30+
},
31+
}),
2432
},
2533
stick: {
2634
width: 2,

src/OtpInput/OtpInput.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
118118
testID="otp-input-hidden"
119119
onFocus={handleFocus}
120120
onBlur={handleBlur}
121+
caretHidden={Platform.OS === "ios"}
121122
{...textInputProps}
122123
style={[styles.hiddenInput, textInputProps?.style]}
123124
/>

src/OtpInput/__tests__/OtpInput.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,35 @@ describe("OtpInput", () => {
200200
expect(input.props.inputMode).toBe("text");
201201
}
202202
);
203+
204+
describe("caretHidden", () => {
205+
test("should be true on iOS", () => {
206+
Platform.OS = "ios";
207+
renderOtpInput();
208+
209+
const input = screen.getByTestId("otp-input-hidden");
210+
211+
expect(input.props.caretHidden).toBe(true);
212+
});
213+
214+
test("should be false on android", () => {
215+
Platform.OS = "android";
216+
renderOtpInput();
217+
218+
const input = screen.getByTestId("otp-input-hidden");
219+
220+
expect(input.props.caretHidden).toBe(false);
221+
});
222+
223+
test("should be false on web", () => {
224+
Platform.OS = "web";
225+
renderOtpInput();
226+
227+
const input = screen.getByTestId("otp-input-hidden");
228+
229+
expect(input.props.caretHidden).toBe(false);
230+
});
231+
});
203232
});
204233
describe("Logic", () => {
205234
test("should split text on screen from the text written in the hidden input", () => {

src/OtpInput/__tests__/__snapshots__/OtpInput.test.tsx.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ exports[`OtpInput UI should render correctly 1`] = `
382382
<TextInput
383383
autoComplete="one-time-code"
384384
autoFocus={true}
385+
caretHidden={true}
385386
editable={true}
386387
inputMode="numeric"
387388
maxLength={6}
@@ -393,8 +394,9 @@ exports[`OtpInput UI should render correctly 1`] = `
393394
[
394395
{
395396
"bottom": 0,
397+
"color": "transparent",
396398
"left": 0,
397-
"opacity": 0,
399+
"opacity": 0.02,
398400
"position": "absolute",
399401
"right": 0,
400402
"top": 0,

0 commit comments

Comments
 (0)