Skip to content

Fix issue with RN TextInput clear doesn't trigger onChangeText #1711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import { TextInput } from 'react-native';
declare const useImperativeInputHandle: (ref: React.Ref<any>) => React.MutableRefObject<TextInput | undefined>;
import { TextInput, TextInputProps } from 'react-native';
declare const useImperativeInputHandle: (ref: React.Ref<any>, props: Pick<TextInputProps, 'onChangeText'>) => React.MutableRefObject<TextInput | undefined>;
export default useImperativeInputHandle;
2 changes: 1 addition & 1 deletion src/incubator/TextField/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Input = ({
formatter,
...props
}: InputProps & ForwardRefInjectedProps) => {
const inputRef = useImperativeInputHandle(forwardedRef);
const inputRef = useImperativeInputHandle(forwardedRef, {onChangeText: props.onChangeText});
const context = useContext(FieldContext);
const placeholder = !context.isFocused ? props.placeholder : hint || props.placeholder;
const inputColor = getColorByState(color, context);
Expand Down
10 changes: 7 additions & 3 deletions src/incubator/TextField/useImperativeInputHandle.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import React, {useContext, useImperativeHandle, useRef} from 'react';
import {TextInput} from 'react-native';
import {TextInput, TextInputProps} from 'react-native';
import FieldContext from './FieldContext';

const useImperativeInputHandle = (ref: React.Ref<any>) => {
const useImperativeInputHandle = (ref: React.Ref<any>, props: Pick<TextInputProps, 'onChangeText'>) => {
const inputRef = useRef<TextInput>();
const context = useContext(FieldContext);
useImperativeHandle(ref, () => {
return {
focus: () => inputRef.current?.focus(),
blur: () => inputRef.current?.blur(),
clear: () => inputRef.current?.clear(),
clear: () => {
inputRef.current?.clear();
// NOTE: This fixes an RN issue - when triggering imperative clear method, it doesn't call onChangeText
props.onChangeText?.('');
},
validate: () => {
context.validateField();
}
Expand Down