Skip to content

Commit e79513a

Browse files
committed
add systemDefault to autoCapitalize
1 parent 47e45a6 commit e79513a

File tree

7 files changed

+64
-15
lines changed

7 files changed

+64
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class SearchBarManager :
4444
) {
4545
view.autoCapitalize =
4646
when (autoCapitalize) {
47-
null, "none" -> SearchBarView.SearchBarAutoCapitalize.NONE
47+
null, "systemDefault", "none" -> SearchBarView.SearchBarAutoCapitalize.NONE
4848
"words" -> SearchBarView.SearchBarAutoCapitalize.WORDS
4949
"sentences" -> SearchBarView.SearchBarAutoCapitalize.SENTENCES
5050
"characters" -> SearchBarView.SearchBarAutoCapitalize.CHARACTERS

guides/GUIDE_FOR_LIBRARY_AUTHORS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ Along with this component's properties that can be used to customize header beha
455455

456456
To render a search bar use `ScreenStackHeaderSearchBarView` with `<SearchBar>` component provided as a child. `<SearchBar>` component that comes from react-native-screens supports various properties:
457457

458-
- `autoCapitalize` - Controls whether the text is automatically auto-capitalized as it is entered by the user. Can be one of these: `none`, `words`, `sentences`, `characters`. Defaults to `sentences` on iOS and `'none'` on Android.
458+
- `autoCapitalize` - Controls whether the text is automatically auto-capitalized as it is entered by the user. Can be one of these: `systemDefault`, `none`, `words`, `sentences`, `characters`. Defaults to `systemDefault` which is the same as `sentences` on iOS and `none` on Android.
459459
- `autoFocus` - If `true` automatically focuses search bar when screen is appearing. (Android only)
460460
- `barTintColor` - The search field background color. By default bar tint color is translucent.
461461
- `tintColor` - The color for the cursor caret and cancel button text. (iOS only)

ios/RNSConvert.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ + (UITextAutocapitalizationType)UITextAutocapitalizationTypeFromCppEquivalent:
203203
using enum react::RNSSearchBarAutoCapitalize;
204204
case Words:
205205
return UITextAutocapitalizationTypeWords;
206+
case SystemDefault:
206207
case Sentences:
207208
return UITextAutocapitalizationTypeSentences;
208209
case Characters:

ios/RNSSearchBar.mm

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,19 @@ - (UIView *)view
491491
RCT_EXPORT_VIEW_PROPERTY(obscureBackground, RNSOptionalBoolean)
492492
RCT_EXPORT_VIEW_PROPERTY(hideNavigationBar, RNSOptionalBoolean)
493493
RCT_EXPORT_VIEW_PROPERTY(hideWhenScrolling, BOOL)
494-
RCT_EXPORT_VIEW_PROPERTY(autoCapitalize, UITextAutocapitalizationType)
494+
495+
// We want to use "systemDefault" option which is not in UITextAutocapitalizationType
496+
// but RCTConvert enum conversion already exists.
497+
RCT_CUSTOM_VIEW_PROPERTY(autoCapitalize, UITextAutocapitalizationType, RNSSearchBar)
498+
{
499+
RNSSearchBar *searchBarView = static_cast<RNSSearchBar *>(view);
500+
if ([json isKindOfClass:[NSString class]] && [static_cast<NSString *>(json) isEqualToString:@"systemDefault"]) {
501+
[searchBarView setAutoCapitalize:UITextAutocapitalizationTypeSentences];
502+
} else {
503+
[searchBarView setAutoCapitalize:[RCTConvert UITextAutocapitalizationType:json]];
504+
}
505+
}
506+
495507
RCT_EXPORT_VIEW_PROPERTY(placeholder, NSString)
496508
RCT_EXPORT_VIEW_PROPERTY(barTintColor, UIColor)
497509
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)

src/components/SearchBar.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,49 @@ function SearchBar(
8888
return View as unknown as React.ReactNode;
8989
}
9090

91+
// This is necessary only for legacy architecture (Paper).
92+
const parsedProps = parseUndefinedPropsToSystemDefault(props);
93+
94+
const {
95+
obscureBackground,
96+
hideNavigationBar,
97+
onFocus,
98+
onBlur,
99+
onSearchButtonPress,
100+
onCancelButtonPress,
101+
onChangeText,
102+
...rest
103+
} = parsedProps;
104+
91105
return (
92106
<NativeSearchBar
93107
ref={searchBarRef}
94-
{...props}
108+
{...rest}
95109
obscureBackground={parseBooleanToOptionalBooleanNativeProp(
96-
props.obscureBackground,
110+
obscureBackground,
97111
)}
98112
hideNavigationBar={parseBooleanToOptionalBooleanNativeProp(
99-
props.hideNavigationBar,
113+
hideNavigationBar,
100114
)}
101-
onSearchFocus={props.onFocus as DirectEventHandler<SearchBarEvent>}
102-
onSearchBlur={props.onBlur as DirectEventHandler<SearchBarEvent>}
115+
onSearchFocus={onFocus as DirectEventHandler<SearchBarEvent>}
116+
onSearchBlur={onBlur as DirectEventHandler<SearchBarEvent>}
103117
onSearchButtonPress={
104-
props.onSearchButtonPress as DirectEventHandler<SearchButtonPressedEvent>
118+
onSearchButtonPress as DirectEventHandler<SearchButtonPressedEvent>
105119
}
106120
onCancelButtonPress={
107-
props.onCancelButtonPress as DirectEventHandler<SearchBarEvent>
121+
onCancelButtonPress as DirectEventHandler<SearchBarEvent>
108122
}
109-
onChangeText={props.onChangeText as DirectEventHandler<ChangeTextEvent>}
123+
onChangeText={onChangeText as DirectEventHandler<ChangeTextEvent>}
110124
/>
111125
);
112126
}
113127

