diff --git a/__tests__/AutocompleteInput.spec.tsx b/__tests__/AutocompleteInput.spec.tsx index 546b8c4..f88fc34 100644 --- a/__tests__/AutocompleteInput.spec.tsx +++ b/__tests__/AutocompleteInput.spec.tsx @@ -96,6 +96,13 @@ describe('', () => { expect(ref!.current?.constructor.name).toBe('TextInput'); }); + it('should hide suggestion list when hideResults is true', () => { + render(); + + const suggestionList = screen.queryByTestId(suggestionListTestId); + expect(suggestionList).not.toBeOnTheScreen(); + }); + it('should only pass text input props to the TextInput', () => { const MockTextInput = jest.fn(() => <>) as jest.Mock; diff --git a/index.tsx b/index.tsx index 58001be..1333381 100644 --- a/index.tsx +++ b/index.tsx @@ -49,7 +49,21 @@ export const AutocompleteInput = React.forwardRef(function AutocompleteInputComp ...textInputProps } = props; - function ResultListWrapper(): React.ReactElement { + function TextInputWrapper(): React.ReactElement { + const autocompleteTextInputProps = { + ...textInputProps, + style: [styles.input, style], + ref, + }; + + return ( + + ; + + ); + } + + function ResultListWrapper(): React.ReactElement | null { const resultListProps: FlatListProps = { data, renderItem: defaultRenderItem, @@ -58,35 +72,28 @@ export const AutocompleteInput = React.forwardRef(function AutocompleteInputComp style: [styles.list, flatListProps?.style], }; - return ; - } + const showResults = data.length > 0; + onShowResults?.(showResults); - function TextInputWrapper(): React.ReactElement { - const autocompleteTextInputProps = { - ...textInputProps, - style: [styles.input, style], - ref, - }; + const doNotShowResults = hideResults || !showResults; + if (doNotShowResults) { + return null; + } - return ; + return ( + + ; + + ); } - const showResults = data.length > 0; - onShowResults?.(showResults); - return ( - - - - {!hideResults && ( - - {showResults && } - - )} + + ); });