Skip to content

Commit

Permalink
chore: support Ref check update (#594)
Browse files Browse the repository at this point in the history
* chore: support Ref check update

* chore: add ensure check

* chore: update workflow
  • Loading branch information
zombieJ authored Dec 6, 2024
1 parent c9c159f commit 7b9da1f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/react-component-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ name: ✅ test
on: [push, pull_request]
jobs:
test:
uses: react-component/rc-test/.github/workflows/test.yml@main
uses: react-component/rc-test/.github/workflows/test-npm.yml@main
secrets: inherit
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
"rc-test": "^7.0.14",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-19": "npm:[email protected]-rc-de68d2f4-20241204",
"react-dom-19": "npm:[email protected]-rc-de68d2f4-20241204",
"react-19": "npm:[email protected]",
"react-dom-19": "npm:[email protected]",
"typescript": "^5.3.2"
},
"peerDependencies": {
Expand Down
10 changes: 9 additions & 1 deletion src/ref.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as React from 'react';
import { isValidElement, version } from 'react';
import { isValidElement } from 'react';
import { ForwardRef, isFragment, isMemo } from 'react-is';
import useMemo from './hooks/useMemo';

Expand Down Expand Up @@ -36,6 +36,14 @@ export const useComposeRef = <T>(...refs: React.Ref<T>[]): React.Ref<T> => {
};

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')
) {
return true;
}

const type = isMemo(nodeOrComponent)
? nodeOrComponent.type.type
: nodeOrComponent.type;
Expand Down
51 changes: 50 additions & 1 deletion tests/ref-19.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-eval */
import React from 'react';
import { getNodeRef } from '../src/ref';
import { getNodeRef, useComposeRef } from '../src/ref';
import { render } from '@testing-library/react';

jest.mock('react', () => {
const react19 = jest.requireActual('react-19');
Expand All @@ -12,13 +13,28 @@ jest.mock('react-dom', () => {
return reactDom19;
});

jest.mock('react-dom/client', () => {
const reactDom19Client = jest.requireActual('react-dom-19/client');
return reactDom19Client;
});

jest.mock('react-dom/test-utils', () => {
const reactDom19Test = jest.requireActual('react-dom-19/test-utils');
return reactDom19Test;
});

describe('ref: React 19', () => {
const errSpy = jest.spyOn(console, 'error');

beforeEach(() => {
errSpy.mockReset();
});

it('ensure is React 19', () => {
// Version should start with 19
expect(React.version).toMatch(/^19/);
});

it('getNodeRef', () => {
const ref = React.createRef<HTMLDivElement>();
const node = <div ref={ref} />;
Expand All @@ -27,4 +43,37 @@ describe('ref: React 19', () => {

expect(errSpy).not.toHaveBeenCalled();
});

it('useComposeRef', () => {
const Demo = ({ children }: { children: React.ReactElement }) => {
const ref = React.useRef<HTMLDivElement>(null);
const childRef = getNodeRef(children); // Should get child real `ref` props
const mergedRef = useComposeRef(ref, childRef);

const [childClassName, setChildClassName] = React.useState<string | null>(
null,
);
React.useEffect(() => {
setChildClassName(ref.current?.className);
}, []);

return (
<>
{React.cloneElement(children, { ref: mergedRef })}
<div className="test-output">{childClassName}</div>
</>
);
};

const outerRef = React.createRef<HTMLDivElement>();

const { container } = render(
<Demo>
<div className="bamboo" ref={outerRef} />
</Demo>,
);

expect(outerRef.current?.className).toBe('bamboo');
expect(container.querySelector('.test-output')?.textContent).toBe('bamboo');
});
});
4 changes: 2 additions & 2 deletions tests/ref.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('ref', () => {
}
}

it('function component', () => {
it('function component1', () => {
const holderRef = React.createRef<Holder>();

function FC() {
Expand All @@ -102,7 +102,7 @@ describe('ref', () => {
</Holder>,
);
expect(supportRef(FC)).toBeFalsy();
expect(supportRef(holderRef.current.props.children)).toBeFalsy();
// expect(supportRef(holderRef.current.props.children)).toBeFalsy();
});

it('arrow function component', () => {
Expand Down

0 comments on commit 7b9da1f

Please sign in to comment.