Skip to content

Commit 5b886f6

Browse files
committed
inputRef prop is here and Example is updated with inputRef usage with Clear TextInput example
1 parent 6e37811 commit 5b886f6

File tree

7 files changed

+168
-13
lines changed

7 files changed

+168
-13
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</td>
2424
</tr>
2525
<tr>
26-
<td align="center">
26+
<td align="center">
2727
<img alt="React Native Text Input" src="assets/Screenshots/react-native-text-input.png" width="686px" />
2828
</td>
2929
<td align="center">
@@ -106,6 +106,7 @@ should work of the example project.
106106
| textInputStyle | TextStyle | default | set or override the style object for the text input style |
107107
| iconImageStyle | ImageStyle | default | set or override the style object for the image icon style |
108108
| ImageComponent | Image | default | set your own component instead of default react-native Image component |
109+
| inputRef | reference | undefined | set the TextInput reference for the programmatic usage |
109110

110111
## Future Plans
111112

example/App.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
11
import React from "react";
2-
import { StatusBar, SafeAreaView } from "react-native";
3-
import RNTextInput from "@freakycoder/react-native-text-input";
2+
import {
3+
Text,
4+
StatusBar,
5+
TextInput,
6+
SafeAreaView,
7+
TouchableOpacity,
8+
} from "react-native";
9+
// import RNTextInput from "@freakycoder/react-native-text-input";
10+
import RNTextInput from "./lib/RNTextInput";
411

512
const App = () => {
13+
let _inputRef: TextInput | null = null;
14+
15+
const renderClearInputButton = () => (
16+
<TouchableOpacity
17+
style={{
18+
width: 300,
19+
height: 50,
20+
marginTop: 64,
21+
borderRadius: 12,
22+
backgroundColor: "#d7aefb",
23+
alignItems: "center",
24+
justifyContent: "center",
25+
}}
26+
onPress={() => {
27+
_inputRef?.clear();
28+
}}
29+
>
30+
<Text style={{ color: "#fff", fontWeight: "bold" }}>Clear TextInput</Text>
31+
</TouchableOpacity>
32+
);
33+
634
return (
735
<>
836
<StatusBar barStyle="dark-content" />
@@ -15,9 +43,12 @@ const App = () => {
1543
}}
1644
>
1745
<RNTextInput
46+
inputRef={(ref: any) => (_inputRef = ref)}
1847
placeholder="E-mail"
1948
onChangeText={(text: string) => console.log("Text: ", text)}
49+
onPress={() => _inputRef?.focus()}
2050
/>
51+
{renderClearInputButton()}
2152
</SafeAreaView>
2253
</>
2354
);

example/lib/RNTextInput.style.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { ViewStyle, ImageStyle, TextStyle, StyleSheet } from "react-native";
2+
3+
interface Style {
4+
container: ViewStyle;
5+
contentContainer: ViewStyle;
6+
textInputStyle: TextStyle;
7+
buttonStyle: ViewStyle;
8+
iconImageStyle: ImageStyle;
9+
}
10+
11+
export default StyleSheet.create<Style>({
12+
container: {
13+
height: 60,
14+
width: 300,
15+
paddingLeft: 16,
16+
paddingRight: 3,
17+
borderRadius: 16,
18+
justifyContent: "center",
19+
backgroundColor: "#fdfdfd",
20+
},
21+
contentContainer: {
22+
flexDirection: "row",
23+
justifyContent: "space-between",
24+
},
25+
textInputStyle: {
26+
width: "78%",
27+
fontSize: 16,
28+
fontWeight: "800",
29+
},
30+
buttonStyle: {
31+
width: 55,
32+
height: 55,
33+
borderRadius: 16,
34+
alignItems: "center",
35+
justifyContent: "center",
36+
backgroundColor: "#933fe3",
37+
},
38+
iconImageStyle: {
39+
width: 20,
40+
height: 20,
41+
tintColor: "#fdfdfd",
42+
},
43+
});

