Skip to content

Commit 9125545

Browse files
authored
feat: Add onClearIconPress to SearchBar (#3679)
1 parent e961a87 commit 9125545

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

example/src/Examples/SearchbarExample.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { StyleSheet } from 'react-native';
2+
import { Keyboard, StyleSheet } from 'react-native';
33

44
import type { StackNavigationProp } from '@react-navigation/stack';
55
import { Caption, Searchbar, Text } from 'react-native-paper';
@@ -45,6 +45,10 @@ const SearchExample = ({ navigation }: Props) => {
4545
onChangeText={(query: string) => setThirdQuery(query)}
4646
value={thirdQuery}
4747
onIconPress={/* In real code, this will open the drawer */ () => {}}
48+
onClearIconPress={
49+
/* delete query text (default behavior) and dismiss keyboard */ () =>
50+
Keyboard.dismiss()
51+
}
4852
icon="menu"
4953
style={styles.searchbar}
5054
/>

src/components/Searchbar.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ export type Props = React.ComponentPropsWithRef<typeof TextInput> & {
5353
* Callback to execute if we want the left icon to act as button.
5454
*/
5555
onIconPress?: (e: GestureResponderEvent) => void;
56+
57+
/**
58+
* Callback to execute if we want to add custom behaviour to close icon button.
59+
*/
60+
onClearIconPress?: (e: GestureResponderEvent) => void;
5661
/**
5762
* @supported Available in v5.x with theme version 3
5863
* Changes Searchbar shadow and background on iOS and Android.
@@ -129,6 +134,7 @@ const Searchbar = forwardRef<TextInputHandles, Props>(
129134
iconColor: customIconColor,
130135
inputStyle,
131136
onIconPress,
137+
onClearIconPress,
132138
placeholder,
133139
searchAccessibilityLabel = 'search',
134140
elevation = 1,
@@ -170,9 +176,10 @@ const Searchbar = forwardRef<TextInputHandles, Props>(
170176
};
171177
});
172178

173-
const handleClearPress = () => {
179+
const handleClearPress = (e: any) => {
174180
root.current?.clear();
175181
rest.onChangeText?.('');
182+
onClearIconPress?.(e);
176183
};
177184

178185
const { colors, roundness, dark, isV3 } = theme;

src/components/__tests__/Searchbar.test.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { Animated } from 'react-native';
33

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

77
import Searchbar from '../Searchbar';
@@ -93,3 +93,18 @@ it('animated value changes correctly', () => {
9393
transform: [{ scale: 1.5 }],
9494
});
9595
});
96+
97+
it('defines onClearIconPress action and checks if it is called when close button is pressed', () => {
98+
const onClearIconPressMock = jest.fn();
99+
const { getByTestId } = render(
100+
<Searchbar
101+
testID="search-bar"
102+
value="value"
103+
onClearIconPress={onClearIconPressMock}
104+
/>
105+
);
106+
const iconComponent = getByTestId('search-bar-icon-wrapper').props.children;
107+
108+
fireEvent(iconComponent, 'onPress');
109+
expect(onClearIconPressMock).toHaveBeenCalledTimes(1);
110+
});

0 commit comments

Comments
 (0)