mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-15 13:38:29 +02:00
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import * as React from 'react';
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
|
|
|
import type { PanelProps } from '../interface';
|
|
|
|
export type ItemType = Omit<PanelProps, 'collapsible'> & {
|
|
collapsible: {
|
|
start?: boolean;
|
|
end?: boolean;
|
|
showCollapsibleIcon: 'auto' | boolean;
|
|
};
|
|
};
|
|
|
|
function getCollapsible(collapsible?: PanelProps['collapsible']): ItemType['collapsible'] {
|
|
if (collapsible && typeof collapsible === 'object') {
|
|
return {
|
|
...collapsible,
|
|
showCollapsibleIcon:
|
|
collapsible.showCollapsibleIcon === undefined ? 'auto' : collapsible.showCollapsibleIcon,
|
|
};
|
|
}
|
|
|
|
const mergedCollapsible = !!collapsible;
|
|
return {
|
|
start: mergedCollapsible,
|
|
end: mergedCollapsible,
|
|
showCollapsibleIcon: 'auto',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Convert `children` into `items`.
|
|
*/
|
|
function useItems(children: React.ReactNode): ItemType[] {
|
|
const items = React.useMemo(
|
|
() =>
|
|
toArray(children)
|
|
.filter((item) => React.isValidElement<PanelProps>(item))
|
|
.map((node) => {
|
|
const { props } = node;
|
|
const { collapsible, ...restProps } = props;
|
|
return {
|
|
...restProps,
|
|
collapsible: getCollapsible(collapsible),
|
|
};
|
|
}),
|
|
[children],
|
|
);
|
|
return items;
|
|
}
|
|
|
|
export default useItems;
|