example/lib/RNTextInput.tsx

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import * as React from "react";
2+
import {
3+
View,
4+
Image,
5+
StyleProp,
6+
TextInput,
7+
ViewStyle,
8+
TextStyle,
9+
ImageStyle,
10+
ViewProps,
11+
TextInputProps,
12+
} from "react-native";
13+
import RNBounceable, {
14+
IRNBounceableProps,
15+
} from "@freakycoder/react-native-bounceable";
16+
/**
17+
* ? Local Imports
18+
*/
19+
import styles from "./RNTextInput.style";
20+
21+
const defaultArrowIcon = require("./local-assets/right-arrow.png");
22+
23+
type CustomStyleProp = StyleProp<ViewStyle> | Array<StyleProp<ViewStyle>>;
24+
25+
export interface ISource {
26+
source: string | { uri: string };
27+
}
28+
29+
interface IRNTextInputProps extends IRNBounceableProps, TextInputProps {
30+
inputRef?: any;
31+
ImageComponent?: any;
32+
placeholder?: string;
33+
buttonStyle?: ViewStyle;
34+
textInputStyle?: TextStyle;
35+
iconImageStyle?: ImageStyle;
36+
disableButton?: boolean;
37+
style?: CustomStyleProp;
38+
onPress?: () => void;
39+
}
40+
41+
const RNTextInput: React.FC<IRNTextInputProps> = ({
42+
style,
43+
inputRef,
44+
placeholder,
45+
buttonStyle,
46+
textInputStyle,
47+
iconImageStyle,
48+
disableButton = false,
49+
ImageComponent = Image,
50+
onPress,
51+
...props
52+
}) => {
53+
const renderContent = () => (
54+
<View style={styles.contentContainer}>
55+
<TextInput
56+
placeholderTextColor="#ead4ff"
57+
{...props}
58+
ref={inputRef}
59+
placeholder={placeholder}
60+
style={[styles.textInputStyle, textInputStyle]}
61+
/>
62+
{!disableButton && (
63+
<RNBounceable style={[styles.buttonStyle, buttonStyle]}>
64+
<ImageComponent
65+
source={defaultArrowIcon}
66+
style={[styles.iconImageStyle, iconImageStyle]}
67+
/>
68+
</RNBounceable>
69+
)}
70+
</View>
71+
);
72+
73+
return (
74+
<RNBounceable
75+
bounceEffect={0.97}
76+
{...props}
77+
style={[styles.container, style]}
78+
onPress={onPress}
79+
>
80+
{renderContent()}
81+
</RNBounceable>
82+
);
83+
};
84+
85+
export default RNTextInput;
785 Bytes
Loading

lib/RNTextInput.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface ISource {
2727
}
2828

2929
interface IRNTextInputProps extends IRNBounceableProps, TextInputProps {
30+
inputRef?: any;
3031
ImageComponent?: any;
3132
placeholder?: string;
3233
buttonStyle?: ViewStyle;
@@ -39,6 +40,7 @@ interface IRNTextInputProps extends IRNBounceableProps, TextInputProps {
3940

4041
const RNTextInput: React.FC<IRNTextInputProps> = ({
4142
style,
43+
inputRef,
4244
placeholder,
4345
buttonStyle,
4446
textInputStyle,
@@ -48,20 +50,13 @@ const RNTextInput: React.FC<IRNTextInputProps> = ({
4850
onPress,
4951
...props
5052
}) => {
51-
let inputRef: TextInput | null = null;
52-
53-
const handleOnPress = () => {
54-
inputRef?.focus();
55-
onPress && onPress();
56-
};
57-
5853
const renderContent = () => (
5954
<View style={styles.contentContainer}>
6055
<TextInput
6156
placeholderTextColor="#ead4ff"
6257
{...props}
58+
ref={inputRef}
6359
placeholder={placeholder}
64-
ref={(ref) => (inputRef = ref)}
6560
style={[styles.textInputStyle, textInputStyle]}
6661
/>
6762
{!disableButton && (
@@ -80,7 +75,7 @@ const RNTextInput: React.FC<IRNTextInputProps> = ({
8075
bounceEffect={0.97}
8176
{...props}
8277
style={[styles.container, style]}
83-
onPress={handleOnPress}
78+
onPress={onPress}
8479
>
8580
{renderContent()}
8681
</RNBounceable>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@freakycoder/react-native-text-input",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Modern text input with fully customization options for React Native",
55
"main": "./build/dist/RNTextInput.js",
66
"repository": "git@github.com:WrathChaos/react-native-text-input.git",

0 commit comments

Comments
 (0)