Skip to content

Commit 5ae55d0

Browse files
committed
refactor: remove onSearch and onChange callbacks from Search props
1 parent e014579 commit 5ae55d0

File tree

8 files changed

+24
-53
lines changed

8 files changed

+24
-53
lines changed

src/components/ChannelList/ChannelList.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,12 @@ const UnMemoizedChannelList = <
227227
useImageFlagEmojisOnWindows,
228228
} = useChatContext<StreamChatGenerics>('ChannelList');
229229
const { Search } = useComponentContext();
230-
const { searchIsActive } = useStateStore(searchController.state, searchControllerStateSelector);
231230
const channelListRef = useRef<HTMLDivElement>(null);
232231
const [channelUpdateCount, setChannelUpdateCount] = useState(0);
233232
const [searchActive, setSearchActive] = useState(false);
233+
234+
// Indicator relevant when Search component that relies on SearchController is used
235+
const { searchIsActive } = useStateStore(searchController.state, searchControllerStateSelector);
234236
/**
235237
* Set a channel with id {customActiveChannel} as active and move it to the top of the list.
236238
* If customActiveChannel prop is absent, then set the first channel in list as active channel.
@@ -388,12 +390,10 @@ const UnMemoizedChannelList = <
388390
{showChannelSearch &&
389391
(Search ? (
390392
<Search
393+
directMessagingChannelType={additionalChannelSearchProps?.channelType}
391394
disabled={additionalChannelSearchProps?.disabled}
392395
exitSearchOnInputBlur={additionalChannelSearchProps?.clearSearchOnClickOutside}
393-
inputOnChangeHandler={onSearch}
394-
onSearchExit={onSearchExit}
395396
placeholder={additionalChannelSearchProps?.placeholder}
396-
userToUserCreatedChannelType={additionalChannelSearchProps?.channelType}
397397
/>
398398
) : (
399399
<ChannelSearch

src/experimental/Search/Search.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,22 @@ const searchControllerStateSelector = <
2222

2323
// todo: rename all search components to Search only
2424
export type SearchProps = {
25+
directMessagingChannelType?: string;
2526
/** Sets the input element into disabled state */
2627
disabled?: boolean;
2728
/** Clear search state / results on every click outside the search input, defaults to true */
2829
exitSearchOnInputBlur?: boolean;
29-
/** Callback invoked with every search input change handler */
30-
inputOnChangeHandler?: React.ChangeEventHandler<HTMLInputElement>;
31-
/** Callback invoked when the search UI is deactivated */
32-
onSearchExit?: () => void;
3330
/** Custom placeholder text to be displayed in the search input */
3431
placeholder?: string;
35-
userToUserCreatedChannelType?: string;
3632
};
3733

