Skip to content
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

feat: added loading icon in Searchbar #3322

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions example/src/Examples/SearchbarExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const SearchExample = ({ navigation }: Props) => {
const [firstQuery, setFirstQuery] = React.useState<string>('');
const [secondQuery, setSecondQuery] = React.useState<string>('');
const [thirdQuery, setThirdQuery] = React.useState<string>('');
const [fourthQuery, setFourthQuery] = React.useState<string>('');

const { isV3 } = useTheme();

Expand Down Expand Up @@ -44,6 +45,14 @@ const SearchExample = ({ navigation }: Props) => {
icon="menu"
style={styles.searchbar}
/>

<Searchbar
placeholder="Search"
onChangeText={(query: string) => setFourthQuery(query)}
value={fourthQuery}
loading
style={styles.searchbar}
/>
</ScreenWrapper>
);
};
Expand Down
60 changes: 40 additions & 20 deletions src/components/Searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof TextInput> & {
/**
Expand Down Expand Up @@ -70,6 +73,14 @@ export type Props = React.ComponentPropsWithRef<typeof TextInput> & {
* Custom icon for clear button, default will be icon close
*/
clearIcon?: IconSource;
/**
* Custom flag for replacing clear button with activity indicator.
dakshbhardwaj marked this conversation as resolved.
Show resolved Hide resolved
*/
loading?: Boolean;
/**
* Custom styles for the activity indicator.
*/
loaderStyle?: Omit<ActivityIndicatorProps, 'theme'>;
};

type TextInputHandles = Pick<
Expand Down Expand Up @@ -122,6 +133,8 @@ const Searchbar = React.forwardRef<TextInputHandles, Props>(
style,
theme,
value,
loading = false,
loaderStyle,
...rest
}: Props,
ref
Expand Down Expand Up @@ -218,26 +231,30 @@ const Searchbar = React.forwardRef<TextInputHandles, Props>(
value={value}
{...rest}
/>
<IconButton
borderless
disabled={!value}
accessibilityLabel={clearAccessibilityLabel}
iconColor={value ? iconColor : 'rgba(255, 255, 255, 0)'}
rippleColor={rippleColor}
onPress={handleClearPress}
icon={
clearIcon ||
(({ size, color }) => (
<MaterialCommunityIcon
name="close"
color={color}
size={size}
direction={I18nManager.isRTL ? 'rtl' : 'ltr'}
/>
))
}
accessibilityRole="button"
/>
{loading ? (
dakshbhardwaj marked this conversation as resolved.
Show resolved Hide resolved
<ActivityIndicator {...loaderStyle} style={styles.loader} />
dakshbhardwaj marked this conversation as resolved.
Show resolved Hide resolved
) : (
<IconButton
borderless
disabled={!value}
accessibilityLabel={clearAccessibilityLabel}
iconColor={value ? iconColor : 'rgba(255, 255, 255, 0)'}
rippleColor={rippleColor}
onPress={handleClearPress}
icon={
clearIcon ||
(({ size, color }) => (
<MaterialCommunityIcon
name="close"
color={color}
size={size}
direction={I18nManager.isRTL ? 'rtl' : 'ltr'}
/>
))
}
accessibilityRole="button"
/>
)}
</Surface>
);
}
Expand All @@ -259,6 +276,9 @@ const styles = StyleSheet.create({
elevation: {
elevation: 4,
},
loader: {
margin: 10,
},
});

export default withTheme(Searchbar);
29 changes: 29 additions & 0 deletions src/components/__tests__/Searchbar.test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,3 +16,31 @@ it('renders with text', () => {

expect(tree).toMatchSnapshot();
});

it('activity indicator snapshot test', () => {
const tree = renderer.create(<Searchbar loading={true} />).toJSON();

expect(tree).toMatchSnapshot();
});

it('renders with ActivityIndicator', () => {
const tree = render(
<Searchbar
loading={true}
loaderStyle={{ testID: 'withActivityIndicator' }}
/>
);

expect(tree.getByTestId('withActivityIndicator')).toBeTruthy();
});

it('renders without ActivityIndicator', () => {
const tree = render(
<Searchbar
loading={false}
loaderStyle={{ testID: 'withOutActivityIndicator' }}
/>
);

expect(() => tree.getByTestId('withOutActivityIndicator')).toThrow();
});
Loading