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 1 commit
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);