refactor: improve the acquisition of dom logic (#48989)

* deps: bump rc-util to 5.41.0

* refactor: improve the acquisition of dom logic

* test: add unit test
This commit is contained in:
2024-05-20 19:41:30 +08:00 committed by GitHub
parent 35015c7b4f
commit baef247630
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 7 deletions

View File

@ -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<any>();
const { getByText } = render(
<Form scrollToFirstError>
<Form.Item name="demo-form_input-number" rules={[{ required: true }]}>
<InputNumber ref={inputNumberRef} />
</Form.Item>
<Form.Item>
<Button htmlType="submit">Submit</Button>
</Form.Item>
</Form>,
);
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', () => {

View File

@ -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);

View File

@ -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"
},