Skip to content

Commit 133cf7a

Browse files
authored
Merge pull request #158 from achubai/master
fix: ios on selectionChange causes cursor to move to initial position
2 parents 4499796 + 151dad5 commit 133cf7a

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/CNTextInput.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const IS_IOS = Platform.OS == 'ios';
1313
class CNTextInput extends Component {
1414
constructor(props) {
1515
super(props);
16-
this.textInput = null;
16+
this.textInput = React.createRef();
1717
this.prevSelection = { start: 0, end: 0 };
1818
this.beforePrevSelection = { start: 0, end: 0 };
1919
this.avoidSelectionChangeOnFocus = false;
@@ -42,7 +42,7 @@ class CNTextInput extends Component {
4242
DiffMatchPatch.DIFF_EQUAL = 0;
4343
}
4444

45-
componentWillMount() {
45+
UNSAFE_componentWillMount() {
4646
const { items } = this.props;
4747
if(items && Array.isArray(items) === true) {
4848
let content = items;
@@ -52,7 +52,7 @@ class CNTextInput extends Component {
5252
if (this.props.onContentChanged) {
5353
this.props.onContentChanged(content);
5454
}
55-
}
55+
}
5656
}
5757

5858
componentDidMount() {
@@ -128,7 +128,7 @@ class CNTextInput extends Component {
128128
}
129129
} else if (itemNo > 0) {
130130
const foundElement = content[index];
131-
beforeContent = {
131+
let beforeContent = {
132132
id: foundElement.id,
133133
len: itemNo,
134134
stype: foundElement.stype,
@@ -137,7 +137,7 @@ class CNTextInput extends Component {
137137
text: foundElement.text.substring(0, itemNo),
138138
};
139139

140-
afterContent = {
140+
let afterContent = {
141141
id: shortid.generate(),
142142
len: foundElement.len - itemNo,
143143
stype: foundElement.stype,
@@ -216,10 +216,14 @@ class CNTextInput extends Component {
216216
this.avoidAndroidJump = false;
217217

218218
if (selection.end >= selection.start) {
219+
this.textInput.current.setNativeProps({ selection });
219220
this.setState({
220221
selection,
221222
});
222223
} else {
224+
this.textInput.current.setNativeProps({
225+
selection: { start: selection.end, end: selection.start },
226+
});
223227
this.setState({
224228
selection: { start: selection.end, end: selection.start },
225229
});
@@ -727,7 +731,7 @@ class CNTextInput extends Component {
727731
}
728732
}
729733
} else {
730-
beforeContent = {
734+
let beforeContent = {
731735
id: foundElement.id,
732736
len: itemNo,
733737
stype: foundElement.stype,
@@ -737,7 +741,7 @@ class CNTextInput extends Component {
737741
NewLine: foundElement.text.substring(0, itemNo).indexOf('\n') === 0 || index === 0,
738742
};
739743

740-
afterContent = {
744+
let afterContent = {
741745
id: shortid.generate(),
742746
len: foundElement.len - itemNo,
743747
text: foundElement.text.substring(itemNo, foundElement.len),
@@ -1311,7 +1315,7 @@ class CNTextInput extends Component {
13111315
const { selection } = this.state;
13121316
const color = foreColor || '#000';
13131317
const fontSize =styleList && styleList.body && styleList.body.fontSize ? styleList.body.fontSize : 20;
1314-
1318+
13151319
return (
13161320
<TextInput
13171321
{...textInputProps}
@@ -1330,10 +1334,9 @@ class CNTextInput extends Component {
13301334
scrollEnabled={false}
13311335
returnKeyType={returnKeyType || 'next'}
13321336
keyboardType="default"
1333-
ref={component => this.textInput = component}
1337+
ref={this.textInput}
13341338
onChangeText={this.handleChangeText}
13351339
onKeyPress={this.handleKeyDown}
1336-
selection={selection}
13371340
onFocus={this.onFocus}
13381341
onBlur={this.onBlur}
13391342
onContentSizeChange={this.handleContentSizeChange}
@@ -1405,6 +1408,7 @@ class CNTextInput extends Component {
14051408
this.textInput.focus();
14061409

14071410
if (selection != null && selection.start && selection.end) {
1411+
this.textInput.current.setNativeProps({ selection });
14081412
setTimeout(() => {
14091413
this.setState({
14101414
selection,

0 commit comments

Comments
 (0)