From 99d8344cbefdd370e1faab3c211f9b9731a4a1b5 Mon Sep 17 00:00:00 2001 From: afc163 Date: Sat, 8 Feb 2025 12:02:09 +0800 Subject: [PATCH 1/3] fix: ref could be passed as a prop in 19.x close https://github.com/ant-design/ant-design/issues/52282 --- src/ref.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ref.ts b/src/ref.ts index 9297a4be..3c653f93 100644 --- a/src/ref.ts +++ b/src/ref.ts @@ -45,7 +45,7 @@ export const supportRef = (nodeOrComponent: any): boolean => { // React 19 no need `forwardRef` anymore. So just pass if is a React element. if ( isReactElement(nodeOrComponent) && - (nodeOrComponent as any).props.propertyIsEnumerable('ref') + React.version.startsWith('19.') ) { return true; } From b26f06820a159b44e1d6814d50af027aae374c2d Mon Sep 17 00:00:00 2001 From: afc163 Date: Sat, 8 Feb 2025 12:05:43 +0800 Subject: [PATCH 2/3] Update ref.ts --- src/ref.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ref.ts b/src/ref.ts index 3c653f93..71442fa8 100644 --- a/src/ref.ts +++ b/src/ref.ts @@ -1,5 +1,5 @@ import type * as React from 'react'; -import { isValidElement } from 'react'; +import { isValidElement, version } from 'react'; import { ForwardRef, isMemo } from 'react-is'; import useMemo from './hooks/useMemo'; import isFragment from './React/isFragment'; @@ -45,7 +45,7 @@ export const supportRef = (nodeOrComponent: any): boolean => { // React 19 no need `forwardRef` anymore. So just pass if is a React element. if ( isReactElement(nodeOrComponent) && - React.version.startsWith('19.') + version.startsWith('19.') ) { return true; } From 4ac8edc7bc5f9fd8b45e5fe34667c6c9c5dcae0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Sat, 8 Feb 2025 14:43:35 +0800 Subject: [PATCH 3/3] test: add test case --- src/ref.ts | 7 +++---- tests/ref-19.test.tsx | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ref.ts b/src/ref.ts index 71442fa8..fd9513b9 100644 --- a/src/ref.ts +++ b/src/ref.ts @@ -4,6 +4,8 @@ import { ForwardRef, isMemo } from 'react-is'; import useMemo from './hooks/useMemo'; import isFragment from './React/isFragment'; +const ReactMajorVersion = Number(version.split('.')[0]); + export const fillRef = (ref: React.Ref, node: T) => { if (typeof ref === 'function') { ref(node); @@ -43,10 +45,7 @@ export const supportRef = (nodeOrComponent: any): boolean => { } // React 19 no need `forwardRef` anymore. So just pass if is a React element. - if ( - isReactElement(nodeOrComponent) && - version.startsWith('19.') - ) { + if (isReactElement(nodeOrComponent) && ReactMajorVersion >= 19) { return true; } diff --git a/tests/ref-19.test.tsx b/tests/ref-19.test.tsx index fab08af2..775fb317 100644 --- a/tests/ref-19.test.tsx +++ b/tests/ref-19.test.tsx @@ -1,6 +1,6 @@ /* eslint-disable no-eval */ import React from 'react'; -import { getNodeRef, useComposeRef } from '../src/ref'; +import { getNodeRef, useComposeRef, supportRef } from '../src/ref'; import { render } from '@testing-library/react'; jest.mock('react', () => { @@ -76,4 +76,20 @@ describe('ref: React 19', () => { expect(outerRef.current?.className).toBe('bamboo'); expect(container.querySelector('.test-output')?.textContent).toBe('bamboo'); }); + + it('supportRef with not provide ref', () => { + const Empty = () =>
; + + const Checker = ({ children }: { children: React.ReactElement }) => { + return

{String(supportRef(children))}

; + }; + + const { container } = render( + + + , + ); + + expect(container.querySelector('p')?.textContent).toBe('true'); + }); });