mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-06 07:56:28 +08:00
refactor: update ref logic (#51952)
Some checks are pending
Publish Any Commit / build (push) Waiting to run
🔀 Sync mirror to Gitee / mirror (push) Waiting to run
✅ test / lint (push) Waiting to run
✅ test / test-react-legacy (16, 1/2) (push) Waiting to run
✅ test / test-react-legacy (16, 2/2) (push) Waiting to run
✅ test / test-react-legacy (17, 1/2) (push) Waiting to run
✅ test / test-react-legacy (17, 2/2) (push) Waiting to run
✅ test / test-node (push) Waiting to run
✅ test / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test / test-coverage (push) Blocked by required conditions
✅ test / build (push) Waiting to run
✅ test / test lib/es module (es, 1/2) (push) Waiting to run
✅ test / test lib/es module (es, 2/2) (push) Waiting to run
✅ test / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
Some checks are pending
Publish Any Commit / build (push) Waiting to run
🔀 Sync mirror to Gitee / mirror (push) Waiting to run
✅ test / lint (push) Waiting to run
✅ test / test-react-legacy (16, 1/2) (push) Waiting to run
✅ test / test-react-legacy (16, 2/2) (push) Waiting to run
✅ test / test-react-legacy (17, 1/2) (push) Waiting to run
✅ test / test-react-legacy (17, 2/2) (push) Waiting to run
✅ test / test-node (push) Waiting to run
✅ test / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test / test-coverage (push) Blocked by required conditions
✅ test / build (push) Waiting to run
✅ test / test lib/es module (es, 1/2) (push) Waiting to run
✅ test / test lib/es module (es, 2/2) (push) Waiting to run
✅ test / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
* refactor: update ref logic * chore: clean up * chore: coverage adjust * test: coverage * chore: back of code
This commit is contained in:
parent
c2efea0b33
commit
91878f82ad
@ -140,6 +140,13 @@ const WaveEffect: React.FC<WaveEffectProps> = (props) => {
|
|||||||
const showWaveEffect: ShowWaveEffect = (target, info) => {
|
const showWaveEffect: ShowWaveEffect = (target, info) => {
|
||||||
const { component } = info;
|
const { component } = info;
|
||||||
|
|
||||||
|
// Skip if not support `render` since `rc-util` render not support React 19
|
||||||
|
// TODO: remove this check in v6
|
||||||
|
/* istanbul ignore next */
|
||||||
|
if (!render) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Skip for unchecked checkbox
|
// Skip for unchecked checkbox
|
||||||
if (component === 'Checkbox' && !target.querySelector<HTMLInputElement>('input')?.checked) {
|
if (component === 'Checkbox' && !target.querySelector<HTMLInputElement>('input')?.checked) {
|
||||||
return;
|
return;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import React, { useContext, useRef } from 'react';
|
import React, { useContext, useRef } from 'react';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import isVisible from 'rc-util/lib/Dom/isVisible';
|
import isVisible from 'rc-util/lib/Dom/isVisible';
|
||||||
import { composeRef, supportRef } from 'rc-util/lib/ref';
|
import { composeRef, getNodeRef, supportRef } from 'rc-util/lib/ref';
|
||||||
|
|
||||||
import type { ConfigConsumerProps } from '../../config-provider';
|
import type { ConfigConsumerProps } from '../../config-provider';
|
||||||
import { ConfigContext } from '../../config-provider';
|
import { ConfigContext } from '../../config-provider';
|
||||||
@ -64,7 +64,7 @@ const Wave: React.FC<WaveProps> = (props) => {
|
|||||||
return children ?? null;
|
return children ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ref = supportRef(children) ? composeRef((children as any).ref, containerRef) : containerRef;
|
const ref = supportRef(children) ? composeRef(getNodeRef(children), containerRef) : containerRef;
|
||||||
|
|
||||||
return cloneElement(children, { ref });
|
return cloneElement(children, { ref });
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import type { FormRef } from 'rc-field-form/lib/interface';
|
import type { FormRef } from 'rc-field-form/lib/interface';
|
||||||
|
|
||||||
import Form from '..';
|
import Form, { FormInstance } from '..';
|
||||||
import { fireEvent, render } from '../../../tests/utils';
|
import { fireEvent, render } from '../../../tests/utils';
|
||||||
import Button from '../../button';
|
import Button from '../../button';
|
||||||
import type { InputRef } from '../../input';
|
import type { InputRef } from '../../input';
|
||||||
@ -94,4 +94,21 @@ describe('Form.Ref', () => {
|
|||||||
|
|
||||||
expect(container.querySelector('.ant-form')).toBe(formRef.current?.nativeElement);
|
expect(container.querySelector('.ant-form')).toBe(formRef.current?.nativeElement);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO: this is no need to test in React 19
|
||||||
|
it('not crash if not support Ref', () => {
|
||||||
|
const NoRefComp = () => <div />;
|
||||||
|
|
||||||
|
const formRef = React.createRef<FormInstance>();
|
||||||
|
render(
|
||||||
|
<Form ref={formRef}>
|
||||||
|
<Form.Item name="bamboo" label="Bamboo">
|
||||||
|
<NoRefComp />
|
||||||
|
</Form.Item>
|
||||||
|
</Form>,
|
||||||
|
);
|
||||||
|
|
||||||
|
const ele = formRef.current?.getFieldInstance('bamboo');
|
||||||
|
expect(ele).toBeFalsy();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { composeRef } from 'rc-util/lib/ref';
|
import { composeRef, getNodeRef } from 'rc-util/lib/ref';
|
||||||
|
|
||||||
import { FormContext } from '../context';
|
import { FormContext } from '../context';
|
||||||
import type { InternalNamePath } from '../interface';
|
import type { InternalNamePath } from '../interface';
|
||||||
@ -13,8 +13,10 @@ export default function useItemRef() {
|
|||||||
}>({});
|
}>({});
|
||||||
|
|
||||||
function getRef(name: InternalNamePath, children: any) {
|
function getRef(name: InternalNamePath, children: any) {
|
||||||
|
// Outer caller already check the `supportRef`
|
||||||
const childrenRef: React.Ref<React.ReactElement> =
|
const childrenRef: React.Ref<React.ReactElement> =
|
||||||
children && typeof children === 'object' && children.ref;
|
children && typeof children === 'object' && getNodeRef(children);
|
||||||
|
|
||||||
const nameStr = name.join('_');
|
const nameStr = name.join('_');
|
||||||
if (cacheRef.current.name !== nameStr || cacheRef.current.originRef !== childrenRef) {
|
if (cacheRef.current.name !== nameStr || cacheRef.current.originRef !== childrenRef) {
|
||||||
cacheRef.current.name = nameStr;
|
cacheRef.current.name = nameStr;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { supportNodeRef, useComposeRef } from 'rc-util/lib/ref';
|
import { getNodeRef, supportNodeRef, useComposeRef } from 'rc-util/lib/ref';
|
||||||
|
|
||||||
import ContextIsolator from '../_util/ContextIsolator';
|
import ContextIsolator from '../_util/ContextIsolator';
|
||||||
import type { MenuProps } from './menu';
|
import type { MenuProps } from './menu';
|
||||||
@ -39,7 +39,7 @@ export const OverrideProvider = React.forwardRef<
|
|||||||
);
|
);
|
||||||
|
|
||||||
const canRef = supportNodeRef(children);
|
const canRef = supportNodeRef(children);
|
||||||
const mergedRef = useComposeRef(ref, canRef ? children.ref : null);
|
const mergedRef = useComposeRef(ref, canRef ? getNodeRef(children) : null);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<OverrideContext.Provider value={context}>
|
<OverrideContext.Provider value={context}>
|
||||||
|
Loading…
Reference in New Issue
Block a user