Skip to content

Commit

Permalink
feat: support focus options (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair committed Feb 29, 2024
1 parent 1460ad6 commit 28eba5e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
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

0 comments on commit 28eba5e

Please sign in to comment.