Skip to content

Commit 66669c6

Browse files
authored
feat: android - allow setting up value imperatively (#42)
1 parent 15ab759 commit 66669c6

6 files changed

Lines changed: 21 additions & 6 deletions

File tree

android/src/main/java/com/swmansion/reactnativerichtexteditor/ReactNativeRichTextEditorView.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ReactNativeRichTextEditorView : AppCompatEditText {
4444
val paragraphStyles: ParagraphStyles? = ParagraphStyles(this)
4545
val listStyles: ListStyles? = ListStyles(this)
4646
val parametrizedStyles: ParametrizedStyles? = ParametrizedStyles(this)
47-
var isSettingDefaultValue: Boolean = false
47+
var isSettingValue: Boolean = false
4848

4949
var linkHandler: LinkHandler? = LinkHandler(this)
5050
val mentionHandler: MentionHandler? = MentionHandler(this)
@@ -144,9 +144,9 @@ class ReactNativeRichTextEditorView : AppCompatEditText {
144144
setSelection(selection?.start ?: text?.length ?: 0)
145145
}
146146

147-
fun setDefaultValue(value: String?) {
147+
fun setValue(value: String?) {
148148
if (value == null) return
149-
isSettingDefaultValue = true
149+
isSettingValue = true
150150

151151
val isHtml = value.startsWith("<html>") && value.endsWith("</html>")
152152
if (isHtml) {
@@ -162,7 +162,7 @@ class ReactNativeRichTextEditorView : AppCompatEditText {
162162
// Scroll to the last line of text
163163
setSelection(text?.length ?: 0)
164164

165-
isSettingDefaultValue = false
165+
isSettingValue = false
166166
}
167167

168168
fun setAutoFocus(autoFocus: Boolean) {

android/src/main/java/com/swmansion/reactnativerichtexteditor/ReactNativeRichTextEditorViewManager.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class ReactNativeRichTextEditorViewManager : SimpleViewManager<ReactNativeRichTe
7474

7575
@ReactProp(name = "defaultValue")
7676
override fun setDefaultValue(view: ReactNativeRichTextEditorView?, value: String?) {
77-
view?.setDefaultValue(value)
77+
view?.setValue(value)
7878
}
7979

8080
@ReactProp(name = "placeholder")
@@ -171,6 +171,10 @@ class ReactNativeRichTextEditorViewManager : SimpleViewManager<ReactNativeRichTe
171171
view?.clearFocus()
172172
}
173173

174+
override fun setValue(view: ReactNativeRichTextEditorView?, text: String) {
175+
view?.setValue(text)
176+
}
177+
174178
override fun toggleBold(view: ReactNativeRichTextEditorView?) {
175179
view?.verifyAndToggleStyle(EditorSpans.BOLD)
176180
}

android/src/main/java/com/swmansion/reactnativerichtexteditor/watchers/EditorTextWatcher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class EditorTextWatcher(private val editorView: ReactNativeRichTextEditorView) :
2222
}
2323

2424
override fun afterTextChanged(s: Editable?) {
25-
if (s == null || editorView.isSettingDefaultValue) return
25+
if (s == null || editorView.isSettingValue) return
2626

2727
emitEvent(s)
2828
applyStyles(s)

example/src/App.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ export default function App() {
101101
ref.current?.blur();
102102
};
103103

104+
const handleSetValue = () => {
105+
ref.current?.setValue('<html><b>Hello</b> <i>world</i></html>');
106+
};
107+
104108
const openLinkModal = () => {
105109
setIsLinkModalOpen(true);
106110
};
@@ -196,6 +200,7 @@ export default function App() {
196200
/>
197201
<Button title="Focus" onPress={handleFocus} />
198202
<Button title="Blur" onPress={handleBlur} />
203+
<Button title="Set value" onPress={handleSetValue} />
199204
{DEBUG_SCROLLABLE && <View style={styles.scrollPlaceholder} />}
200205
</ScrollView>
201206
<LinkModal

src/ReactNativeRichTextEditorViewNativeComponent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ interface NativeCommands {
8787
// General commands
8888
focus: (viewRef: React.ElementRef<ComponentType>) => void;
8989
blur: (viewRef: React.ElementRef<ComponentType>) => void;
90+
setValue: (viewRef: React.ElementRef<ComponentType>, text: string) => void;
9091

9192
// Text formatting commands
9293
toggleBold: (viewRef: React.ElementRef<ComponentType>) => void;
@@ -124,6 +125,7 @@ export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
124125
// General commands
125126
'focus',
126127
'blur',
128+
'setValue',
127129

128130
// Text formatting commands
129131
'toggleBold',

src/RichTextInput.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface RichTextInputInstance extends NativeMethods {
3232
// General commands
3333
focus: () => void;
3434
blur: () => void;
35+
setValue: (value: string) => void;
3536

3637
// Text formatting commands
3738
toggleBold: () => void;
@@ -146,6 +147,9 @@ export const RichTextInput = ({
146147
blur: () => {
147148
Commands.blur(nullthrows(nativeRef.current));
148149
},
150+
setValue: (value: string) => {
151+
Commands.setValue(nullthrows(nativeRef.current), value);
152+
},
149153
toggleBold: () => {
150154
Commands.toggleBold(nullthrows(nativeRef.current));
151155
},

0 commit comments

Comments
 (0)