Skip to content

Commit

Permalink
feat: add setText command on SearchBar (#1739)
Browse files Browse the repository at this point in the history
## Description

Add `setText` native command to SearchBar imperative API.

## Changes

Added the JS impl, native impl (both Android & iOS, Fabric & Paper),
updated JS types & codegen specs.


## Test code and steps to reproduce

`Test1097` in both `FabricTestExample` & `TestsExample`

## Checklist

- [x] Included code example that can be used to test this change
- [x] Updated TS types
- [x] Updated documentation: <!-- For adding new props to native-stack
-->
- [x]
https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md
- [x]
https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md
- [x]
https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx
- [x]
https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx
- [x] Ensured that CI passes
  • Loading branch information
kkafar authored Mar 16, 2023
1 parent 2a30e22 commit 9112700
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 11 deletions.
18 changes: 11 additions & 7 deletions FabricTestExample/src/Test1097.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,20 @@ function Third({navigation}: {navigation: NavigationProp<ParamListBase>}) {
hideNavigationBar: false,
autoCapitalize: 'sentences',
placeholder: 'Some text',
onChangeText: (e: NativeSyntheticEvent<{text: string}>) => console.warn(`Text changed to ${e.nativeEvent.text}`),
onChangeText: (e: NativeSyntheticEvent<{text: string}>) =>
console.warn(`Text changed to ${e.nativeEvent.text}`),
onCancelButtonPress: () => console.warn('Cancel button pressed'),
onSearchButtonPress: () => console.warn('Search button pressed'),
onFocus: () => console.warn('onFocus event'),
onBlur: () => console.warn('onBlur event'),
}
};

React.useEffect(() => {
navigation.setOptions({
searchBar: searchBarProps
})
searchBar: searchBarProps,
});
}, [navigation]);


return (
<ScrollView
contentInsetAdjustmentBehavior="automatic"
Expand All @@ -172,7 +172,7 @@ function Third({navigation}: {navigation: NavigationProp<ParamListBase>}) {
title="Focus search bar"
onPress={() => searchBarRef.current?.focus()}
/>
<Button
<Button
title="Remove focus from search bar"
onPress={() => searchBarRef.current?.blur()}
/>
Expand All @@ -188,6 +188,10 @@ function Third({navigation}: {navigation: NavigationProp<ParamListBase>}) {
title="Hide cancel button"
onPress={() => searchBarRef.current?.toggleCancelButton(false)}
/>
<Button
title="Set 'sometext' text"
onPress={() => searchBarRef.current?.setText('sometext')}
/>
<Button
title="Tap me for the first screen"
onPress={() => navigation.navigate('First')}
Expand All @@ -197,5 +201,5 @@ function Third({navigation}: {navigation: NavigationProp<ParamListBase>}) {
onPress={() => searchBarRef.current?.focus()}
/>
</ScrollView>
)
);
}
4 changes: 4 additions & 0 deletions TestsExample/src/Test1097.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ function Third({navigation}: {navigation: NavigationProp<ParamListBase>}) {
title="Hide cancel button"
onPress={() => searchBarRef.current?.toggleCancelButton(false)}
/>
<Button
title="Set 'sometext' text"
onPress={() => searchBarRef.current?.setText('sometext')}
/>
<Button
title="Tap me for the first screen"
onPress={() => navigation.navigate('First')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class CustomSearchView(context: Context, fragment: Fragment) : SearchView(contex
requestFocusFromTouch()
}

fun clearText() {
setQuery("", false)
}
fun clearText() = setQuery("", false)

fun setText(text: String) = setQuery(text, false)

override fun setOnCloseListener(listener: OnCloseListener?) {
mCustomOnCloseListener = listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class SearchBarManager : ViewGroupManager<SearchBarView>() {
"blur" -> root.handleBlurJsRequest()
"clearText" -> root.handleClearTextJsRequest()
"toggleCancelButton" -> root.handleToggleCancelButtonJsRequest(false) // just a dummy argument
"setText" -> root.handleSetTextJsRequest(args?.getString(0))
else -> throw JSApplicationIllegalArgumentException("Unsupported native command received: $commandId")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ class SearchBarView(reactContext: ReactContext?) : ReactViewGroup(reactContext)

fun handleToggleCancelButtonJsRequest(flag: Boolean) = Unit

fun handleSetTextJsRequest(text: String?) {
text?.let { screenStackFragment?.searchView?.setText(it) }
}

enum class SearchBarAutoCapitalize {
NONE, WORDS, SENTENCES, CHARACTERS
}
Expand Down
1 change: 1 addition & 0 deletions guides/GUIDE_FOR_LIBRARY_AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ Allowed imperative actions on search bar are:
- `focus` - Function to focus on search bar.
- `blur` - Function to remove focus from search bar.
- `clearText` - Function to clear text in search bar.
- `setText` - Function to set search bar's text to given value.
- `toggleCancelButton` - Function toggle cancel button display near search bar. (iOS only)

Below is a list of properties that can be set with `ScreenStackHeaderConfig` component:
Expand Down
13 changes: 13 additions & 0 deletions ios/RNSSearchBar.mm
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ - (void)toggleCancelButton:(BOOL)flag
#endif
}

- (void)setText:(NSString *)text
{
[_controller.searchBar setText:text];
}

#pragma mark-- Fabric specific

#ifdef RCT_NEW_ARCH_ENABLED
Expand Down Expand Up @@ -408,6 +413,14 @@ - (UIView *)view
}];
}

RCT_EXPORT_METHOD(setText : (NSNumber *_Nonnull)reactTag text : (NSString *)text)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary *viewRegistry) {
RNSSearchBar *searchBar = viewRegistry[reactTag];
[searchBar setText:text];
}];
}

