Skip to content

Commit daa49af

Browse files
authored
fix: custom text input loosing focus (#349)
1 parent 3237e00 commit daa49af

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

packages/react-native-autocomplete-input/__tests__/AutocompleteInput.spec.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { type ReactElement } from 'react';
2-
import { render, screen, within } from '@testing-library/react-native';
2+
import { render, screen, userEvent, within } from '@testing-library/react-native';
33
import { FlatList, TextInput, type FlatListProps } from 'react-native';
44

55
import Autocomplete from '../index';
@@ -104,7 +104,7 @@ describe('<AutocompleteInput />', () => {
104104
data={[]}
105105
placeholder="Enter search"
106106
renderResultList={() => <></>}
107-
renderTextInput={MockTextInput}
107+
renderTextInput={(props) => <MockTextInput {...props} />}
108108
/>,
109109
);
110110

@@ -123,4 +123,27 @@ describe('<AutocompleteInput />', () => {
123123
{},
124124
);
125125
});
126+
127+
it('should preserve custom text input on re-render', async () => {
128+
function TestWrapper() {
129+
const [text, setText] = React.useState('');
130+
return (
131+
<Autocomplete
132+
data={ITEMS}
133+
placeholder="Enter search"
134+
renderTextInput={(props) => <TextInput {...props} value={text} onChangeText={setText} />}
135+
value={text}
136+
/>
137+
);
138+
}
139+
140+
render(<TestWrapper />);
141+
142+
const input = screen.getByPlaceholderText('Enter search');
143+
await userEvent.type(input, 'A');
144+
expect(input).toHaveDisplayValue('A');
145+
146+
await userEvent.type(input, ' New Hope');
147+
expect(input).toHaveDisplayValue('A New Hope');
148+
});
126149
});

packages/react-native-autocomplete-input/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const AutocompleteInput = React.forwardRef(function AutocompleteInputComp
4141
onShowResults,
4242
onStartShouldSetResponderCapture = () => false,
4343
renderResultList: ResultList = FlatList,
44-
renderTextInput: AutoCompleteTextInput = TextInput,
44+
renderTextInput: customRenderTextInput = (props) => <TextInput {...props} />,
4545
flatListProps,
4646
style,
4747
...textInputProps
@@ -59,14 +59,14 @@ export const AutocompleteInput = React.forwardRef(function AutocompleteInputComp
5959
return <ResultList {...listProps} />;
6060
}
6161

62-
function renderTextInput(): React.ReactElement {
62+
function renderTextInput(): React.ReactNode {
6363
const renderProps = {
6464
...textInputProps,
6565
style: [styles.input, style],
66-
ref,
66+
...(ref && { ref }),
6767
};
6868

69-
return <AutoCompleteTextInput {...renderProps} />;
69+
return customRenderTextInput(renderProps);
7070
}
7171

7272
const showResults = data.length > 0;

0 commit comments

Comments
 (0)