From c4ff5782b0a9e216d1f115bf4b25322361d72a22 Mon Sep 17 00:00:00 2001 From: Daksh Bhardwaj Date: Wed, 17 Aug 2022 12:28:28 +0530 Subject: [PATCH 1/4] feat: added loading icon in searchBar --- example/src/Examples/SearchbarExample.tsx | 9 ++++ src/components/Searchbar.tsx | 60 +++++++++++++++-------- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/example/src/Examples/SearchbarExample.tsx b/example/src/Examples/SearchbarExample.tsx index bd18bbe270..9d68d4ba16 100644 --- a/example/src/Examples/SearchbarExample.tsx +++ b/example/src/Examples/SearchbarExample.tsx @@ -12,6 +12,7 @@ const SearchExample = ({ navigation }: Props) => { const [firstQuery, setFirstQuery] = React.useState(''); const [secondQuery, setSecondQuery] = React.useState(''); const [thirdQuery, setThirdQuery] = React.useState(''); + const [fourthQuery, setFourthQuery] = React.useState(''); const { isV3 } = useTheme(); @@ -44,6 +45,14 @@ const SearchExample = ({ navigation }: Props) => { icon="menu" style={styles.searchbar} /> + + setFourthQuery(query)} + value={fourthQuery} + loading + style={styles.searchbar} + /> ); }; diff --git a/src/components/Searchbar.tsx b/src/components/Searchbar.tsx index 741b37cf4f..e1f1a95fc0 100644 --- a/src/components/Searchbar.tsx +++ b/src/components/Searchbar.tsx @@ -18,6 +18,9 @@ import { withTheme } from '../core/theming'; import type { IconSource } from './Icon'; import type { Theme } from '../types'; import MaterialCommunityIcon from './MaterialCommunityIcon'; +import ActivityIndicator, { + Props as ActivityIndicatorProps, +} from './ActivityIndicator'; export type Props = React.ComponentPropsWithRef & { /** @@ -70,6 +73,14 @@ export type Props = React.ComponentPropsWithRef & { * Custom icon for clear button, default will be icon close */ clearIcon?: IconSource; + /** + * Custom flag for replacing clear button with activity indicator. + */ + loading?: Boolean; + /** + * Custom styles for the activity indicator. + */ + loaderStyle?: Omit; }; type TextInputHandles = Pick< @@ -122,6 +133,8 @@ const Searchbar = React.forwardRef( style, theme, value, + loading = false, + loaderStyle, ...rest }: Props, ref @@ -218,26 +231,30 @@ const Searchbar = React.forwardRef( value={value} {...rest} /> - ( - - )) - } - accessibilityRole="button" - /> + {loading ? ( + + ) : ( + ( + + )) + } + accessibilityRole="button" + /> + )} ); } @@ -259,6 +276,9 @@ const styles = StyleSheet.create({ elevation: { elevation: 4, }, + loader: { + margin: 10, + }, }); export default withTheme(Searchbar); From b280e7c2ea59ab8602b3dd8e780c2995dbe95ac2 Mon Sep 17 00:00:00 2001 From: Daksh Bhardwaj Date: Wed, 17 Aug 2022 16:04:34 +0530 Subject: [PATCH 2/4] test: added test cases --- src/components/__tests__/Searchbar.test.js | 6 + .../__snapshots__/Searchbar.test.js.snap | 384 ++++++++++++++++++ 2 files changed, 390 insertions(+) diff --git a/src/components/__tests__/Searchbar.test.js b/src/components/__tests__/Searchbar.test.js index 1d17e5187f..23f3fd426d 100644 --- a/src/components/__tests__/Searchbar.test.js +++ b/src/components/__tests__/Searchbar.test.js @@ -15,3 +15,9 @@ it('renders with text', () => { expect(tree).toMatchSnapshot(); }); + +it('renders with loadingIcon', () => { + const tree = renderer.create().toJSON(); + + expect(tree).toMatchSnapshot(); +}); diff --git a/src/components/__tests__/__snapshots__/Searchbar.test.js.snap b/src/components/__tests__/__snapshots__/Searchbar.test.js.snap index 66802d975c..cca04f7ced 100644 --- a/src/components/__tests__/__snapshots__/Searchbar.test.js.snap +++ b/src/components/__tests__/__snapshots__/Searchbar.test.js.snap @@ -1,5 +1,389 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`renders with loadingIcon 1`] = ` + + + + + + + + + □ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +`; + exports[`renders with placeholder 1`] = ` Date: Thu, 18 Aug 2022 12:51:04 +0530 Subject: [PATCH 3/4] test: added unit test cases for activity indicator --- src/components/__tests__/Searchbar.test.js | 25 ++++++++++++++++++- .../__snapshots__/Searchbar.test.js.snap | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/components/__tests__/Searchbar.test.js b/src/components/__tests__/Searchbar.test.js index 23f3fd426d..b0a0ccd350 100644 --- a/src/components/__tests__/Searchbar.test.js +++ b/src/components/__tests__/Searchbar.test.js @@ -1,3 +1,4 @@ +import { render } from '@testing-library/react-native'; import * as React from 'react'; import renderer from 'react-test-renderer'; import Searchbar from '../Searchbar.tsx'; @@ -16,8 +17,30 @@ it('renders with text', () => { expect(tree).toMatchSnapshot(); }); -it('renders with loadingIcon', () => { +it('activity indicator snapshot test', () => { const tree = renderer.create().toJSON(); expect(tree).toMatchSnapshot(); }); + +it('renders with ActivityIndicator', () => { + const tree = render( + + ); + + expect(tree.getByTestId('withActivityIndicator')).toBeTruthy(); +}); + +it('renders without ActivityIndicator', () => { + const tree = render( + + ); + + expect(() => tree.getByTestId('withOutActivityIndicator')).toThrow(); +}); diff --git a/src/components/__tests__/__snapshots__/Searchbar.test.js.snap b/src/components/__tests__/__snapshots__/Searchbar.test.js.snap index cca04f7ced..21618c4602 100644 --- a/src/components/__tests__/__snapshots__/Searchbar.test.js.snap +++ b/src/components/__tests__/__snapshots__/Searchbar.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`renders with loadingIcon 1`] = ` +exports[`activity indicator snapshot test 1`] = ` Date: Fri, 19 Aug 2022 17:35:03 +0530 Subject: [PATCH 4/4] refactor: removed the loaderStyle props and updated the docs --- src/components/Searchbar.tsx | 15 +++++-------- src/components/__tests__/Searchbar.test.js | 22 +++++-------------- .../__snapshots__/Searchbar.test.js.snap | 1 + 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/components/Searchbar.tsx b/src/components/Searchbar.tsx index e1f1a95fc0..e46a9a0fe2 100644 --- a/src/components/Searchbar.tsx +++ b/src/components/Searchbar.tsx @@ -18,9 +18,7 @@ import { withTheme } from '../core/theming'; import type { IconSource } from './Icon'; import type { Theme } from '../types'; import MaterialCommunityIcon from './MaterialCommunityIcon'; -import ActivityIndicator, { - Props as ActivityIndicatorProps, -} from './ActivityIndicator'; +import ActivityIndicator from './ActivityIndicator'; export type Props = React.ComponentPropsWithRef & { /** @@ -74,13 +72,10 @@ export type Props = React.ComponentPropsWithRef & { */ clearIcon?: IconSource; /** + * @supported Available in v5.x * Custom flag for replacing clear button with activity indicator. */ loading?: Boolean; - /** - * Custom styles for the activity indicator. - */ - loaderStyle?: Omit; }; type TextInputHandles = Pick< @@ -134,7 +129,6 @@ const Searchbar = React.forwardRef( theme, value, loading = false, - loaderStyle, ...rest }: Props, ref @@ -232,7 +226,10 @@ const Searchbar = React.forwardRef( {...rest} /> {loading ? ( - + ) : ( { }); it('renders with ActivityIndicator', () => { - const tree = render( - - ); - - expect(tree.getByTestId('withActivityIndicator')).toBeTruthy(); + const tree = render(); + + expect(tree.getByTestId('activity-indicator')).toBeTruthy(); }); it('renders without ActivityIndicator', () => { - const tree = render( - - ); - - expect(() => tree.getByTestId('withOutActivityIndicator')).toThrow(); + const tree = render(); + + expect(() => tree.getByTestId('activity-indicator')).toThrow(); }); diff --git a/src/components/__tests__/__snapshots__/Searchbar.test.js.snap b/src/components/__tests__/__snapshots__/Searchbar.test.js.snap index 21618c4602..21b696a044 100644 --- a/src/components/__tests__/__snapshots__/Searchbar.test.js.snap +++ b/src/components/__tests__/__snapshots__/Searchbar.test.js.snap @@ -201,6 +201,7 @@ exports[`activity indicator snapshot test 1`] = ` }, ] } + testID="activity-indicator" >