Skip to content

Commit

Permalink
fix focus
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoa Phan committed Jul 2, 2021
1 parent b860ffc commit 7d16af0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ or
| showIcon | Boolean | No | |
| iconStyle | ImageStyle | No | |
| numeric | Boolean | No | |
| focusColor | String | No | |
| fontFamily | String | No | |
| renderLeftIcon | () => JSX.Element | No | |
| renderRightIcon | () => JSX.Element | No | |
Expand All @@ -48,6 +49,7 @@ or
onChangeText={text => {
console.log(text);
}}
focusColor="red"
textError="Please enter"
/>
<TextInput
Expand Down
1 change: 1 addition & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const TextInputScreen = _props => {
onChangeText={text => {
console.log(text);
}}
focusColor="red"
textError="Please enter"
/>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-element-textinput",
"title": "React Native Element TextInput",
"version": "0.1.2",
"version": "0.1.3",
"description": "A react-native TextInput component easy to customize for both iOS and Android.",
"main": "index.ts",
"scripts": {
Expand Down
29 changes: 27 additions & 2 deletions src/TextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ const TextInputComponent: CTextInput = props => {
showIcon,
numeric,
textError,
onChangeText = (value: string) => {},
focusColor,
onFocus,
onBlur,
onChangeText = (value: string) => { },
renderLeftIcon,
renderRightIcon,
} = props;

const [text, setText] = useState<string>('');
const [isFocus, setIsFocus] = useState<boolean>(false);
const [textEntry, setTextEntry] = useState<boolean>(
secureTextEntry ? true : false,
);
Expand Down Expand Up @@ -113,11 +117,30 @@ const TextInputComponent: CTextInput = props => {
}
};

const onFocusCustom = (e) => {
setIsFocus(true);
if (onFocus) {
onFocus(e);
}
}

const ononBlurCustom = (e) => {
setIsFocus(false);
if (onBlur) {
onBlur(e);
}
}

return (
<View>
<View style={[style]}>
{label && <Text style={[styles.label, labelStyle]}>{label}</Text>}
<View style={[styles.textInput, containerStyle]}>
<View
style={[
styles.textInput,
containerStyle,
isFocus && focusColor && {borderBottomColor: focusColor, borderTopColor: focusColor, borderLeftColor: focusColor, borderRightColor: focusColor}]
}>
{renderLeftIcon?.()}
<TextInput
{...props}
Expand All @@ -127,6 +150,8 @@ const TextInputComponent: CTextInput = props => {
placeholder={placeholder}
placeholderTextColor={placeholderTextColor}
onChangeText={onChange}
onFocus={onFocusCustom}
onBlur={ononBlurCustom}
/>
{_renderRightIcon()}
</View>
Expand Down
5 changes: 5 additions & 0 deletions src/TextInput/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
ImageStyle,
NativeSyntheticEvent,
StyleProp,
TextInputFocusEventData,
TextInputProps,
TextStyle,
ViewStyle,
Expand All @@ -18,6 +20,9 @@ interface Props extends TextInputProps {
label?: string;
showIcon?: boolean;
numeric?: boolean;
focusColor?: string;
onFocus?: (e: NativeSyntheticEvent<TextInputFocusEventData>) => void;
onBlur?: (e: NativeSyntheticEvent<TextInputFocusEventData>) => void;
renderRightIcon?: () => JSX.Element | null | undefined;
renderLeftIcon?: () => JSX.Element | null | undefined;
}
Expand Down

0 comments on commit 7d16af0

Please sign in to comment.