Skip to content

Commit 28eba5e

Browse files
authored
feat: support focus options (#275)
1 parent 1460ad6 commit 28eba5e

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

docs/demo/focus.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { CheckboxRef } from 'rc-checkbox';
2+
import Checkbox from 'rc-checkbox';
3+
import { useLayoutEffect, useRef, useState } from 'react';
4+
import '../../assets/index.less';
5+
6+
const MyCheckbox = () => {
7+
const ref = useRef<CheckboxRef>(null);
8+
useLayoutEffect(() => {
9+
if (ref.current) {
10+
ref.current.focus({ preventScroll: true });
11+
}
12+
}, []);
13+
return (
14+
<div>
15+
<Checkbox ref={ref} />
16+
</div>
17+
);
18+
};
19+
20+
const Demo = () => {
21+
const [open, setOpen] = useState(false);
22+
return (
23+
<div>
24+
<div style={{ height: '50vh' }} />
25+
<a onClick={() => setOpen(!open)}>{`${open}`}</a>
26+
<div style={{ height: '80vh' }} />
27+
{open && <MyCheckbox />}
28+
<div style={{ height: '30vh' }} />
29+
</div>
30+
);
31+
};
32+
33+
export default Demo;

docs/example.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ nav:
66
---
77

88
<code src="./demo/simple.tsx"></code>
9+
10+
<code src="./demo/focus.tsx"></code>

src/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface CheckboxChangeEventTarget extends CheckboxProps {
1515
}
1616

1717
export interface CheckboxRef {
18-
focus: () => void;
18+
focus: (options?: FocusOptions) => void;
1919
blur: () => void;
2020
input: HTMLInputElement | null;
2121
}
@@ -46,8 +46,8 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
4646
});
4747

4848
useImperativeHandle(ref, () => ({
49-
focus: () => {
50-
inputRef.current?.focus();
49+
focus: (options) => {
50+
inputRef.current?.focus(options);
5151
},
5252
blur: () => {
5353
inputRef.current?.blur();

0 commit comments

Comments
 (0)