2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2022-08-05 10:49:08 +08:00
|
|
|
import type { Tab } from 'rc-tabs/lib/interface';
|
2023-09-11 17:28:04 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
|
|
|
|
|
|
|
import type { TabPaneProps, TabsProps } from '..';
|
|
|
|
import { devUseWarning } from '../../_util/warning';
|
2022-08-05 10:49:08 +08:00
|
|
|
|
|
|
|
function filter<T>(items: (T | null)[]): T[] {
|
2022-11-19 13:47:33 +08:00
|
|
|
return items.filter((item) => item) as T[];
|
2022-08-05 10:49:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function useLegacyItems(items?: TabsProps['items'], children?: React.ReactNode) {
|
2023-09-11 17:28:04 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
const warning = devUseWarning();
|
|
|
|
warning(
|
|
|
|
!children,
|
|
|
|
'Tabs',
|
|
|
|
'deprecated',
|
|
|
|
'Tabs.TabPane is deprecated. Please use `items` directly.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:49:08 +08:00
|
|
|
if (items) {
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
const childrenItems = toArray(children).map((node: React.ReactElement<TabPaneProps>) => {
|
|
|
|
if (React.isValidElement(node)) {
|
|
|
|
const { key, props } = node;
|
|
|
|
const { tab, ...restProps } = props || {};
|
|
|
|
|
|
|
|
const item: Tab = {
|
|
|
|
key: String(key),
|
|
|
|
...restProps,
|
|
|
|
label: tab,
|
|
|
|
};
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
return filter(childrenItems);
|
|
|
|
}
|