1
1
import classNames from 'classnames' ;
2
2
import { BaseInput } from 'rc-input' ;
3
+ import type { HolderRef } from 'rc-input/lib/BaseInput' ;
3
4
import type { CommonInputProps } from 'rc-input/lib/interface' ;
4
5
import type { TextAreaProps , TextAreaRef } from 'rc-textarea' ;
5
6
import TextArea from 'rc-textarea' ;
6
7
import toArray from 'rc-util/lib/Children/toArray' ;
7
8
import useMergedState from 'rc-util/lib/hooks/useMergedState' ;
8
9
import KeyCode from 'rc-util/lib/KeyCode' ;
9
10
import warning from 'rc-util/lib/warning' ;
10
- import React , { forwardRef , useEffect , useMemo , useRef , useState } from 'react' ;
11
+ import React , {
12
+ forwardRef ,
13
+ useEffect ,
14
+ useImperativeHandle ,
15
+ useMemo ,
16
+ useRef ,
17
+ useState ,
18
+ } from 'react' ;
11
19
import useEffectState from './hooks/useEffectState' ;
12
20
import KeywordTrigger from './KeywordTrigger' ;
13
21
import MentionsContext from './MentionsContext' ;
@@ -71,6 +79,8 @@ export interface MentionsRef {
71
79
72
80
/** @deprecated It may not work as expected */
73
81
textarea : HTMLTextAreaElement | null ;
82
+
83
+ nativeElement : HTMLElement ;
74
84
}
75
85
76
86
const InternalMentions = forwardRef < MentionsRef , MentionsProps > (
@@ -124,6 +134,7 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
124
134
) ;
125
135
126
136
// =============================== Refs ===============================
137
+ const containerRef = useRef < HTMLDivElement > ( null ) ;
127
138
const textareaRef = useRef < TextAreaRef > ( null ) ;
128
139
const measureRef = useRef < HTMLDivElement > ( null ) ;
129
140
@@ -133,6 +144,7 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
133
144
focus : ( ) => textareaRef . current ?. focus ( ) ,
134
145
blur : ( ) => textareaRef . current ?. blur ( ) ,
135
146
textarea : textareaRef . current ?. resizableTextArea ?. textArea ,
147
+ nativeElement : containerRef . current ,
136
148
} ) ) ;
137
149
138
150
// ============================== State ===============================
@@ -430,7 +442,11 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
430
442
431
443
// ============================== Render ==============================
432
444
return (
433
- < div className = { classNames ( prefixCls , className ) } style = { style } >
445
+ < div
446
+ className = { classNames ( prefixCls , className ) }
447
+ style = { style }
448
+ ref = { containerRef }
449
+ >
434
450
< TextArea
435
451
ref = { textareaRef }
436
452
value = { mergedValue }
@@ -495,6 +511,16 @@ const Mentions = forwardRef<MentionsRef, MentionsProps>(
495
511
} ,
496
512
ref ,
497
513
) => {
514
+ // =============================== Ref ================================
515
+ const holderRef = useRef < HolderRef > ( null ) ;
516
+ const mentionRef = useRef < MentionsRef > ( null ) ;
517
+
518
+ useImperativeHandle ( ref , ( ) => ( {
519
+ ...mentionRef . current ,
520
+ nativeElement :
521
+ holderRef . current ?. nativeElement || mentionRef . current ?. nativeElement ,
522
+ } ) ) ;
523
+
498
524
// ============================== Value ===============================
499
525
const [ mergedValue , setMergedValue ] = useMergedState ( '' , {
500
526
defaultValue,
@@ -522,11 +548,12 @@ const Mentions = forwardRef<MentionsRef, MentionsProps>(
522
548
className = { className }
523
549
classNames = { classes }
524
550
disabled = { disabled }
551
+ ref = { holderRef }
525
552
>
526
553
< InternalMentions
527
554
className = { classes ?. mentions }
528
555
prefixCls = { prefixCls }
529
- ref = { ref }
556
+ ref = { mentionRef }
530
557
onChange = { triggerChange }
531
558
{ ...rest }
532
559
/>
0 commit comments