Skip to content

Commit 8f51849

Browse files
committed
fix(TextInputProps): forwarding unknown props
1 parent 9e2796b commit 8f51849

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

__tests__/AutocompleteInput.spec.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,30 @@ describe('<AutocompleteInput />', () => {
9494
render(<TestForwardRefComponent />);
9595
expect(ref!.current?.constructor.name).toBe('TextInput');
9696
});
97+
98+
it('should only pass text input props to the TextInput', () => {
99+
const MockTextInput = jest.fn(() => null) as unknown as jest.Mock<ReactElement>;
100+
101+
render(
102+
<Autocomplete
103+
data={[]}
104+
placeholder="Enter search"
105+
renderResultList={() => <></>}
106+
renderTextInput={MockTextInput}
107+
/>,
108+
);
109+
110+
expect(MockTextInput).toHaveBeenCalledWith({
111+
placeholder: 'Enter search',
112+
ref: null,
113+
style: [
114+
{
115+
backgroundColor: 'white',
116+
height: 40,
117+
paddingLeft: 3,
118+
},
119+
undefined,
120+
],
121+
});
122+
});
97123
});

index.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,23 @@ export const AutocompleteInput = React.forwardRef(function AutocompleteInputComp
3939
ref: React.ForwardedRef<Ref>,
4040
): React.ReactElement {
4141
const defaultRenderItems: ListRenderItem<Item> = ({ item }) => <Text>{String(item)}</Text>;
42-
function renderResultList(): React.ReactElement {
43-
const { data, renderResultList: renderFunction = DefaultResultList, flatListProps } = props;
4442

43+
const {
44+
data,
45+
containerStyle,
46+
hideResults,
47+
inputContainerStyle,
48+
listContainerStyle,
49+
onShowResults,
50+
onStartShouldSetResponderCapture = () => false,
51+
renderResultList: renderResultListFunction = DefaultResultList,
52+
renderTextInput: renderTextInputFunction = DefaultTextInput,
53+
flatListProps,
54+
style,
55+
...textInputProps
56+
} = props;
57+
58+
function renderResultList(): React.ReactElement {
4559
const listProps: FlatListProps<Item> = {
4660
data,
4761
renderItem: defaultRenderItems,
@@ -50,30 +64,19 @@ export const AutocompleteInput = React.forwardRef(function AutocompleteInputComp
5064
style: [styles.list, flatListProps?.style],
5165
};
5266

53-
return renderFunction(listProps);
67+
return renderResultListFunction(listProps);
5468
}
5569

5670
function renderTextInput() {
57-
const { renderTextInput: renderFunction = DefaultTextInput, style } = props;
58-
const textProps = {
59-
...props,
71+
const renderProps = {
72+
...textInputProps,
6073
style: [styles.input, style],
6174
ref,
6275
};
6376

64-
return renderFunction(textProps);
77+
return renderTextInputFunction(renderProps);
6578
}
6679

67-
const {
68-
data,
69-
containerStyle,
70-
hideResults,
71-
inputContainerStyle,
72-
listContainerStyle,
73-
onShowResults,
74-
onStartShouldSetResponderCapture = () => false,
75-
} = props;
76-
7780
const showResults = data.length > 0;
7881
onShowResults?.(showResults);
7982

0 commit comments

Comments
 (0)