mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-11 13:59:11 +08:00
63 lines
1.1 KiB
Markdown
63 lines
1.1 KiB
Markdown
---
|
||
order: 999999
|
||
title:
|
||
zh-CN: 引用字段
|
||
en-US: Ref item
|
||
debug: true
|
||
---
|
||
|
||
## zh-CN
|
||
|
||
请优先使用 `ref`!
|
||
|
||
## en-US
|
||
|
||
Use `ref` first!
|
||
|
||
```tsx
|
||
import type { InputRef } from 'antd';
|
||
import { Button, Form, Input } from 'antd';
|
||
import React from 'react';
|
||
|
||
const App: React.FC = () => {
|
||
const [form] = Form.useForm();
|
||
const ref = React.useRef<InputRef>(null);
|
||
|
||
return (
|
||
<Form form={form} initialValues={{ list: ['light'] }}>
|
||
<Form.Item name="test" label="test">
|
||
<Input ref={ref} />
|
||
</Form.Item>
|
||
|
||
<Form.List name="list">
|
||
{fields =>
|
||
fields.map(field => (
|
||
<Form.Item {...field} key={field.key}>
|
||
<Input ref={ref} />
|
||
</Form.Item>
|
||
))
|
||
}
|
||
</Form.List>
|
||
|
||
<Button
|
||
htmlType="button"
|
||
onClick={() => {
|
||
form.getFieldInstance('test').focus();
|
||
}}
|
||
>
|
||
Focus Form.Item
|
||
</Button>
|
||
<Button
|
||
onClick={() => {
|
||
form.getFieldInstance(['list', 0]).focus();
|
||
}}
|
||
>
|
||
Focus Form.List
|
||
</Button>
|
||
</Form>
|
||
);
|
||
};
|
||
|
||
export default App;
|
||
```
|