Skip to content

Commit

Permalink
feat: Add onClearIconPress to SearchBar (#3679)
Browse files Browse the repository at this point in the history
  • Loading branch information
n0tl3ss authored Mar 6, 2023
1 parent e961a87 commit 9125545
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 5 additions & 1 deletion example/src/Examples/SearchbarExample.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { StyleSheet } from 'react-native';
import { Keyboard, StyleSheet } from 'react-native';

import type { StackNavigationProp } from '@react-navigation/stack';
import { Caption, Searchbar, Text } from 'react-native-paper';
Expand Down Expand Up @@ -45,6 +45,10 @@ const SearchExample = ({ navigation }: Props) => {
onChangeText={(query: string) => setThirdQuery(query)}
value={thirdQuery}
onIconPress={/* In real code, this will open the drawer */ () => {}}
onClearIconPress={
/* delete query text (default behavior) and dismiss keyboard */ () =>
Keyboard.dismiss()
}
icon="menu"
style={styles.searchbar}
/>
Expand Down
9 changes: 8 additions & 1 deletion src/components/Searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export type Props = React.ComponentPropsWithRef<typeof TextInput> & {
* Callback to execute if we want the left icon to act as button.
*/
onIconPress?: (e: GestureResponderEvent) => void;

/**
* Callback to execute if we want to add custom behaviour to close icon button.
*/
onClearIconPress?: (e: GestureResponderEvent) => void;
/**
* @supported Available in v5.x with theme version 3
* Changes Searchbar shadow and background on iOS and Android.
Expand Down Expand Up @@ -129,6 +134,7 @@ const Searchbar = forwardRef<TextInputHandles, Props>(
iconColor: customIconColor,
inputStyle,
onIconPress,
onClearIconPress,
placeholder,
searchAccessibilityLabel = 'search',
elevation = 1,
Expand Down Expand Up @@ -170,9 +176,10 @@ const Searchbar = forwardRef<TextInputHandles, Props>(
};
});

const handleClearPress = () => {
const handleClearPress = (e: any) => {
root.current?.clear();
rest.onChangeText?.('');
onClearIconPress?.(e);
};

const { colors, roundness, dark, isV3 } = theme;
Expand Down
17 changes: 16 additions & 1 deletion src/components/__tests__/Searchbar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { Animated } from 'react-native';

import { render } from '@testing-library/react-native';
import { fireEvent, render } from '@testing-library/react-native';
import renderer from 'react-test-renderer';

import Searchbar from '../Searchbar';
Expand Down Expand Up @@ -93,3 +93,18 @@ it('animated value changes correctly', () => {
transform: [{ scale: 1.5 }],
});
});

it('defines onClearIconPress action and checks if it is called when close button is pressed', () => {
const onClearIconPressMock = jest.fn();
const { getByTestId } = render(
<Searchbar
testID="search-bar"
value="value"
onClearIconPress={onClearIconPressMock}
/>
);
const iconComponent = getByTestId('search-bar-icon-wrapper').props.children;

fireEvent(iconComponent, 'onPress');
expect(onClearIconPressMock).toHaveBeenCalledTimes(1);
});

0 comments on commit 9125545

Please sign in to comment.