mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-11 23:00:20 +08:00
24a7da8ea8
* docs(Input): add proper type for `inputRef` * docs: use `InputRef` instead of `ElementRef` Co-authored-by: afc163 <afc163@gmail.com>
88 lines
1.7 KiB
Markdown
88 lines
1.7 KiB
Markdown
---
|
|
order: 21
|
|
title:
|
|
zh-CN: 聚焦
|
|
en-US: Focus
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
聚焦额外配置属性。
|
|
|
|
## en-US
|
|
|
|
Focus with additional option.
|
|
|
|
```tsx
|
|
import type { InputRef } from 'antd';
|
|
|
|
import { Button, Input, Space, Switch } from 'antd';
|
|
import React, { useRef, useState } from 'react';
|
|
|
|
const App: React.FC = () => {
|
|
const inputRef = useRef<InputRef>(null);
|
|
const [input, setInput] = useState(true);
|
|
|
|
const sharedProps = {
|
|
style: { width: '100%' },
|
|
defaultValue: 'Ant Design love you!',
|
|
ref: inputRef,
|
|
};
|
|
|
|
return (
|
|
<Space direction="vertical" style={{ width: '100%' }}>
|
|
<Space wrap>
|
|
<Button
|
|
onClick={() => {
|
|
inputRef.current!.focus({
|
|
cursor: 'start',
|
|
});
|
|
}}
|
|
>
|
|
Focus at first
|
|
</Button>
|
|
<Button
|
|
onClick={() => {
|
|
inputRef.current!.focus({
|
|
cursor: 'end',
|
|
});
|
|
}}
|
|
>
|
|
Focus at last
|
|
</Button>
|
|
<Button
|
|
onClick={() => {
|
|
inputRef.current!.focus({
|
|
cursor: 'all',
|
|
});
|
|
}}
|
|
>
|
|
Focus to select all
|
|
</Button>
|
|
<Button
|
|
onClick={() => {
|
|
inputRef.current!.focus({
|
|
preventScroll: true,
|
|
});
|
|
}}
|
|
>
|
|
Focus prevent scroll
|
|
</Button>
|
|
<Switch
|
|
checked={input}
|
|
checkedChildren="Input"
|
|
unCheckedChildren="TextArea"
|
|
onChange={() => {
|
|
setInput(!input);
|
|
}}
|
|
/>
|
|
</Space>
|
|
<br />
|
|
{input ? <Input {...sharedProps} /> : <Input.TextArea {...sharedProps} />}
|
|
</Space>
|
|
);
|
|
};
|
|
|
|
export default App;
|
|
```
|