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 all 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
57 changes: 37 additions & 20 deletions src/components/Searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { withTheme } from '../core/theming';
import type { IconSource } from './Icon';
import type { Theme } from '../types';
import MaterialCommunityIcon from './MaterialCommunityIcon';
import ActivityIndicator from './ActivityIndicator';

export type Props = React.ComponentPropsWithRef<typeof TextInput> & {
/**
Expand Down Expand Up @@ -70,6 +71,11 @@ export type Props = React.ComponentPropsWithRef<typeof TextInput> & {
* Custom icon for clear button, default will be icon close
*/
clearIcon?: IconSource;
/**
* @supported Available in v5.x
* Custom flag for replacing clear button with activity indicator.
dakshbhardwaj marked this conversation as resolved.
Show resolved Hide resolved
*/
loading?: Boolean;
};

type TextInputHandles = Pick<
Expand Down Expand Up @@ -122,6 +128,7 @@ const Searchbar = React.forwardRef<TextInputHandles, Props>(
style,
theme,
value,
loading = false,
...rest
}: Props,
ref
Expand Down Expand Up @@ -218,26 +225,33 @@ 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
testID="activity-indicator"
style={styles.loader}
/>
) : (
<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 +273,9 @@ const styles = StyleSheet.create({
elevation: {
elevation: 4,
},
loader: {
margin: 10,
},
});

export default withTheme(Searchbar);
19 changes: 19 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,21 @@ 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} />);

expect(tree.getByTestId('activity-indicator')).toBeTruthy();
});

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

expect(() => tree.getByTestId('activity-indicator')).toThrow();
});
Loading