Skip to content

Commit 8462de9

Browse files
authored
Fix issue with RN TextInput clear doesn't trigger onChangeText (#1711)
1 parent 4885cb0 commit 8462de9

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import React from 'react';
2-
import { TextInput } from 'react-native';
3-
declare const useImperativeInputHandle: (ref: React.Ref<any>) => React.MutableRefObject<TextInput | undefined>;
2+
import { TextInput, TextInputProps } from 'react-native';
3+
declare const useImperativeInputHandle: (ref: React.Ref<any>, props: Pick<TextInputProps, 'onChangeText'>) => React.MutableRefObject<TextInput | undefined>;
44
export default useImperativeInputHandle;

src/incubator/TextField/Input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const Input = ({
4141
formatter,
4242
...props
4343
}: InputProps & ForwardRefInjectedProps) => {
44-
const inputRef = useImperativeInputHandle(forwardedRef);
44+
const inputRef = useImperativeInputHandle(forwardedRef, {onChangeText: props.onChangeText});
4545
const context = useContext(FieldContext);
4646
const placeholder = !context.isFocused ? props.placeholder : hint || props.placeholder;
4747
const inputColor = getColorByState(color, context);

src/incubator/TextField/useImperativeInputHandle.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import React, {useContext, useImperativeHandle, useRef} from 'react';
2-
import {TextInput} from 'react-native';
2+
import {TextInput, TextInputProps} from 'react-native';
33
import FieldContext from './FieldContext';
44

5-
const useImperativeInputHandle = (ref: React.Ref<any>) => {
5+
const useImperativeInputHandle = (ref: React.Ref<any>, props: Pick<TextInputProps, 'onChangeText'>) => {
66
const inputRef = useRef<TextInput>();
77
const context = useContext(FieldContext);
88
useImperativeHandle(ref, () => {
99
return {
1010
focus: () => inputRef.current?.focus(),
1111
blur: () => inputRef.current?.blur(),
12-
clear: () => inputRef.current?.clear(),
12+
clear: () => {
13+
inputRef.current?.clear();
14+
// NOTE: This fixes an RN issue - when triggering imperative clear method, it doesn't call onChangeText
15+
props.onChangeText?.('');
16+
},
1317
validate: () => {
1418
context.validateField();
1519
}

0 commit comments

Comments
 (0)