diff --git a/components/form/__tests__/index.test.tsx b/components/form/__tests__/index.test.tsx index 2bc7e6b26e..08d911d73d 100644 --- a/components/form/__tests__/index.test.tsx +++ b/components/form/__tests__/index.test.tsx @@ -559,6 +559,26 @@ describe('Form', () => { expect(scrollIntoView).toHaveBeenCalled(); expect((scrollIntoView as any).mock.calls[0][0]).toBe(uploadRef.current.nativeElement); }); + + // https://github.com/ant-design/ant-design/issues/48981 + it('should not throw error when use InputNumber', async () => { + const inputNumberRef = React.createRef(); + + const { getByText } = render( +
+ + + + + + +
, + ); + fireEvent.click(getByText('Submit')); + await waitFakeTimer(); + expect(scrollIntoView).toHaveBeenCalled(); + expect((scrollIntoView as any).mock.calls[0][0]).toBe(inputNumberRef.current?.nativeElement); + }); }); it('Form.Item should support data-*、aria-* and custom attribute', () => { diff --git a/components/form/hooks/useForm.ts b/components/form/hooks/useForm.ts index 36bbaaa2de..b5601b1502 100644 --- a/components/form/hooks/useForm.ts +++ b/components/form/hooks/useForm.ts @@ -2,6 +2,7 @@ import * as React from 'react'; import type { FormInstance as RcFormInstance } from 'rc-field-form'; import { useForm as useRcForm } from 'rc-field-form'; import scrollIntoView from 'scroll-into-view-if-needed'; +import { getDOM } from 'rc-util/lib/Dom/findDOMNode'; import type { InternalNamePath, NamePath, ScrollOptions } from '../interface'; import { getFieldId, toArray } from '../util'; @@ -25,13 +26,10 @@ function toNamePathStr(name: NamePath) { function getFieldDOMNode(name: NamePath, wrapForm: FormInstance) { const field = wrapForm.getFieldInstance(name); + const fieldDom = getDOM(field); - if (field instanceof HTMLElement) { - return field; - } - - if (field?.nativeElement instanceof HTMLElement) { - return field.nativeElement; + if (fieldDom) { + return fieldDom; } const fieldId = getFieldId(toArray(name), wrapForm.__INTERNAL__.name); diff --git a/package.json b/package.json index 99fe657c07..a9be6c7048 100644 --- a/package.json +++ b/package.json @@ -146,7 +146,7 @@ "rc-tree": "~5.8.7", "rc-tree-select": "~5.21.0", "rc-upload": "~4.5.2", - "rc-util": "^5.40.1", + "rc-util": "^5.41.0", "scroll-into-view-if-needed": "^3.1.0", "throttle-debounce": "^5.0.0" },