Skip to content

Commit

Permalink
refactor: rewrite getAction function to FC (#46032)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jia-nan committed Nov 23, 2023
1 parent 36768c4 commit ea56970
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions components/card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,24 @@ export interface CardProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 't
tabProps?: TabsProps;
}

function getAction(actions: React.ReactNode[]): React.ReactNode[] {
return actions.map<React.ReactNode>((action, index) => {
// Move this out since eslint not allow index key
// And eslint-disable makes conflict with rollup
// ref https://github.com/ant-design/ant-design/issues/46022
const key = `action-${index}`;

return (
<li style={{ width: `${100 / actions.length}%` }} key={key}>
<span>{action}</span>
</li>
);
});
}
const ActionNode: React.FC<{ prefixCls: string; actions: React.ReactNode[] }> = (props) => {
const { prefixCls, actions = [] } = props;
return (
<ul className={`${prefixCls}-actions`}>
{actions.map<React.ReactNode>((action, index) => {
// Move this out since eslint not allow index key
// And eslint-disable makes conflict with rollup
// ref https://github.com/ant-design/ant-design/issues/46022
const key = `action-${index}`;
return (
<li style={{ width: `${100 / actions.length}%` }} key={key}>
<span>{action}</span>
</li>
);
})}
</ul>
);
};

const Card = React.forwardRef<HTMLDivElement, CardProps>((props, ref) => {
const {
Expand Down Expand Up @@ -151,10 +155,9 @@ const Card = React.forwardRef<HTMLDivElement, CardProps>((props, ref) => {
{loading ? loadingBlock : children}
</div>
);

const actionDom =
actions && actions.length ? (
<ul className={`${prefixCls}-actions`}>{getAction(actions)}</ul>
) : null;
actions && actions.length ? <ActionNode prefixCls={prefixCls} actions={actions} /> : null;

const divProps = omit(others, ['onTabChange']);

Expand Down

0 comments on commit ea56970

Please sign in to comment.