From 96c3b3eccfcfe2bdb377f929a2147c497ab5b05d Mon Sep 17 00:00:00 2001 From: kyletsang <6854874+kyletsang@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:57:46 -0700 Subject: [PATCH] use react version --- src/useRTGTransitionProps.ts | 10 +++++++--- src/utils.ts | 12 +++++++++++- test/ModalSpec.js | 3 --- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/useRTGTransitionProps.ts b/src/useRTGTransitionProps.ts index 0340602..7685d5a 100644 --- a/src/useRTGTransitionProps.ts +++ b/src/useRTGTransitionProps.ts @@ -1,9 +1,11 @@ +import * as React from 'react'; import { cloneElement, useCallback, useRef } from 'react'; import useMergedRefs from '@restart/hooks/useMergedRefs'; import { TransitionProps as RTGTransitionProps, TransitionStatus, } from 'react-transition-group/Transition'; +import { getReactVersion } from './utils'; export type TransitionProps = RTGTransitionProps & { children: @@ -32,12 +34,14 @@ export default function useRTGTransitionProps({ children, ...props }: TransitionProps) { + const { major } = getReactVersion(); + const childRef = + major >= 19 ? (children as any).props.ref : (children as any).ref; + const nodeRef = useRef(null); const mergedRef = useMergedRefs( nodeRef, - typeof children === 'function' - ? null - : (children as any).props.ref || (children as any).ref, + typeof children === 'function' ? null : childRef, ); const normalize = diff --git a/src/utils.ts b/src/utils.ts index fcd515b..1eda92f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,14 @@ -/* eslint-disable import/prefer-default-export */ +import * as React from 'react'; + export function isEscKey(e: KeyboardEvent) { return e.code === 'Escape' || e.keyCode === 27; } + +export function getReactVersion() { + const parts = React.version.split('.'); + return { + major: +parts[0], + minor: +parts[1], + patch: +parts[2], + }; +} diff --git a/test/ModalSpec.js b/test/ModalSpec.js index 96e0151..09485b9 100644 --- a/test/ModalSpec.js +++ b/test/ModalSpec.js @@ -8,7 +8,6 @@ import simulant from 'simulant'; import { render } from '@testing-library/react'; import { mount } from 'enzyme'; -import { shouldWarn } from './helpers'; import Modal from '../src/Modal'; import { OPEN_DATA_ATTRIBUTE } from '../src/ModalManager'; @@ -240,8 +239,6 @@ describe('', () => { }); it('should pass transition callbacks to Transition', (done) => { - // Expected since ref is only a prop in react 19+. - shouldWarn('Warning: div: `ref` is not a prop'); let count = 0; let increment = () => count++;