Skip to content

Commit

Permalink
feat: Add forwardRef for Card
Browse files Browse the repository at this point in the history
  • Loading branch information
linhf123 committed Nov 2, 2023
1 parent 2cbbf6c commit 1305842
Showing 1 changed file with 46 additions and 40 deletions.
86 changes: 46 additions & 40 deletions packages/design/src/card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,55 @@ export interface CardProps extends AntCardProps {
divided?: boolean;
tabList?: CardTabListType[];
}
type CardType = typeof AntCard;

const Card = ({
children,
divided = true,
tabList,
prefixCls: customizePrefixCls,
className,
...restProps
}: CardProps) => {
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
const prefixCls = getPrefixCls('card', customizePrefixCls);
const tabsPrefixCls = getPrefixCls('tabs', customizePrefixCls);
const { wrapSSR } = useStyle(prefixCls, tabsPrefixCls);
const cardCls = classNames(
{
[`${prefixCls}-no-divider`]: !divided,
},
className
);
const Card = React.forwardRef<HTMLDivElement, CardProps>(
(
{ children, divided = true, tabList, prefixCls: customizePrefixCls, className, ...restProps },
ref
) => {
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
const prefixCls = getPrefixCls('card', customizePrefixCls);
const tabsPrefixCls = getPrefixCls('tabs', customizePrefixCls);
const { wrapSSR } = useStyle(prefixCls, tabsPrefixCls);

const newTabList = tabList?.map(item => {
if (!isNullValue(item.tag)) {
return {
...item,
tab: (
<Space size={4}>
{item.tab}
<Tag bordered={false} className={`${tabsPrefixCls}-tab-tag`}>
{item.tag}
</Tag>
</Space>
),
};
}
return item;
});
const cardCls = classNames(
{
[`${prefixCls}-no-divider`]: !divided,
},
className
);

return wrapSSR(
<AntCard tabList={newTabList} prefixCls={customizePrefixCls} className={cardCls} {...restProps}>
{children}
</AntCard>
);
};
const newTabList = tabList?.map(item => {
if (!isNullValue(item.tag)) {
return {
...item,
tab: (
<Space size={4}>
{item.tab}
<Tag bordered={false} className={`${tabsPrefixCls}-tab-tag`}>
{item.tag}
</Tag>
</Space>
),
};
}
return item;
});

return wrapSSR(
<AntCard
ref={ref}
tabList={newTabList}
prefixCls={customizePrefixCls}
className={cardCls}
{...restProps}
>
{children}
</AntCard>
);
}
);

Card.Grid = AntCard.Grid;

Check failure on line 74 in packages/design/src/card/index.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

Property 'Grid' does not exist on type 'ForwardRefExoticComponent<CardProps & RefAttributes<HTMLDivElement>>'.
Card.Meta = AntCard.Meta;

Check failure on line 75 in packages/design/src/card/index.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

Property 'Meta' does not exist on type 'ForwardRefExoticComponent<CardProps & RefAttributes<HTMLDivElement>>'.
Expand Down

0 comments on commit 1305842

Please sign in to comment.