128+
// This function is necessary for legacy architecture (Paper) to ensure
129+
// consistent behavior for props with `systemDefault` option.
130+
function parseUndefinedPropsToSystemDefault(
131+
props: SearchBarProps,
132+
): SearchBarProps {
133+
return { ...props, autoCapitalize: props.autoCapitalize ?? 'systemDefault' };
134+
}
135+
114136
export default React.forwardRef<SearchBarCommands, SearchBarProps>(SearchBar);

src/fabric/SearchBarNativeComponent.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ type SearchBarPlacement =
2727
| 'integratedButton'
2828
| 'integratedCentered';
2929

30-
type AutoCapitalizeType = 'none' | 'words' | 'sentences' | 'characters';
30+
type AutoCapitalizeType =
31+
| 'systemDefault'
32+
| 'none'
33+
| 'words'
34+
| 'sentences'
35+
| 'characters';
3136

3237
type OptionalBoolean = 'undefined' | 'false' | 'true';
3338

@@ -38,7 +43,7 @@ export interface NativeProps extends ViewProps {
3843
onCancelButtonPress?: DirectEventHandler<SearchBarEvent> | null;
3944
onChangeText?: DirectEventHandler<ChangeTextEvent> | null;
4045
hideWhenScrolling?: WithDefault<boolean, true>;
41-
autoCapitalize?: WithDefault<AutoCapitalizeType, 'none'>;
46+
autoCapitalize?: WithDefault<AutoCapitalizeType, 'systemDefault'>;
4247
placeholder?: string;
4348
placement?: WithDefault<SearchBarPlacement, 'automatic'>;
4449
allowToolbarIntegration?: WithDefault<boolean, true>;

src/types.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,9 +746,18 @@ export interface SearchBarProps {
746746
ref?: React.RefObject<SearchBarCommands>;
747747

748748
/**
749-
* The auto-capitalization behavior
749+
* The auto-capitalization behavior.
750+
*
751+
* Defaults to `systemDefault`:
752+
* - on Android, it is the same as `none`,
753+
* - on iOS, it is the same as `sentences`.
750754
*/
751-
autoCapitalize?: 'none' | 'words' | 'sentences' | 'characters';
755+
autoCapitalize?:
756+
| 'systemDefault'
757+
| 'none'
758+
| 'words'
759+
| 'sentences'
760+
| 'characters';
752761
/**
753762
* Automatically focuses search bar on mount
754763
*

0 commit comments

Comments
 (0)