Skip to content

Commit a0ef928

Browse files
authored
feat: support focus options (#1030)
* feat: done * feat: add demo * feat: add demo
1 parent 2535b87 commit a0ef928

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ src/*.js
3535
src/*.map
3636
tslint.json
3737
tsconfig.test.json
38-
.prettierignore
3938
.doc
4039
.umi
4140
.dumi/tmp

.prettierignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.doc
2+
.storybook
3+
es
4+
lib
5+
**/*.svg
6+
**/*.ejs
7+
**/*.html
8+
package.json
9+
.umi
10+
.umi-production
11+
.umi-test

docs/demo/focus.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: focus
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/focus.tsx"></code>

docs/examples/focus.tsx

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

src/BaseSelect.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export type CustomTagProps = {
7070
};
7171

7272
export interface BaseSelectRef {
73-
focus: () => void;
73+
focus: (options?: FocusOptions) => void;
7474
blur: () => void;
7575
scrollTo: ScrollTo;
7676
}

src/Selector/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
* - https://www.w3.org/TR/wai-aria-practices/examples/combobox/aria1.1pattern/listbox-combo.html
99
*/
1010

11-
import * as React from 'react';
12-
import { useRef } from 'react';
1311
import KeyCode from 'rc-util/lib/KeyCode';
1412
import type { ScrollTo } from 'rc-virtual-list/lib/List';
15-
import MultipleSelector from './MultipleSelector';
16-
import SingleSelector from './SingleSelector';
17-
import useLock from '../hooks/useLock';
13+
import * as React from 'react';
14+
import { useRef } from 'react';
1815
import type { CustomTagProps, DisplayValueType, Mode, RenderNode } from '../BaseSelect';
16+
import useLock from '../hooks/useLock';
1917
import { isValidateOpenKey } from '../utils/keyUtil';
18+
import MultipleSelector from './MultipleSelector';
19+
import SingleSelector from './SingleSelector';
2020

2121
export interface InnerSelectorProps {
2222
prefixCls: string;
@@ -47,7 +47,7 @@ export interface InnerSelectorProps {
4747
}
4848

4949
export interface RefSelectorProps {
50-
focus: () => void;
50+
focus: (options?: FocusOptions) => void;
5151
blur: () => void;
5252
scrollTo?: ScrollTo;
5353
}
@@ -122,8 +122,8 @@ const Selector: React.ForwardRefRenderFunction<RefSelectorProps, SelectorProps>
122122

123123
// ======================= Ref =======================
124124
React.useImperativeHandle(ref, () => ({
125-
focus: () => {
126-
inputRef.current.focus();
125+
focus: (options) => {
126+
inputRef.current.focus(options);
127127
},
128128
blur: () => {
129129
inputRef.current.blur();

0 commit comments

Comments
 (0)