Skip to content

Commit 96a94ca

Browse files
feat(RichTextEditor): Add external ref support and improve prop handling (#679)
1 parent 319e7bc commit 96a94ca

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

src/components/RichTextEditor.jsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import {
99
import styled from "styled-components/native";
1010
import { space, flexbox, border, layout } from "styled-system";
1111

12-
import { Container } from "@components";
1312
import { useKeyboard } from "@hooks";
1413

14+
import Container from "./Container";
15+
1516
export const ScrollView = styled.ScrollView.attrs(() => ({
1617
keyboardShouldPersistTaps: "handled",
1718
showsVerticalScrollIndicator: true,
@@ -80,13 +81,17 @@ export const RichTextEditor = ({
8081
toolbarActions,
8182
editorProps,
8283
toolBarProps,
84+
richTextRef: externalRichTextRef,
8385
...rest
8486
}) => {
85-
const richTextRef = useRef();
87+
const internalRichTextRef = useRef();
8688
const keyboardHeight = useKeyboard();
8789
const [toolbarVisible, setToolbarVisible] = useState(false);
8890
const showToolbar = keyboardHeight > 0 && toolbarVisible;
8991

92+
// Use the external ref if provided, otherwise use the internal ref
93+
const richTextRef = externalRichTextRef || internalRichTextRef;
94+
9095
const computeToolbarActions = () => {
9196
const actionItems = [];
9297
toolbarActions?.map(actionItem => {
@@ -116,11 +121,11 @@ export const RichTextEditor = ({
116121
onChange={onChange}
117122
onBlur={() => {
118123
setToolbarVisible(false);
119-
editorProps?.onBlurFn();
124+
editorProps?.onBlurFn?.();
120125
}}
121126
onFocus={() => {
122127
setToolbarVisible(true);
123-
editorProps?.onFocusFn();
128+
editorProps?.onFocusFn?.();
124129
}}
125130
{...editorProps}
126131
/>
@@ -160,4 +165,8 @@ RichTextEditor.propTypes = {
160165
toolBarProps: PropTypes.object,
161166
editorWrapperStyle: PropTypes.object,
162167
toolbarWrapperStyle: PropTypes.object,
168+
/**
169+
* Ref object to control the editor from outside the component.
170+
*/
171+
richTextRef: PropTypes.object,
163172
};

0 commit comments

Comments
 (0)