Skip to content

Commit

Permalink
feat: support focus options (#1030)
Browse files Browse the repository at this point in the history
* feat: done

* feat: add demo

* feat: add demo
  • Loading branch information
crazyair committed Feb 28, 2024
1 parent 2535b87 commit a0ef928
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 10 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -35,7 +35,6 @@ src/*.js
src/*.map
tslint.json
tsconfig.test.json
.prettierignore
.doc
.umi
.dumi/tmp
Expand Down
11 changes: 11 additions & 0 deletions .prettierignore
@@ -0,0 +1,11 @@
.doc
.storybook
es
lib
**/*.svg
**/*.ejs
**/*.html
package.json
.umi
.umi-production
.umi-test
8 changes: 8 additions & 0 deletions docs/demo/focus.md
@@ -0,0 +1,8 @@
---
title: focus
nav:
title: Demo
path: /demo
---

<code src="../examples/focus.tsx"></code>
40 changes: 40 additions & 0 deletions docs/examples/focus.tsx
@@ -0,0 +1,40 @@
import React, { useLayoutEffect, useRef, useState } from 'react';
import type { BaseSelectRef } from 'rc-select';
import Select, { Option } from 'rc-select';
import '../../assets/index.less';

const MySelect = () => {
const ref = useRef<BaseSelectRef>(null);
useLayoutEffect(() => {
if (ref.current) {
ref.current.focus({ preventScroll: true });
}
}, []);
return (
<div>
<Select placeholder="placeholder" showSearch ref={ref}>
<Option value="11" text="lucy">
lucy
</Option>
<Option value="21" text="disabled">
disabled
</Option>
</Select>
</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 && <MySelect />}
<div style={{ height: '30vh' }} />
</div>
);
};

export default Demo;
2 changes: 1 addition & 1 deletion src/BaseSelect.tsx
Expand Up @@ -70,7 +70,7 @@ export type CustomTagProps = {
};

export interface BaseSelectRef {
focus: () => void;
focus: (options?: FocusOptions) => void;
blur: () => void;
scrollTo: ScrollTo;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Selector/index.tsx
Expand Up @@ -8,15 +8,15 @@
* - https://www.w3.org/TR/wai-aria-practices/examples/combobox/aria1.1pattern/listbox-combo.html
*/

import * as React from 'react';
import { useRef } from 'react';
import KeyCode from 'rc-util/lib/KeyCode';
import type { ScrollTo } from 'rc-virtual-list/lib/List';
import MultipleSelector from './MultipleSelector';
import SingleSelector from './SingleSelector';
import useLock from '../hooks/useLock';
import * as React from 'react';
import { useRef } from 'react';
import type { CustomTagProps, DisplayValueType, Mode, RenderNode } from '../BaseSelect';
import useLock from '../hooks/useLock';
import { isValidateOpenKey } from '../utils/keyUtil';
import MultipleSelector from './MultipleSelector';
import SingleSelector from './SingleSelector';

export interface InnerSelectorProps {
prefixCls: string;
Expand Down Expand Up @@ -47,7 +47,7 @@ export interface InnerSelectorProps {
}

export interface RefSelectorProps {
focus: () => void;
focus: (options?: FocusOptions) => void;
blur: () => void;
scrollTo?: ScrollTo;
}
Expand Down Expand Up @@ -122,8 +122,8 @@ const Selector: React.ForwardRefRenderFunction<RefSelectorProps, SelectorProps>

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

0 comments on commit a0ef928

Please sign in to comment.