Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jia-nan committed Apr 28, 2024
1 parent c2d2a30 commit 3293f24
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/ref.ts
@@ -1,5 +1,6 @@
/* eslint-disable no-param-reassign */
import type * as React from 'react';
import type { ReactNode } from 'react';
import { isValidElement } from 'react';
import { isForwardRef, isFragment, isMemo } from 'react-is';
import useMemo from './hooks/useMemo';
Expand Down Expand Up @@ -37,44 +38,37 @@ export function useComposeRef<T>(...refs: React.Ref<T>[]): React.Ref<T> {
);
}

interface WithRef {
ref: React.Ref<any>;
}

export function supportRef(nodeOrComponent: any): nodeOrComponent is WithRef {
if (isFragment(nodeOrComponent)) {
export const supportRef = <T>(value: any): value is React.RefAttributes<T> => {
if (isFragment(value)) {
return false;
}
if (isForwardRef(nodeOrComponent)) {
if (isForwardRef(value)) {
return true;
}
const type = isMemo(nodeOrComponent)
? nodeOrComponent.type.type
: nodeOrComponent.type;
const type = isMemo(value) ? value.type.type : value.type;

// Function component node
if (typeof type === 'function' && !type.prototype?.render) {
return false;
}

// Class component
if (
typeof nodeOrComponent === 'function' &&
!nodeOrComponent.prototype?.render
) {
if (typeof value === 'function' && !value.prototype?.render) {
return false;
}

return true;
}
};

export function supportNodeRef(node: React.ReactNode): boolean {
export function supportNodeRef(node: ReactNode): boolean {
if (!isValidElement(node)) {
return false;
}

if (isFragment(node)) {
return false;
}

return supportRef(node);
}
/* eslint-enable */

0 comments on commit 3293f24

Please sign in to comment.