Skip to content

Commit bcae185

Browse files
committed
feat(rich-text): add onChange for android
1 parent 4e11f1e commit bcae185

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const handleChange = (event: RichTextChangeEvent) => {
7474
1. Setting placeholder (iOS, Android)
7575
2. Select a portion of text and add different styles (bold, italic, underline, strikethrough) (iOS, Android)
7676
3. Add native context menu for formatting (iOS)
77-
4. Add onChange prop (iOS)
77+
4. Add onChange prop (iOS, Android)
7878
5. Return text without markdown (iOS)
7979
6. Add method that returns rich text in RTF (iOS)
8080

android/src/main/java/com/richtextinput/RichTextInputViewManager.kt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,41 @@ import android.text.style.StrikethroughSpan
66
import android.text.style.StyleSpan
77
import android.text.style.UnderlineSpan
88
import android.widget.EditText
9+
import androidx.core.widget.doOnTextChanged
910
import com.facebook.infer.annotation.Assertions
11+
import com.facebook.react.bridge.Arguments
1012
import com.facebook.react.bridge.ReadableArray
1113
import com.facebook.react.uimanager.SimpleViewManager
1214
import com.facebook.react.uimanager.ThemedReactContext
1315
import com.facebook.react.uimanager.annotations.ReactProp
14-
16+
import com.facebook.react.uimanager.events.RCTEventEmitter
1517

1618
class RichTextInputViewManager : SimpleViewManager<EditText>() {
1719
override fun getName() = "RichTextInputView"
1820

21+
override fun getExportedCustomBubblingEventTypeConstants(): Map<String, Any> {
22+
return mapOf(
23+
"topChange" to mapOf(
24+
"phasedRegistrationNames" to mapOf(
25+
"bubbled" to "onChange"
26+
)
27+
)
28+
)
29+
}
30+
1931
override fun createViewInstance(reactContext: ThemedReactContext): EditText {
20-
return EditText(reactContext)
32+
val editText = EditText(reactContext)
33+
34+
editText.doOnTextChanged { text, start, before, count ->
35+
val event = Arguments.createMap().apply {
36+
putString("text", text.toString())
37+
}
38+
reactContext
39+
.getJSModule(RCTEventEmitter::class.java)
40+
.receiveEvent(editText.id, "topChange", event)
41+
}
42+
43+
return editText
2144
}
2245

2346
@ReactProp(name = "placeholder")

0 commit comments

Comments
 (0)