Skip to content

Commit 6dacf11

Browse files
committed
Change name of focus and blur events to searchFocus and searchBlur
1 parent 4f6f974 commit 6dacf11

File tree

8 files changed

+54
-25
lines changed

8 files changed

+54
-25
lines changed

android/src/main/java/com/swmansion/rnscreens/SearchBarManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ class SearchBarManager : ViewGroupManager<SearchBarView>() {
114114
override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any>? {
115115
return MapBuilder.of(
116116
SearchBarBlurEvent.EVENT_NAME,
117-
MapBuilder.of("registrationName", "onBlur"),
117+
MapBuilder.of("registrationName", "onSearchBlur"),
118118
SearchBarChangeTextEvent.EVENT_NAME,
119119
MapBuilder.of("registrationName", "onChangeText"),
120120
SearchBarCloseEvent.EVENT_NAME,
121121
MapBuilder.of("registrationName", "onClose"),
122122
SearchBarFocusEvent.EVENT_NAME,
123-
MapBuilder.of("registrationName", "onFocus"),
123+
MapBuilder.of("registrationName", "onSearchFocus"),
124124
SearchBarOpenEvent.EVENT_NAME,
125125
MapBuilder.of("registrationName", "onOpen"),
126126
SearchBarSearchButtonPressEvent.EVENT_NAME,

android/src/main/java/com/swmansion/rnscreens/events/SearchBarBlurEvent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ class SearchBarBlurEvent(surfaceId: Int, viewId: Int) : Event<SearchBarBlurEvent
1313
override fun getEventData(): WritableMap? = Arguments.createMap()
1414

1515
companion object {
16-
const val EVENT_NAME = "topBlur"
16+
const val EVENT_NAME = "topSearchBlur"
1717
}
1818
}

android/src/main/java/com/swmansion/rnscreens/events/SearchBarFocusEvent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ class SearchBarFocusEvent(surfaceId: Int, viewId: Int) : Event<SearchBarFocusEve
1313
override fun getEventData(): WritableMap? = Arguments.createMap()
1414

1515
companion object {
16-
const val EVENT_NAME = "topFocus"
16+
const val EVENT_NAME = "topSearchFocus"
1717
}
1818
}

ios/RNSSearchBar.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929

3030
#ifdef RCT_NEW_ARCH_ENABLED
3131
#else
32-
@property (nonatomic, copy) RCTBubblingEventBlock onChangeText;
33-
@property (nonatomic, copy) RCTBubblingEventBlock onCancelButtonPress;
34-
@property (nonatomic, copy) RCTBubblingEventBlock onSearchButtonPress;
35-
@property (nonatomic, copy) RCTBubblingEventBlock onFocus;
36-
@property (nonatomic, copy) RCTBubblingEventBlock onBlur;
32+
@property (nonatomic, copy) RCTDirectEventBlock onChangeText;
33+
@property (nonatomic, copy) RCTDirectEventBlock onCancelButtonPress;
34+
@property (nonatomic, copy) RCTDirectEventBlock onSearchButtonPress;
35+
@property (nonatomic, copy) RCTDirectEventBlock onSearchFocus;
36+
@property (nonatomic, copy) RCTDirectEventBlock onSearchBlur;
3737
#endif
3838

3939
@end

ios/RNSSearchBar.mm

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ - (void)emitOnFocusEvent
6767
#ifdef RCT_NEW_ARCH_ENABLED
6868
if (_eventEmitter != nullptr) {
6969
std::dynamic_pointer_cast<const react::RNSSearchBarEventEmitter>(_eventEmitter)
70-
->onFocus(react::RNSSearchBarEventEmitter::OnFocus{});
70+
->onSearchFocus(react::RNSSearchBarEventEmitter::OnSearchFocus{});
7171
}
7272
#else
73-
if (self.onFocus) {
74-
self.onFocus(@{});
73+
if (self.onSearchFocus) {
74+
self.onSearchFocus(@{});
7575
}
7676
#endif
7777
}
@@ -81,11 +81,11 @@ - (void)emitOnBlurEvent
8181
#ifdef RCT_NEW_ARCH_ENABLED
8282
if (_eventEmitter != nullptr) {
8383
std::dynamic_pointer_cast<const react::RNSSearchBarEventEmitter>(_eventEmitter)
84-
->onBlur(react::RNSSearchBarEventEmitter::OnBlur{});
84+
->onSearchBlur(react::RNSSearchBarEventEmitter::OnSearchBlur{});
8585
}
8686
#else
87-
if (self.onBlur) {
88-
self.onBlur(@{});
87+
if (self.onSearchBlur) {
88+
self.onSearchBlur(@{});
8989
}
9090
#endif
9191
}
@@ -414,11 +414,11 @@ - (UIView *)view
414414
RCT_EXPORT_VIEW_PROPERTY(cancelButtonText, NSString)
415415
RCT_EXPORT_VIEW_PROPERTY(placement, RNSSearchBarPlacement)
416416

417-
RCT_EXPORT_VIEW_PROPERTY(onChangeText, RCTBubblingEventBlock)
418-
RCT_EXPORT_VIEW_PROPERTY(onCancelButtonPress, RCTBubblingEventBlock)
419-
RCT_EXPORT_VIEW_PROPERTY(onSearchButtonPress, RCTBubblingEventBlock)
420-
RCT_EXPORT_VIEW_PROPERTY(onFocus, RCTBubblingEventBlock)
421-
RCT_EXPORT_VIEW_PROPERTY(onBlur, RCTBubblingEventBlock)
417+
RCT_EXPORT_VIEW_PROPERTY(onChangeText, RCTDirectEventBlock)
418+
RCT_EXPORT_VIEW_PROPERTY(onCancelButtonPress, RCTDirectEventBlock)
419+
RCT_EXPORT_VIEW_PROPERTY(onSearchButtonPress, RCTDirectEventBlock)
420+
RCT_EXPORT_VIEW_PROPERTY(onSearchFocus, RCTDirectEventBlock)
421+
RCT_EXPORT_VIEW_PROPERTY(onSearchBlur, RCTDirectEventBlock)
422422

423423
#ifndef RCT_NEW_ARCH_ENABLED
424424

src/components/SearchBar.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
isSearchBarAvailableForCurrentPlatform,
44
SearchBarCommands,
55
SearchBarProps,
6+
RenamedSearchBarProps,
67
} from 'react-native-screens';
78
import { View } from 'react-native';
89

