Skip to content

Commit 6117050

Browse files
authored
feat: support nativeElement (#289)
1 parent 9b7822b commit 6117050

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface CheckboxRef {
1818
focus: (options?: FocusOptions) => void;
1919
blur: () => void;
2020
input: HTMLInputElement | null;
21+
nativeElement: HTMLElement | null;
2122
}
2223

2324
export interface CheckboxProps
@@ -41,6 +42,8 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
4142
} = props;
4243

4344
const inputRef = useRef<HTMLInputElement>(null);
45+
const holderRef = useRef<HTMLElement>(null);
46+
4447
const [rawValue, setRawValue] = useMergedState(defaultChecked, {
4548
value: checked,
4649
});
@@ -53,6 +56,7 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
5356
inputRef.current?.blur();
5457
},
5558
input: inputRef.current,
59+
nativeElement: holderRef.current,
5660
}));
5761

5862
const classString = classNames(prefixCls, className, {
@@ -86,7 +90,7 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
8690
};
8791

8892
return (
89-
<span className={classString} title={title} style={style}>
93+
<span className={classString} title={title} style={style} ref={holderRef}>
9094
<input
9195
{...inputProps}
9296
className={`${prefixCls}-input`}

tests/index.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,13 @@ describe('Checkbox ref', () => {
273273

274274
expect(ref.current?.input).toBe(inputEl);
275275
});
276+
277+
it('nativeElement should work', () => {
278+
const ref = React.createRef<CheckboxRef>();
279+
280+
const { container } = render(<Checkbox ref={ref} />);
281+
const holderEl = container.querySelector('.rc-checkbox')!;
282+
283+
expect(ref.current?.nativeElement).toBe(holderEl);
284+
});
276285
});

0 commit comments

Comments
 (0)