Skip to content

Commit 3fa1962

Browse files
authored
feat: support nativeElement (#58)
1 parent 9151ed1 commit 3fa1962

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
"dependencies": {
4646
"@babel/runtime": "^7.10.1",
4747
"classnames": "^2.2.1",
48-
"rc-input": "~1.4.0",
48+
"rc-input": "~1.5.0",
4949
"rc-resize-observer": "^1.0.0",
5050
"rc-util": "^5.27.0"
5151
},
5252
"devDependencies": {
5353
"@rc-component/father-plugin": "^1.0.0",
5454
"@testing-library/jest-dom": "^5.16.5",
55-
"@testing-library/react": "^13.3.0",
55+
"@testing-library/react": "^15.0.0",
5656
"@types/classnames": "^2.2.9",
5757
"@types/react": "^18.0.0",
5858
"@types/react-dom": "^18.0.0",

src/TextArea.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import clsx from 'classnames';
22
import { BaseInput } from 'rc-input';
3+
import { type HolderRef } from 'rc-input/lib/BaseInput';
34
import useCount from 'rc-input/lib/hooks/useCount';
45
import { resolveOnChange } from 'rc-input/lib/utils/commonUtils';
56
import useMergedState from 'rc-util/lib/hooks/useMergedState';
@@ -54,6 +55,7 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>(
5455
const [textareaResized, setTextareaResized] = React.useState<boolean>(null);
5556

5657
// =============================== Ref ================================
58+
const holderRef = useRef<HolderRef>(null);
5759
const resizableTextAreaRef = useRef<ResizableTextAreaRef>(null);
5860
const getTextArea = () => resizableTextAreaRef.current?.textArea;
5961

@@ -67,6 +69,7 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>(
6769
blur: () => {
6870
getTextArea().blur();
6971
},
72+
nativeElement: holderRef.current?.nativeElement || getTextArea(),
7073
}));
7174

7275
useEffect(() => {
@@ -208,6 +211,7 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>(
208211

209212
return (
210213
<BaseInput
214+
ref={holderRef}
211215
value={formatValue}
212216
allowClear={allowClear}
213217
handleReset={handleReset}

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ export type TextAreaRef = {
4242
resizableTextArea: ResizableTextAreaRef;
4343
focus: () => void;
4444
blur: () => void;
45+
nativeElement: HTMLElement;
4546
};

tests/index.spec.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fireEvent, render } from '@testing-library/react';
22
import React from 'react';
3-
import type { TextAreaProps } from '../src';
3+
import type { TextAreaProps, TextAreaRef } from '../src';
44
import TextArea from '../src';
55
import calculateAutoSizeStyle, {
66
calculateNodeStyling,
@@ -352,4 +352,32 @@ describe('TextArea', () => {
352352
const { container } = render(<TextArea value={BigInt('903')} autoSize />);
353353
expect(container.querySelector('textarea').value).toBe('903');
354354
});
355+
356+
describe('ref.nativeElement', () => {
357+
it('textarea', () => {
358+
const ref = React.createRef<TextAreaRef>();
359+
const { container } = render(
360+
<div>
361+
<TextArea ref={ref} />
362+
</div>,
363+
);
364+
365+
expect(ref.current.nativeElement).toEqual(
366+
container.querySelector('textarea'),
367+
);
368+
});
369+
370+
it('holder', () => {
371+
const ref = React.createRef<TextAreaRef>();
372+
const { container } = render(
373+
<div>
374+
<TextArea ref={ref} allowClear />
375+
</div>,
376+
);
377+
378+
expect(ref.current.nativeElement).toEqual(
379+
container.querySelector('.rc-textarea-affix-wrapper'),
380+
);
381+
});
382+
});
355383
});

0 commit comments

Comments
 (0)