Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/demo/focus.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { CheckboxRef } from 'rc-checkbox';
import Checkbox from 'rc-checkbox';
import { useLayoutEffect, useRef, useState } from 'react';
import '../../assets/index.less';

const MyCheckbox = () => {
const ref = useRef<CheckboxRef>(null);
useLayoutEffect(() => {
if (ref.current) {
ref.current.focus({ preventScroll: true });
}
}, []);
return (
<div>
<Checkbox ref={ref} />
</div>
);
};

const Demo = () => {
const [open, setOpen] = useState(false);
return (
<div>
<div style={{ height: '50vh' }} />
<a onClick={() => setOpen(!open)}>{`${open}`}</a>
<div style={{ height: '80vh' }} />
{open && <MyCheckbox />}
<div style={{ height: '30vh' }} />
</div>
);
};

export default Demo;
2 changes: 2 additions & 0 deletions docs/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ nav:
---

<code src="./demo/simple.tsx"></code>

<code src="./demo/focus.tsx"></code>
6 changes: 3 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface CheckboxChangeEventTarget extends CheckboxProps {
}

export interface CheckboxRef {
focus: () => void;
focus: (options?: FocusOptions) => void;
blur: () => void;
input: HTMLInputElement | null;
}
Expand Down Expand Up @@ -46,8 +46,8 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
});

useImperativeHandle(ref, () => ({
focus: () => {
inputRef.current?.focus();
focus: (options) => {
inputRef.current?.focus(options);
},
blur: () => {
inputRef.current?.blur();
Expand Down