Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IOS] Add smartInsertDelete prop to TextInput component #40

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Libraries/Components/TextInput/RCTTextInputViewConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const RCTTextInputViewConfig = {
showSoftInputOnFocus: true,
autoFocus: true,
lineBreakStrategyIOS: true,
smartInsertDelete: true,
...ConditionallyIgnoredEventHandlers({
onChange: true,
onSelectionChange: true,
Expand Down
8 changes: 8 additions & 0 deletions Libraries/Components/TextInput/TextInput.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ export interface TextInputIOSProps {
* If false, scrolling of the text view will be disabled. The default value is true. Only works with multiline={true}
*/
scrollEnabled?: boolean | undefined;

/**
* If `false`, the iOS system will not insert an extra space after a paste operation
* neither delete one or two spaces after a cut or delete operation.
*
* The default value is `true`.
*/
smartInsertDelete?: boolean | undefined;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions Libraries/Components/TextInput/TextInput.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ type IOSProps = $ReadOnly<{|
* @platform ios
*/
lineBreakStrategyIOS?: ?('none' | 'standard' | 'hangul-word' | 'push-out'),

/**
* If `false`, the iOS system will not insert an extra space after a paste operation
* neither delete one or two spaces after a cut or delete operation.
*
* The default value is `true`.
*
* @platform ios
*/
smartInsertDelete?: ?boolean,
|}>;

type AndroidProps = $ReadOnly<{|
Expand Down
10 changes: 10 additions & 0 deletions Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,16 @@ type IOSProps = $ReadOnly<{|
* @platform ios
*/
lineBreakStrategyIOS?: ?('none' | 'standard' | 'hangul-word' | 'push-out'),

/**
* If `false`, the iOS system will not insert an extra space after a paste operation
* neither delete one or two spaces after a cut or delete operation.
*
* The default value is `true`.
*
* @platform ios
*/
smartInsertDelete?: ?boolean,
|}>;

type AndroidProps = $ReadOnly<{|
Expand Down
1 change: 1 addition & 0 deletions Libraries/Text/RCTConvert+Text.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (UITextAutocorrectionType)UITextAutocorrectionType:(nullable id)json;
+ (UITextSpellCheckingType)UITextSpellCheckingType:(nullable id)json;
+ (RCTTextTransform)RCTTextTransform:(nullable id)json;
+ (UITextSmartInsertDeleteType)UITextSmartInsertDeleteType:(nullable id)json;

@end

Expand Down
7 changes: 7 additions & 0 deletions Libraries/Text/RCTConvert+Text.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ + (UITextSpellCheckingType)UITextSpellCheckingType:(id)json
RCTTextTransformUndefined,
integerValue)

+ (UITextSmartInsertDeleteType)UITextSmartInsertDeleteType:(id)json
{
return json == nil ? UITextSmartInsertDeleteTypeDefault
: [RCTConvert BOOL:json] ? UITextSmartInsertDeleteTypeYes
: UITextSmartInsertDeleteTypeNo;
}

@end
1 change: 1 addition & 0 deletions Libraries/Text/TextInput/RCTBaseTextInputViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ @implementation RCTBaseTextInputViewManager {
RCT_REMAP_VIEW_PROPERTY(clearButtonMode, backedTextInputView.clearButtonMode, UITextFieldViewMode)
RCT_REMAP_VIEW_PROPERTY(scrollEnabled, backedTextInputView.scrollEnabled, BOOL)
RCT_REMAP_VIEW_PROPERTY(secureTextEntry, backedTextInputView.secureTextEntry, BOOL)
RCT_REMAP_VIEW_PROPERTY(smartInsertDelete, backedTextInputView.smartInsertDeleteType, UITextSmartInsertDeleteType)
RCT_EXPORT_VIEW_PROPERTY(autoFocus, BOOL)
RCT_EXPORT_VIEW_PROPERTY(submitBehavior, NSString)
RCT_EXPORT_VIEW_PROPERTY(clearTextOnFocus, BOOL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
}
}

/*
* When updating component's props, we compare if the new value is different from the old one.
* If it is different, we update the native view with the new value.
*
* `RCTUITextSmartInsertDeleteTypeFromOptionalBool` is used to convert the boolean value coming
* from JS side to the appropriate `UITextSmartInsertDeleteType` value, that is required for
* this `smartInsertDeleteType` attribute.
*/
if (newTextInputProps.traits.smartInsertDelete != oldTextInputProps.traits.smartInsertDelete) {
mountiny marked this conversation as resolved.
Show resolved Hide resolved
if (@available(iOS 11.0, *)) {
_backedTextInputView.smartInsertDeleteType =
RCTUITextSmartInsertDeleteTypeFromOptionalBool(newTextInputProps.traits.smartInsertDelete);
}
}

// Traits `blurOnSubmit`, `clearTextOnFocus`, and `selectTextOnFocus` were omitted intentially here
// because they are being checked on-demand.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ UITextContentType RCTUITextContentTypeFromString(std::string const &contentType)
API_AVAILABLE(ios(12.0))
UITextInputPasswordRules *RCTUITextInputPasswordRulesFromString(std::string const &passwordRules);

API_AVAILABLE(ios(11.0))
mountiny marked this conversation as resolved.
Show resolved Hide resolved
UITextSmartInsertDeleteType RCTUITextSmartInsertDeleteTypeFromOptionalBool(std::optional<bool> smartInsertDelete);

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ void RCTCopyBackedTextInput(
toTextInput.keyboardType = fromTextInput.keyboardType;
toTextInput.textContentType = fromTextInput.textContentType;

if (@available(iOS 11.0, *)) {
toTextInput.smartInsertDeleteType = fromTextInput.smartInsertDeleteType;
}

if (@available(iOS 12.0, *)) {
toTextInput.passwordRules = fromTextInput.passwordRules;
}
Expand Down Expand Up @@ -232,3 +236,10 @@ UITextContentType RCTUITextContentTypeFromString(std::string const &contentType)
{
return [UITextInputPasswordRules passwordRulesWithDescriptor:RCTNSStringFromStringNilIfEmpty(passwordRules)];
}

API_AVAILABLE(ios(11.0))
UITextSmartInsertDeleteType RCTUITextSmartInsertDeleteTypeFromOptionalBool(std::optional<bool> smartInsertDelete)
{
return smartInsertDelete.has_value() ? (*smartInsertDelete ? UITextSmartInsertDeleteTypeYes : UITextSmartInsertDeleteTypeNo)
: UITextSmartInsertDeleteTypeDefault;
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ class TextInputTraits final {
* Default value: `<empty string>` (no rules).
*/
std::string passwordRules{};

/*
* If `false`, the iOS system will not insert an extra space after a paste operation
* neither delete one or two spaces after a cut or delete operation.
* iOS-only (inherently iOS-specific)
* Can be empty (`null` in JavaScript) which means `default`.
* Default value: `empty` (`null`).
*/
std::optional<bool> smartInsertDelete{};
};

} // namespace react
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ static TextInputTraits convertRawProp(
"passwordRules",
sourceTraits.passwordRules,
defaultTraits.passwordRules);
traits.smartInsertDelete = convertRawProp(
context,
rawProps,
"smartInsertDelete",
sourceTraits.smartInsertDelete,
defaultTraits.smartInsertDelete);

return traits;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/rn-tester/js/examples/TextInput/TextInputExample.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,4 +901,23 @@ exports.examples = ([
);
},
},
{
title: 'iOS autoformatting behaviors',
render: function (): React.Node {
return (
<View>
<WithLabel label="smartInsertDelete: true | undefined">
<TextInput style={styles.default} defaultValue="CopyAndPaste" />
</WithLabel>
<WithLabel label="smartInsertDelete: false">
<TextInput
smartInsertDelete={false}
style={styles.default}
defaultValue="CopyAndPaste"
/>
</WithLabel>
</View>
);
},
},
]: Array<RNTesterModuleExample>);