@@ -11,9 +12,9 @@ import SearchBarNativeComponent, {
1112
Commands as SearchBarNativeCommands,
1213
} from '../fabric/SearchBarNativeComponent';
1314

14-
export const NativeSearchBar: React.ComponentType<SearchBarProps> &
15+
export const NativeSearchBar: React.ComponentType<RenamedSearchBarProps> &
1516
typeof NativeSearchBarCommands =
16-
SearchBarNativeComponent as unknown as React.ComponentType<SearchBarProps> &
17+
SearchBarNativeComponent as unknown as React.ComponentType<RenamedSearchBarProps> &
1718
SearchBarCommandsType;
1819
export const NativeSearchBarCommands: SearchBarCommandsType =
1920
SearchBarNativeCommands as SearchBarCommandsType;
@@ -76,7 +77,14 @@ function SearchBar(props: SearchBarProps, ref: React.Ref<SearchBarCommands>) {
7677
return View as unknown as React.ReactNode;
7778
}
7879

79-
return <NativeSearchBar {...props} ref={searchBarRef} />;
80+
return (
81+
<NativeSearchBar
82+
onSearchFocus={props.onFocus}
83+
onSearchBlur={props.onBlur}
84+
{...props}
85+
ref={searchBarRef}
86+
/>
87+
);
8088
}
8189

8290
export default React.forwardRef<SearchBarCommands, SearchBarProps>(SearchBar);

src/fabric/SearchBarNativeComponent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ type SearchBarPlacement = 'automatic' | 'inline' | 'stacked';
2222
type AutoCapitalizeType = 'none' | 'words' | 'sentences' | 'characters';
2323

2424
interface NativeProps extends ViewProps {
25-
onFocus?: DirectEventHandler<SearchBarEvent> | null;
26-
onBlur?: DirectEventHandler<SearchBarEvent> | null;
25+
onSearchFocus?: DirectEventHandler<SearchBarEvent> | null;
26+
onSearchBlur?: DirectEventHandler<SearchBarEvent> | null;
2727
onSearchButtonPress?: DirectEventHandler<SearchButtonPressedEvent> | null;
2828
onCancelButtonPress?: DirectEventHandler<SearchBarEvent> | null;
2929
onChangeText?: DirectEventHandler<ChangeTextEvent> | null;

src/types.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,3 +713,24 @@ export interface SearchBarProps {
713713
*/
714714
shouldShowHintSearchIcon?: boolean;
715715
}
716+
717+
/**
718+
* Since the search bar is a component that is extended from View on iOS,
719+
* we can't use onFocus and onBlur events directly there (as of the event naming conflicts).
720+
* To omit any breaking changes, we're handling this type to rename onFocus and onBlur events
721+
* to onSearchFocus and onSearchBlur inside the native component of the search bar.
722+
*/
723+
export type RenamedSearchBarProps = Rename<
724+
SearchBarProps,
725+
{ onFocus: 'onSearchFocus'; onBlur: 'onSearchBlur' }
726+
>;
727+
728+
/**
729+
* Helper type, used to rename certain keys in the interface, given from object.
730+
*/
731+
type Rename<
732+
T,
733+
R extends {
734+
[K in keyof R]: K extends keyof T ? PropertyKey : 'Error: key not in T';
735+
}
736+
> = { [P in keyof T as P extends keyof R ? R[P] : P]: T[P] };

0 commit comments

Comments
 (0)