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

fix(android): Normalize start and end args #24938

Conversation

uqmessias
Copy link
Contributor

Summary

Fixes #18579

Normalize start and end arguments when onSelectionChange is
dispatched on Android.

It just applies a fix sent by @TheSavior (Thanks, by the way 😄)

Changelog

[Android] [Fixed] - fix(android): Normalize start and end args

Test Plan

I followed the steps inside react-native/RNTester/README.md in order to build RN from the source for Android. Opened the RNTester App (before applying the following diff) and testing it manually which the result is the following gif:

Diff used for manually test it

diff --git a/RNTester/js/TextInputExample.android.js b/RNTester/js/TextInputExample.android.js
index edeb83e..f172907 100644
--- a/RNTester/js/TextInputExample.android.js
+++ b/RNTester/js/TextInputExample.android.js
@@ -342,6 +342,50 @@ class SelectionExample extends React.Component<
   }
 }
 
+const testStyles = StyleSheet.create({
+  container: {
+    alignItems: 'center',
+    backgroundColor: '#fff',
+    flex: 1,
+    justifyContent: 'center',
+  },
+  textInput: {
+    marginTop: 100,
+    borderColor: 'black',
+    borderWidth: 1,
+    height: 40,
+    width: 150,
+  },
+});
+class TestAppStyleTI extends React.Component<
+  any,
+  {start?: number, end?: number},
+> {
+  constructor(props: any) {
+    super(props);
+    this.state = {start: 0, end: 0};
+  }
+
+  onSelectionChange = (event: any) => {
+    const selection = event.nativeEvent.selection;
+    this.setState({start: selection.start, end: selection.end});
+  };
+
+  render() {
+    return (
+      <View style={testStyles.container}>
+        <Text>{`Start: ${this.state.start}, End: ${this.state.end}`}</Text>
+        <TextInput
+          style={testStyles.textInput}
+          onSelectionChange={this.onSelectionChange}
+          value="initial text"
+          multiline={true}
+        />
+      </View>
+    );
+  }
+}
+
 class AutogrowingTextInputExample extends React.Component<{}> {
   constructor(props) {
     super(props);
@@ -449,6 +493,12 @@ exports.title = '<TextInput>';
 exports.description = 'Single and multi-line text inputs.';
 exports.examples = [
   {
+    title: 'TestAppStyleTI',
+    render: function() {
+      return <TestAppStyleTI />;
+    },
+  },
+  {
     title: 'Auto-focus',
     render: function() {
       return (

Result

textinput-selection-fixed

Normalize `start` and `end` arguments when `onSelectionChange` is
dispatched on Android.
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 18, 2019
@react-native-bot react-native-bot added Platform: Android Android applications. Bug labels May 18, 2019
Copy link
Contributor

@cpojer cpojer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @uqmessias in 2ad3bb2.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label May 20, 2019
@uqmessias uqmessias deleted the fix/on-selection-change-with-end-less-than-start-issue-18579 branch May 20, 2019 14:26
M-i-k-e-l pushed a commit to M-i-k-e-l/react-native that referenced this pull request Mar 10, 2020
Summary:
Fixes facebook#18579

Normalize `start` and `end` arguments when `onSelectionChange` is
dispatched on Android.

It just applies a [fix](facebook#18579 (comment)) sent by TheSavior (Thanks, by the way 😄)

## Changelog

[Android] [Fixed] - fix(android): Normalize start and end args
Pull Request resolved: facebook#24938

Differential Revision: D15412005

Pulled By: cpojer

fbshipit-source-id: bb132313cfb8877a682f3865a5f9e48d45ac20ac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Platform: Android Android applications.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android][TextInput] onSelectionChange can have end < start
4 participants