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

Possible Workaround #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 18 additions & 9 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import {
} from 'react-native';

class App extends React.Component {
selection = {
start: 0,
end: 0,
};
constructor(props) {
super(props);

this.state = {
selection: {
start: 0,
end: 0,
},
selection: undefined,
value: '',
};
}
Expand All @@ -36,14 +37,22 @@ class App extends React.Component {
};

onSelectionChange = event => {
this.setState({
selection: event.nativeEvent.selection,
});
const {selection} = event.nativeEvent;
this.selection = selection;
this.setState(
{
selection,
},
() => {
this.setState({
selection: undefined,
});
},
);
};

insert = text => {
const {selection} = this.state;
const {start, end} = selection;
const {start, end} = this.selection;

this.setState(oldState => ({
value: oldState.value.slice(0, start) + text + oldState.value.slice(end),
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# React Native `TextInput` selection issues
# React Native `TextInput` selection <s>issues</s> workaround

<s>
Repro steps:

1. Clone this repo
Expand All @@ -8,5 +9,6 @@ Repro steps:
4. Type in the text box
5. You'll see that the selection is all over the place while typing, causing letters to be inserted in the wrong location
6. You'll see that pressing backspace from the end of the text crashes the app
</s>

You can tap the emojis to insert one at the current cursor position. The emojis are not really relevant to the issue, but just to show something similar to our use case where we have an emoji picker and a mentions library that can insert text and modify the selection.