refactor: use rc-utils's toArray replace React's Children legacy api (#55354)

* refactor: use rc-utils's toArray replace React's Children legacy api

* Update components/list/Item.tsx

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: thinkasany <480968828@qq.com>

* Update components/button/buttonHelpers.tsx

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: thinkasany <480968828@qq.com>

* Update components/card/Card.tsx

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: thinkasany <480968828@qq.com>

* Update components/carousel/index.tsx

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: thinkasany <480968828@qq.com>

* Update components/button/button.tsx

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: thinkasany <480968828@qq.com>

* fix

* fix

---------

Signed-off-by: thinkasany <480968828@qq.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: 遇见同学 <1875694521@qq.com>
This commit is contained in:
thinkasany
2025-10-20 14:45:14 +08:00
committed by GitHub
parent 4642d990a4
commit 0f91afd5cb
5 changed files with 14 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
import React, { Children, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { omit, useComposeRef } from '@rc-component/util';
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
import { omit, toArray, useComposeRef } from '@rc-component/util';
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
import { clsx } from 'clsx';
@@ -142,7 +142,7 @@ const InternalCompoundedButton = React.forwardRef<
autoFocus,
...rest
} = props;
const childNodes: React.ReactNode[] = toArray(children);
// https://github.com/ant-design/ant-design/issues/47605
// Compatible with original `type` behavior
const mergedType = type || 'default';
@@ -216,7 +216,7 @@ const InternalCompoundedButton = React.forwardRef<
const mergedRef = useComposeRef(ref, buttonRef);
const needInserted =
Children.count(children) === 1 && !icon && !isUnBorderedButtonVariant(mergedVariant);
childNodes.length === 1 && !icon && !isUnBorderedButtonVariant(mergedVariant);
// ========================= Mount ==========================
// Record for mount status.

View File

@@ -1,6 +1,6 @@
import * as React from 'react';
import type { Tab, TabBarExtraContent } from '@rc-component/tabs/lib/interface';
import omit from '@rc-component/util/lib/omit';
import { omit, toArray } from '@rc-component/util';
import { clsx } from 'clsx';
import useMergeSemantic from '../_util/hooks/useMergeSemantic';
@@ -159,8 +159,8 @@ const Card = React.forwardRef<HTMLDivElement, CardProps>((props, ref) => {
};
const isContainGrid = React.useMemo<boolean>(() => {
const childrenArray = React.Children.toArray(children);
return childrenArray.some((child) => React.isValidElement(child) && child.type === Grid);
const childNodes: React.ReactNode[] = toArray(children);
return childNodes.some((child) => React.isValidElement(child) && child.type === Grid);
}, [children]);
const prefixCls = getPrefixCls('card', customizePrefixCls);

View File

@@ -1,6 +1,7 @@
import * as React from 'react';
import type { Settings } from '@ant-design/react-slick';
import SlickCarousel from '@ant-design/react-slick';
import { toArray } from '@rc-component/util';
import { clsx } from 'clsx';
import { devUseWarning } from '../_util/warning';
@@ -104,7 +105,8 @@ const Carousel = React.forwardRef<CarouselRef, CarouselProps>((props, ref) => {
[slickRef.current],
);
const { children, initialSlide = 0 } = props;
const count = React.Children.count(children);
const childNodes: React.ReactNode[] = toArray(children);
const count = childNodes.length;
const isRTL = (rtl ?? direction === 'rtl') && !vertical;
React.useEffect(() => {

View File

@@ -1,5 +1,6 @@
import type { CSSProperties, HTMLAttributes, ReactNode } from 'react';
import React, { useContext } from 'react';
import { toArray } from '@rc-component/util';
import { clsx } from 'clsx';
import type { SemanticClassNames, SemanticStyles } from '../_util/hooks/useMergeSemantic';
@@ -87,9 +88,9 @@ const InternalItem = React.forwardRef<HTMLDivElement, ListItemProps>((props, ref
});
const isItemContainsTextNodeAndNotSingular = () => {
const childrenArray = React.Children.toArray(children);
const hasTextNode = childrenArray.some((node) => typeof node === 'string');
return hasTextNode && childrenArray.length > 1;
const childNodes: React.ReactNode[] = toArray(children);
const hasTextNode = childNodes.some((node) => typeof node === 'string');
return hasTextNode && childNodes.length > 1;
};
const isFlexMode = () => {

View File

@@ -44,7 +44,6 @@ export default antfu(
'react-refresh/only-export-components': 'off', // TODO: remove this
'react/no-clone-element': 'off',
'react/no-children-for-each': 'off',
'react/no-children-count': 'off',
'react/no-children-map': 'off',
'react/no-children-only': 'off',
'react/no-unstable-default-props': 'off',