Skip to content

Commit 29224f8

Browse files
roffelundGustav Haglund
and
Gustav Haglund
authored
(base/SearchBar): Remove deprecated Keyboard.removeListener (react-native-elements#3215)
Co-authored-by: Gustav Haglund <[email protected]>
1 parent 8c285ca commit 29224f8

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

packages/base/src/SearchBar/SearchBar-android.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ActivityIndicator,
66
Keyboard,
77
TextInput,
8+
EmitterSubscription,
89
} from 'react-native';
910
import { defaultTheme, renderNode } from '../helpers';
1011
import { Input, InputProps } from '../Input';
@@ -49,7 +50,7 @@ export class SearchBarAndroid extends Component<
4950
SearchBarState
5051
> {
5152
input!: TextInput;
52-
_keyboardDidHideListener;
53+
5354
static defaultProps = {
5455
onClear: () => null,
5556
onCancel: () => null,
@@ -58,6 +59,8 @@ export class SearchBarAndroid extends Component<
5859
onChangeText: () => null,
5960
};
6061

62+
keyboardListener: EmitterSubscription;
63+
6164
focus = () => {
6265
this.input.focus();
6366
};
@@ -102,7 +105,7 @@ export class SearchBarAndroid extends Component<
102105
hasFocus: false,
103106
isEmpty: value ? value === '' : true,
104107
};
105-
this._keyboardDidHideListener = Keyboard.addListener(
108+
this.keyboardListener = Keyboard.addListener(
106109
'keyboardDidHide',
107110
this._keyboardDidHide
108111
);
@@ -112,7 +115,9 @@ export class SearchBarAndroid extends Component<
112115
};
113116

114117
componentWillUnmount() {
115-
this._keyboardDidHideListener.remove();
118+
if (this.keyboardListener) {
119+
this.keyboardListener.remove();
120+
}
116121
}
117122

118123
render() {

packages/base/src/SearchBar/__tests__/SearchBar.test.tsx

+30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import SearchBar from '../index';
33
import { renderWithWrapper } from '../../../.ci/testHelper';
4+
import { Keyboard } from 'react-native';
45

56
describe('SearchBar wrapper component', () => {
67
it('should match snapshot', () => {
@@ -37,4 +38,33 @@ describe('SearchBar wrapper component', () => {
3738
);
3839
expect(component.toJSON()).toMatchSnapshot();
3940
});
41+
42+
describe('keyboard eventListener', () => {
43+
const mockListener = {
44+
remove: jest.fn(),
45+
};
46+
const originalAddListener = Keyboard.addListener;
47+
const mockAddListener = jest.fn().mockReturnValue(mockListener);
48+
49+
beforeAll(() => {
50+
Keyboard.addListener = mockAddListener;
51+
});
52+
beforeEach(() => {
53+
mockAddListener.mockClear();
54+
mockListener.remove.mockClear();
55+
});
56+
afterAll(() => {
57+
Keyboard.addListener = originalAddListener;
58+
});
59+
it('should subscribe to KeyboardDidClose event', () => {
60+
renderWithWrapper(<SearchBar platform="android" />);
61+
expect(Keyboard.addListener).toHaveBeenCalled();
62+
});
63+
64+
it('should call listener.remove on unmount', () => {
65+
const component = renderWithWrapper(<SearchBar platform="android" />);
66+
component.unmount();
67+
expect(mockListener.remove).toHaveBeenCalled();
68+
});
69+
});
4070
});

0 commit comments

Comments
 (0)