Skip to content

Commit 3ce983d

Browse files
authored
feat: support nativeElement (#246)
* feat: support nativeElement * chore: ts fix
1 parent d00ba9e commit 3ce983d

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@babel/runtime": "^7.22.5",
4949
"@rc-component/trigger": "^2.0.0",
5050
"classnames": "^2.2.6",
51-
"rc-input": "~1.4.0",
51+
"rc-input": "~1.5.0",
5252
"rc-menu": "~9.14.0",
5353
"rc-textarea": "~1.6.1",
5454
"rc-util": "^5.34.1"

src/Mentions.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import classNames from 'classnames';
22
import { BaseInput } from 'rc-input';
3+
import type { HolderRef } from 'rc-input/lib/BaseInput';
34
import type { CommonInputProps } from 'rc-input/lib/interface';
45
import type { TextAreaProps, TextAreaRef } from 'rc-textarea';
56
import TextArea from 'rc-textarea';
67
import toArray from 'rc-util/lib/Children/toArray';
78
import useMergedState from 'rc-util/lib/hooks/useMergedState';
89
import KeyCode from 'rc-util/lib/KeyCode';
910
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';
1119
import useEffectState from './hooks/useEffectState';
1220
import KeywordTrigger from './KeywordTrigger';
1321
import MentionsContext from './MentionsContext';
@@ -71,6 +79,8 @@ export interface MentionsRef {
7179

7280
/** @deprecated It may not work as expected */
7381
textarea: HTMLTextAreaElement | null;
82+
83+
nativeElement: HTMLElement;
7484
}
7585

7686
const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
@@ -124,6 +134,7 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
124134
);
125135

126136
// =============================== Refs ===============================
137+
const containerRef = useRef<HTMLDivElement>(null);
127138
const textareaRef = useRef<TextAreaRef>(null);
128139
const measureRef = useRef<HTMLDivElement>(null);
129140

@@ -133,6 +144,7 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
133144
focus: () => textareaRef.current?.focus(),
134145
blur: () => textareaRef.current?.blur(),
135146
textarea: textareaRef.current?.resizableTextArea?.textArea,
147+
nativeElement: containerRef.current,
136148
}));
137149

138150
// ============================== State ===============================
@@ -430,7 +442,11 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
430442

431443
// ============================== Render ==============================
432444
return (
433-
<div className={classNames(prefixCls, className)} style={style}>
445+
<div
446+
className={classNames(prefixCls, className)}
447+
style={style}
448+
ref={containerRef}
449+
>
434450
<TextArea
435451
ref={textareaRef}
436452
value={mergedValue}
@@ -495,6 +511,16 @@ const Mentions = forwardRef<MentionsRef, MentionsProps>(
495511
},
496512
ref,
497513
) => {
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+
498524
// ============================== Value ===============================
499525
const [mergedValue, setMergedValue] = useMergedState('', {
500526
defaultValue,
@@ -522,11 +548,12 @@ const Mentions = forwardRef<MentionsRef, MentionsProps>(
522548
className={className}
523549
classNames={classes}
524550
disabled={disabled}
551+
ref={holderRef}
525552
>
526553
<InternalMentions
527554
className={classes?.mentions}
528555
prefixCls={prefixCls}
529-
ref={ref}
556+
ref={mentionRef}
530557
onChange={triggerChange}
531558
{...rest}
532559
/>

tests/Mentions.spec.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,24 @@ describe('Mentions', () => {
281281
);
282282
expect(container.firstChild).toHaveClass('rc-mentions-disabled');
283283
});
284+
285+
describe('nativeElement', () => {
286+
it('work', () => {
287+
const ref = createRef<MentionsRef>();
288+
const { container } = render(createMentions({ ref }));
289+
290+
expect(ref.current.nativeElement).toBe(
291+
container.querySelector('.rc-mentions'),
292+
);
293+
});
294+
295+
it('wrap ref', () => {
296+
const ref = createRef<MentionsRef>();
297+
const { container } = render(createMentions({ ref, allowClear: true }));
298+
299+
expect(ref.current.nativeElement).toBe(
300+
container.querySelector('.rc-mentions-affix-wrapper'),
301+
);
302+
});
303+
});
284304
});

0 commit comments

Comments
 (0)