2020-12-29 22:42:04 +08:00
|
|
|
---
|
|
|
|
order: 21
|
|
|
|
title:
|
|
|
|
zh-CN: 聚焦
|
|
|
|
en-US: Focus
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
聚焦额外配置属性。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Focus with additional option.
|
|
|
|
|
|
|
|
```tsx
|
2022-06-21 10:45:49 +08:00
|
|
|
import type { InputRef } from 'antd';
|
|
|
|
|
2022-05-23 14:37:16 +08:00
|
|
|
import { Button, Input, Space, Switch } from 'antd';
|
|
|
|
import React, { useRef, useState } from 'react';
|
2020-12-29 22:42:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
2022-06-21 10:45:49 +08:00
|
|
|
const inputRef = useRef<InputRef>(null);
|
2022-05-19 09:46:26 +08:00
|
|
|
const [input, setInput] = useState(true);
|
2020-12-29 22:42:04 +08:00
|
|
|
|
|
|
|
const sharedProps = {
|
|
|
|
style: { width: '100%' },
|
|
|
|
defaultValue: 'Ant Design love you!',
|
|
|
|
ref: inputRef,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Space direction="vertical" style={{ width: '100%' }}>
|
2021-01-26 09:36:20 +08:00
|
|
|
<Space wrap>
|
2020-12-29 22:42:04 +08:00
|
|
|
<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>
|
2021-01-26 09:36:20 +08:00
|
|
|
<br />
|
2020-12-29 22:42:04 +08:00
|
|
|
{input ? <Input {...sharedProps} /> : <Input.TextArea {...sharedProps} />}
|
|
|
|
</Space>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2020-12-29 22:42:04 +08:00
|
|
|
```
|