3834
export const Search = <
3935
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
4036
>({
37+
directMessagingChannelType = 'messaging',
4138
disabled,
4239
exitSearchOnInputBlur,
43-
inputOnChangeHandler,
44-
onSearchExit,
4540
placeholder,
46-
userToUserCreatedChannelType = 'messaging',
4741
}: SearchProps) => {
4842
const {
4943
SearchBar = DefaultSearchBar,
@@ -60,13 +54,11 @@ export const Search = <
6054
return (
6155
<SearchContextProvider<StreamChatGenerics>
6256
value={{
57+
directMessagingChannelType,
6358
disabled,
6459
exitSearchOnInputBlur,
65-
inputOnChangeHandler,
66-
onSearchExit,
6760
placeholder,
6861
searchController,
69-
userToUserCreatedChannelType,
7062
}}
7163
>
7264
<div

src/experimental/Search/SearchBar/SearchBar.tsx

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import clsx from 'clsx';
2-
import React, { useCallback, useEffect } from 'react';
2+
import React, { useEffect } from 'react';
33

44
import { useSearchContext } from '../SearchContext';
55
import { useSearchQueriesInProgress } from '../hooks';
@@ -16,39 +16,28 @@ const searchControllerStateSelector = (nextValue: SearchControllerState) => ({
1616

1717
export const SearchBar = () => {
1818
const { t } = useTranslationContext();
19-
const {
20-
disabled,
21-
exitSearchOnInputBlur,
22-
onSearchExit,
23-
placeholder,
24-
searchController,
25-
} = useSearchContext();
19+
const { disabled, exitSearchOnInputBlur, placeholder, searchController } = useSearchContext();
2620
const queriesInProgress = useSearchQueriesInProgress(searchController);
2721

2822
const { input, isActive, searchQuery } = useStateStore(
2923
searchController.state,
3024
searchControllerStateSelector,
3125
);
3226

33-
const exitSearch = useCallback(() => {
34-
searchController.exit();
35-
onSearchExit?.();
36-
}, [searchController, onSearchExit]);
37-
3827
useEffect(() => {
3928
if (!input) return;
4029
const handleKeyDown = (event: KeyboardEvent) => {
4130
if (event.key === 'Escape') {
4231
input.blur();
43-
exitSearch();
32+
searchController.exit();
4433
}
4534
};
4635

4736
document.addEventListener('keydown', handleKeyDown);
4837
return () => {
4938
document.removeEventListener('keydown', handleKeyDown);
5039
};
51-
}, [exitSearch, input]);
40+
}, [searchController, input]);
5241

5342
return (
5443
<div className='str-chat__search-bar' data-testid='search-bar'>
@@ -63,7 +52,7 @@ export const SearchBar = () => {
6352
data-testid='search-input'
6453
disabled={disabled}
6554
onBlur={() => {
66-
if (exitSearchOnInputBlur) exitSearch();
55+
if (exitSearchOnInputBlur) searchController.exit();
6756
}}
6857
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
6958
if (event.target.value) {
@@ -98,7 +87,7 @@ export const SearchBar = () => {
9887
data-testid='search-bar-button'
9988
onClick={() => {
10089
input?.blur();
101-
exitSearch();
90+
searchController.exit();
10291
}}
10392
>
10493
{t<string>('Cancel')}

src/experimental/Search/SearchContext.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ import type { DefaultStreamChatGenerics } from '../../types';
55
export type SearchContextValue<
66
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
77
> = {
8-
searchController: SearchController<StreamChatGenerics>;
98
/** The type of channel to create on user result select, defaults to `messaging` */
10-
userToUserCreatedChannelType: string;
9+
directMessagingChannelType: string;
10+
searchController: SearchController<StreamChatGenerics>;
1111
/** Sets the input element into disabled state */
1212
disabled?: boolean;
1313
// /** Clear search state / results on every click outside the search input, defaults to true */
1414
exitSearchOnInputBlur?: boolean;
15-
/** Search input change handler */
16-
inputOnChangeHandler?: React.ChangeEventHandler<HTMLInputElement>;
17-
/** Custom callback invoked when the search UI is deactivated */
18-
onSearchExit?: () => void;
1915
// todo: document this is not available - better override search result item
2016
/** Custom handler function to run on search result item selection */
2117
// onSelectResult?: (

src/experimental/Search/SearchResults/SearchResultItem.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,16 @@ export const UserSearchResultItem = <
100100
}: UserSearchResultItemProps<StreamChatGenerics>) => {
101101
const { client, setActiveChannel } = useChatContext<StreamChatGenerics>();
102102
const { setChannels } = useChannelListContext<StreamChatGenerics>();
103-
const { userToUserCreatedChannelType } = useSearchContext<StreamChatGenerics>();
103+
const { directMessagingChannelType } = useSearchContext<StreamChatGenerics>();
104104

105105
const onClick = useCallback(() => {
106-
const newChannel = client.channel(userToUserCreatedChannelType, {
106+
const newChannel = client.channel(directMessagingChannelType, {
107107
members: [client.userID as string, item.id],
108108
});
109109
newChannel.watch();
110110
setActiveChannel(newChannel);
111111
setChannels?.((channels) => uniqBy([newChannel, ...channels], 'cid'));
112-
}, [client, item, setActiveChannel, setChannels, userToUserCreatedChannelType]);
112+
}, [client, item, setActiveChannel, setChannels, directMessagingChannelType]);
113113

114114
return (
115115
<button

src/experimental/Search/__tests__/Search.test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ describe('Search', () => {
2424
};
2525

2626
const defaultProps = {
27+
directMessagingChannelType: 'messaging',
2728
disabled: false,
2829
exitSearchOnInputBlur: true,
29-
inputOnChangeHandler: jest.fn(),
30-
onSearchExit: jest.fn(),
3130
placeholder: 'Search',
32-
userToUserCreatedChannelType: 'messaging',
3331
};
3432

3533
beforeEach(() => {
@@ -180,12 +178,12 @@ describe('Search', () => {
180178
expect(screen.getByTestId(SEARCH_TEST_ID)).toHaveClass('str-chat__search--active');
181179
});
182180

183-
it('uses default userToUserCreatedChannelType when not provided', () => {
184-
const { userToUserCreatedChannelType, ...propsWithoutChannelType } = defaultProps;
181+
it('uses default directMessagingChannelType when not provided', () => {
182+
const { directMessagingChannelType, ...propsWithoutChannelType } = defaultProps;
185183

186184
const ContextConsumer = () => {
187185
const context = React.useContext(jest.requireActual('../SearchContext').SearchContext);
188-
return <div data-testid='channel-type'>{context.userToUserCreatedChannelType}</div>;
186+
return <div data-testid='channel-type'>{context.directMessagingChannelType}</div>;
189187
};
190188
const SearchBar = () => <ContextConsumer />;
191189
useComponentContext.mockReturnValue({

src/experimental/Search/__tests__/SearchBar.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ describe('SearchBar', () => {
2828
const defaultProps = {
2929
disabled: false,
3030
exitSearchOnInputBlur: false,
31-
onSearchExit: jest.fn(),
3231
placeholder: 'Custom placeholder',
3332
searchController: mockSearchController,
3433
};
@@ -148,7 +147,6 @@ describe('SearchBar', () => {
148147

149148
expect(mockInput.blur).toHaveBeenCalledWith();
150149
expect(mockSearchController.exit).toHaveBeenCalledWith();
151-
expect(defaultProps.onSearchExit).toHaveBeenCalledWith();
152150
});
153151

154152
it('handles escape key press', () => {
@@ -165,7 +163,6 @@ describe('SearchBar', () => {
165163

166164
expect(mockInput.blur).toHaveBeenCalledWith();
167165
expect(mockSearchController.exit).toHaveBeenCalledWith();
168-
expect(defaultProps.onSearchExit).toHaveBeenCalledWith();
169166
});
170167

171168
it('handles blur when exitSearchOnInputBlur is true', () => {
@@ -181,7 +178,6 @@ describe('SearchBar', () => {
181178
fireEvent.blur(input);
182179

183180
expect(mockSearchController.exit).toHaveBeenCalledWith();
184-
expect(defaultProps.onSearchExit).toHaveBeenCalledWith();
185181
});
186182

187183
it('disables input when disabled prop is true', () => {

src/experimental/Search/__tests__/SearchResultItem.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const CHANNEL_PREVIEW_BUTTON_TEST_ID = 'channel-preview-button';
2222

2323
const mockSetActiveChannel = jest.fn().mockImplementation();
2424
const mockSetChannels = jest.fn().mockImplementation();
25-
const userToUserCreatedChannelType = 'X';
25+
const directMessagingChannelType = 'X';
2626

2727
const renderComponent = async ({
2828
activeChannel,
@@ -63,7 +63,7 @@ const renderComponent = async ({
6363
}}
6464
>
6565
<ChannelListContextProvider value={{ setChannels: mockSetChannels }}>
66-
<SearchContextProvider value={{ userToUserCreatedChannelType }}>
66+
<SearchContextProvider value={{ directMessagingChannelType }}>
6767
<SearchResultItemComponent item={item} />
6868
</SearchContextProvider>
6969
</ChannelListContextProvider>

0 commit comments

Comments
 (0)