2023-08-30 22:09:32 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
|
|
|
|
|
|
|
import type { DescriptionsItemType, InternalDescriptionsItemType } from '..';
|
2024-04-08 14:04:08 +08:00
|
|
|
import { matchScreen } from '../../_util/responsiveObserver';
|
|
|
|
import type { ScreenMap } from '../../_util/responsiveObserver';
|
2023-08-30 22:09:32 +08:00
|
|
|
|
|
|
|
// Convert children into items
|
|
|
|
const transChildren2Items = (childNodes?: React.ReactNode) =>
|
2023-11-09 16:37:10 +08:00
|
|
|
toArray(childNodes).map((node) => ({ ...node?.props, key: node.key }));
|
2023-08-30 22:09:32 +08:00
|
|
|
|
|
|
|
export default function useItems(
|
|
|
|
screens: ScreenMap,
|
|
|
|
items?: DescriptionsItemType[],
|
|
|
|
children?: React.ReactNode,
|
|
|
|
) {
|
|
|
|
const mergedItems = React.useMemo<DescriptionsItemType[]>(
|
|
|
|
() =>
|
|
|
|
// Take `items` first or convert `children` into items
|
|
|
|
items || transChildren2Items(children),
|
|
|
|
[items, children],
|
|
|
|
);
|
|
|
|
|
|
|
|
const responsiveItems = React.useMemo<InternalDescriptionsItemType[]>(
|
|
|
|
() =>
|
|
|
|
mergedItems.map(({ span, ...restItem }) => ({
|
|
|
|
...restItem,
|
|
|
|
span: typeof span === 'number' ? span : matchScreen(screens, span),
|
|
|
|
})),
|
|
|
|
[mergedItems, screens],
|
|
|
|
);
|
|
|
|
|
|
|
|
return responsiveItems;
|
|
|
|
}
|