yang24201
yang24201

Reputation: 151

How to get the cursor position in TextInput and the content after the cursor position?

I want to write a component which can edit an article for words input and insert images. I use TextInput for words input. If I want to insert an image in the middle of the text, I will generate an Image component and an new TextInput after the Image. But I don't know how to get the cursor position and content after the cursor. Anyone help?

Upvotes: 5

Views: 1910

Answers (2)

MDH
MDH

Reputation: 132

The answer by @Noitidart points to the right direction

<TextInput> has onSelectionChange which fires with { nativeEvent:{ start:number, end:number } }. It updates whenever cursor position changes.

However, it's useful to note that as of RN 0.61, onSelectionChange fires with { nativeEvent: { selection: { start:number, end:number } }, text: string }.

Upvotes: 1

Noitidart
Noitidart

Reputation: 37238

<TextInput> has onSelectionChange which fires with { nativeEvent:{ start:number, end:number } }. It updates whenever cursor position changes.

Upvotes: 3

Related Questions