#endif /* !RCT_NEW_ARCH_ENABLED */

@end
1 change: 1 addition & 0 deletions native-stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ A React ref to imperatively modify search bar. Supported actions:
* `focus` - focus on search bar
* `blur` - remove focus from search bar
* `clearText` - clear text in search bar
* `setText` - set search bar's content to given string
* `toggleCancelButton` (iOS only) - toggle cancel button display near search bar.

### Events
Expand Down
9 changes: 8 additions & 1 deletion src/fabric/SearchBarNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ interface NativeCommands {
viewRef: React.ElementRef<ComponentType>,
flag: boolean
) => void;
setText: (viewRef: React.ElementRef<ComponentType>, text: string) => void;
}

export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['blur', 'focus', 'clearText', 'toggleCancelButton'],
supportedCommands: [
'blur',
'focus',
'clearText',
'toggleCancelButton',
'setText',
],
});

export default codegenNativeComponent<NativeProps>('RNSSearchBar', {});
10 changes: 10 additions & 0 deletions src/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ type SearchBarCommandsType = {
viewRef: React.ElementRef<typeof ScreensNativeModules.NativeSearchBar>,
flag: boolean
) => void;
setText: (
viewRef: React.ElementRef<typeof ScreensNativeModules.NativeSearchBar>,
text: string
) => void;
};

// We initialize these lazily so that importing the module doesn't throw error when not linked
Expand Down Expand Up @@ -451,6 +455,12 @@ class SearchBar extends React.Component<SearchBarProps> {
);
}

setText(text: string) {
this._callMethodWithRef((ref) =>
ScreensNativeModules.NativeSearchBarCommands.setText(ref, text)
);
}

render() {
if (!isSearchBarAvailableForCurrentPlatform) {
console.warn(
Expand Down
2 changes: 2 additions & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type SearchBarCommands = {
blur: () => void;
clearText: () => void;
toggleCancelButton: (show: boolean) => void;
setText: (text: string) => void;
};

export type StackPresentationTypes =
Expand Down Expand Up @@ -526,6 +527,7 @@ export interface SearchBarProps {
* * `focus` - focuses the search bar
* * `blur` - removes focus from the search bar
* * `clearText` - removes any text present in the search bar input field
* * `setText` - sets the search bar's content to given value
* * `toggleCancelButton` - depending on passed boolean value, hides or shows cancel button (iOS only)
*/
ref?: React.RefObject<SearchBarCommands>;
Expand Down

0 comments on commit 9112700

Please sign in to comment.