pro-components icon indicating copy to clipboard operation
pro-components copied to clipboard

🐛[BUG] ProForm的formRef调用getFieldInstance方法拿不到输入组件实例

Open cui-xxu opened this issue 3 years ago • 2 comments
trafficstars

🐛 bug 描述

ProForm的formRef,调用setFieldsValue正常,但是调用getFieldInstance方法,只能得到undefined

📷 复现步骤

import type { ProFormInstance } from '@ant-design/pro-form'; import ProForm, { ProFormText } from '@ant-design/pro-form'; import { useRef } from 'react';

const App = () => { const formRef = useRef<ProFormInstance>();

return (
	<ProForm
		formRef={formRef}
		onValuesChange={() => {
			console.log('note2', formRef.current?.getFieldInstance('note2'));
			formRef.current?.setFieldsValue({ note3: 'aaa' });
		}}
	>
		<ProFormText name='note' label='Note' />
		<ProFormText name='note2' label='Note2' />
		<ProFormText name='note3' label='Note3' />
	</ProForm>
);

};

export default App;

🏞 期望结果

getFieldInstance能够拿到组件实例,以便调用focus方法

© 版本信息

@ant-design/pro-form: 1.71.2 antd: 4.22.3 umi: 3.5.21 浏览器: 搜狗 开发环境 win10

cui-xxu avatar Aug 02 '22 08:08 cui-xxu

补充一下,之前需要getFieldInstance,是为了调用focus,因为在做页面动态控制的时候,每次动态效果生效,页面刷新,光标就会自动跳到第一个输入框,更新最新版本form以后,这个问题没有了,暂时就不需要调用getFieldInstance了。

cui-xxu avatar Aug 04 '22 02:08 cui-xxu

确实有这个bug

yuankeyan avatar Aug 17 '22 03:08 yuankeyan

确实有这个bug ,同一个form下,用原始的<Form.Item label="Username" name="username" rules={[{ required: true, message: 'Please input your username!' }]} > <Input /> </Form.Item> 就正常!

yuankeyan avatar Aug 17 '22 06:08 yuankeyan

const form = ProForm.useFormInstance();

用这个来解决问题吧,form的实例不一样

chenshuai2144 avatar Aug 26 '22 08:08 chenshuai2144

Is there an example of this? I am trying to set focus on one of my inputs but always get undefined when calling getFieldInstance. I have tried const form = ProForm.useFormInstance(); but get the same result.

@cui-xxu Were you able to get this to work?

askurat avatar Mar 10 '23 21:03 askurat