@@ -27,12 +27,14 @@ export const RichTextArea = withShadowStyles(({
2727} :RichTextAreaProps ) => {
2828 const [ caret , setCaret ] = useState ( value . length ) ;
2929 const textareaRef = useRef < HTMLTextAreaElement > ( null ) ;
30+ const isManualUpdateRef = useRef ( false ) ;
3031
3132 useLayoutEffect ( ( ) => {
32- if ( textareaRef . current ) {
33+ if ( textareaRef . current && isManualUpdateRef . current ) {
3334 textareaRef . current . setSelectionRange ( caret , caret ) ;
35+ isManualUpdateRef . current = false ;
3436 }
35- } , [ caret ] ) ;
37+ } , [ caret , value ] ) ;
3638
3739 return (
3840 < div className = { `rich-text-area ${ className || "" } ` } >
@@ -43,8 +45,10 @@ export const RichTextArea = withShadowStyles(({
4345 value = { value }
4446 rows = { singleLine ? 1 : undefined }
4547 onChange = { ( e ) => {
46- onChange ( e . target . value ) ;
47- setCaret ( e . target . selectionStart ?? e . target . value . length ) ;
48+ if ( ! isManualUpdateRef . current ) {
49+ onChange ( e . target . value ) ;
50+ setCaret ( e . target . selectionStart ?? e . target . value . length ) ;
51+ }
4852 } }
4953 onPaste = { ( e ) => {
5054 e . preventDefault ( ) ;
@@ -65,6 +69,8 @@ export const RichTextArea = withShadowStyles(({
6569 const end = input . selectionEnd ?? 0 ;
6670 const newText = value . slice ( 0 , start ) + pastedText + value . slice ( end ) ;
6771
72+ isManualUpdateRef . current = true ;
73+
6874 setCaret ( start + pastedText . length ) ;
6975 onChange ( newText ) ;
7076 } }
@@ -87,10 +93,14 @@ export const RichTextArea = withShadowStyles(({
8793
8894 if ( e . key === "Tab" ) {
8995 e . preventDefault ( ) ;
96+
9097 newText = value . slice ( 0 , start ) + "\t" + value . slice ( end ) ;
9198 newCaret = start + 1 ;
99+ isManualUpdateRef . current = true ;
100+
92101 setCaret ( newCaret ) ;
93102 onChange ( newText ) ;
103+
94104 return ;
95105 }
96106
@@ -103,6 +113,7 @@ export const RichTextArea = withShadowStyles(({
103113
104114 newText = value . slice ( 0 , start ) + "\n" + value . slice ( end ) ;
105115 newCaret = start + 1 ;
116+ isManualUpdateRef . current = true ;
106117
107118 setCaret ( newCaret ) ;
108119 onChange ( newText ) ;
@@ -121,6 +132,8 @@ export const RichTextArea = withShadowStyles(({
121132 newCaret = start - 1 ;
122133 }
123134
135+ isManualUpdateRef . current = true ;
136+
124137 setCaret ( newCaret ) ;
125138 onChange ( newText ) ;
126139
@@ -138,6 +151,8 @@ export const RichTextArea = withShadowStyles(({
138151 newCaret = start ;
139152 }
140153
154+ isManualUpdateRef . current = true ;
155+
141156 setCaret ( newCaret ) ;
142157 onChange ( newText ) ;
143158
@@ -149,6 +164,7 @@ export const RichTextArea = withShadowStyles(({
149164
150165 newText = value . slice ( 0 , start ) + e . key + value . slice ( end ) ;
151166 newCaret = start + 1 ;
167+ isManualUpdateRef . current = true ;
152168
153169 setCaret ( newCaret ) ;
154170 onChange ( newText ) ;
0 commit comments