Skip to content

Commit

Permalink
feat(ffe-cards-react): icons on right IconCard
Browse files Browse the repository at this point in the history
  • Loading branch information
abhustoft committed Aug 19, 2024
1 parent e9b9e32 commit 636ebd3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/ffe-cards-react/src/IconCard/IconCard.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ describe('IconCard', () => {
).toBeTruthy();
});

it('should render icon on the right when modifier iconPosition="right', () => {
render(
<IconCard
data-testid={TEST_ID}
iconPosition="right"
icon={<Icon fileUrl={savingsIconXlarge} size="xl" />}
>
{children}
</IconCard>,
);
const card = screen.getByTestId(TEST_ID);
expect(card.classList.contains('ffe-icon-card')).toBeTruthy();
});

it('should render children as a function', () => {
render(
<IconCard
Expand Down
17 changes: 15 additions & 2 deletions packages/ffe-cards-react/src/IconCard/IconCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import classNames from 'classnames';
import { WithCardAction, Text, Subtext, Title, CardName } from '../components';
import { fixedForwardRef } from '../fixedForwardRef';

export type IconPosition = 'right' | 'left';

export type IconCardProps<As extends ElementType = 'div'> = Omit<
ComponentAsPropParams<As>,
'children'
Expand All @@ -12,6 +14,8 @@ export type IconCardProps<As extends ElementType = 'div'> = Omit<
icon: ReactElement;
/** Smaller icon and less space */
condensed?: boolean;
/** Position icon at left (default) or right of the card content */
iconPosition?: IconPosition;
children:
| React.ReactNode
| ((cardRenderProps: CardRenderProps) => React.ReactNode);
Expand All @@ -21,7 +25,9 @@ function IconCardWithForwardRef<As extends ElementType>(
props: IconCardProps<As>,
ref: ForwardedRef<any>,
) {
const { className, condensed, icon, children, ...rest } = props;
const { className, condensed, icon, children, iconPosition, ...rest } =
props;
const iconRight = iconPosition === 'right';
return (
<WithCardAction
baseClassName="ffe-icon-card"
Expand All @@ -35,7 +41,7 @@ function IconCardWithForwardRef<As extends ElementType>(
>
{({ CardAction }) => (
<>
{React.cloneElement(icon, {
{!iconRight && React.cloneElement(icon, {
...icon.props,
className: classNames(
'ffe-icon-card__icon',
Expand All @@ -53,6 +59,13 @@ function IconCardWithForwardRef<As extends ElementType>(
})
: children}
</div>
{iconRight && React.cloneElement(icon, {
...icon.props,
className: classNames(
'ffe-icon-card__icon',
icon.props.className,
),
})}
</>
)}
</WithCardAction>
Expand Down

0 comments on commit 636ebd3

Please sign in to comment.