From 954d6377d70566d2140d272ef3c81b3c7ddf17f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Sat, 4 May 2024 19:17:15 +0800 Subject: [PATCH] chore: rewrite function => arrow function --- src/ref.ts | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/ref.ts b/src/ref.ts index 7f708c89..d957966e 100644 --- a/src/ref.ts +++ b/src/ref.ts @@ -1,43 +1,41 @@ -/* eslint-disable no-param-reassign */ import type * as React from 'react'; import { isValidElement } from 'react'; import { ForwardRef, isFragment, isMemo } from 'react-is'; import useMemo from './hooks/useMemo'; -export function fillRef(ref: React.Ref, node: T) { +export const fillRef = (ref: React.Ref, node: T) => { if (typeof ref === 'function') { ref(node); } else if (typeof ref === 'object' && ref && 'current' in ref) { (ref as any).current = node; } -} +}; /** * Merge refs into one ref function to support ref passing. */ -export function composeRef(...refs: React.Ref[]): React.Ref { - const refList = refs.filter(ref => ref); +export const composeRef = (...refs: React.Ref[]): React.Ref => { + const refList = refs.filter(Boolean); if (refList.length <= 1) { return refList[0]; } - return (node: T) => { refs.forEach(ref => { fillRef(ref, node); }); }; -} +}; -export function useComposeRef(...refs: React.Ref[]): React.Ref { +export const useComposeRef = (...refs: React.Ref[]): React.Ref => { return useMemo( () => composeRef(...refs), refs, (prev, next) => prev.length !== next.length || prev.every((ref, i) => ref !== next[i]), ); -} +}; -export function supportRef(nodeOrComponent: any): boolean { +export const supportRef = (nodeOrComponent: any): boolean => { const type = isMemo(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type; @@ -60,11 +58,11 @@ export function supportRef(nodeOrComponent: any): boolean { return false; } return true; -} +}; -export function supportNodeRef( +export const supportNodeRef = ( node: React.ReactNode, -): node is React.ReactElement & React.RefAttributes { +): node is React.ReactElement & React.RefAttributes => { if (!isValidElement(node)) { return false; } @@ -72,5 +70,4 @@ export function supportNodeRef( return false; } return supportRef(node); -} -/* eslint-enable */ +};