Skip to content

Commit

Permalink
feat(ffe-cards-react): rewrite in ts
Browse files Browse the repository at this point in the history
BREAKING CHANGE: different props

element -> as
  • Loading branch information
Peter Hellstrand committed May 6, 2024
1 parent 25ce15b commit f13e402
Show file tree
Hide file tree
Showing 36 changed files with 518 additions and 437 deletions.
4 changes: 2 additions & 2 deletions packages/ffe-cards-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"url": "ssh://[email protected]:SpareBank1/designsystem.git"
},
"scripts": {
"build": "ffe-buildtool babel",
"watch": "ffe-buildtool babel-watch",
"build": "ffe-buildtool tsc",
"watch": "ffe-buildtool tsc-watch",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"test": "ffe-buildtool jest",
Expand Down
25 changes: 0 additions & 25 deletions packages/ffe-cards-react/src/CardBase.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import CardBase from './CardBase';
import { CardBase } from './CardBase';
import { render, screen, within } from '@testing-library/react';

describe('CardBase', () => {
Expand All @@ -15,8 +15,19 @@ describe('CardBase', () => {
});

it('should render my custom element and custom class', () => {
render(<CardBase element="article" className="my-custom-class" />);
render(<CardBase as="article" className="my-custom-class" />);
const article = screen.getByRole('article');
expect(article.classList.contains('my-custom-class')).toBeTruthy();
});

it('should set ref', () => {
const ref = React.createRef<HTMLAnchorElement>();
render(
<CardBase href="#" ref={ref}>
<div>Hello world</div>
</CardBase>,
);
const link = screen.getByRole('link');
expect(link).toBe(ref.current);
});
});
24 changes: 24 additions & 0 deletions packages/ffe-cards-react/src/CardBase.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { ElementType, ForwardedRef } from 'react';
import { fixedForwardRef } from './fixedForwardRef';
import { ComponentAsPropParams } from './types';
import classNames from 'classnames';

export type CardBaseProps<As extends ElementType = 'a'> =
ComponentAsPropParams<As>;

function CardBaseWithForwardRef<As extends ElementType>(
props: CardBaseProps<As>,
ref: ForwardedRef<any>,
) {
const { as: Comp = 'a', className, ...rest } = props;

return (
<Comp
className={classNames('ffe-card-base', className)}
{...rest}
ref={ref}
/>
);
}

export const CardBase = fixedForwardRef(CardBaseWithForwardRef);
54 changes: 0 additions & 54 deletions packages/ffe-cards-react/src/IconCard/IconCard.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import IconCard from './IconCard';
import { IconCard } from './IconCard';
import { Icon } from '@sb1/ffe-icons-react';
import { render, screen } from '@testing-library/react';

Expand Down Expand Up @@ -40,8 +40,8 @@ describe('IconCard', () => {
);
const link = screen.getByRole('link');
const icon = link.querySelector('.ffe-icons');
expect(icon.classList.contains('ffe-icon-card__icon')).toBe(true);
expect(icon.classList.contains('my-custom-class')).toBe(true);
expect(icon?.classList.contains('ffe-icon-card__icon')).toBe(true);
expect(icon?.classList.contains('my-custom-class')).toBe(true);
});

it('should add modifying classes when modifiers are given', () => {
Expand Down Expand Up @@ -73,8 +73,8 @@ describe('IconCard', () => {
);
const link = screen.getByRole('link');
const p = link.querySelector('p');
expect(p.classList.contains('ffe-card-body__text')).toBeTruthy();
expect(p.textContent).toEqual('Hello world');
expect(p?.classList.contains('ffe-card-body__text')).toBeTruthy();
expect(p?.textContent).toEqual('Hello world');
});

it('should render my custom class', () => {
Expand All @@ -91,4 +91,19 @@ describe('IconCard', () => {
expect(link.classList.contains('ffe-icon-card')).toBeTruthy();
expect(link.classList.contains('my-custom-class')).toBeTruthy();
});

it('should set ref', () => {
const ref = React.createRef<HTMLAnchorElement>();
render(
<IconCard
href="#"
icon={<Icon fileUrl={savingsIconXlarge} size="xl" />}
ref={ref}
>
{children}
</IconCard>,
);
const link = screen.getByRole('link');
expect(link).toBe(ref.current);
});
});
59 changes: 59 additions & 0 deletions packages/ffe-cards-react/src/IconCard/IconCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { ElementType, ForwardedRef, ReactElement } from 'react';
import { fixedForwardRef } from '../fixedForwardRef';
import { ComponentAsPropParams, CardRenderProps } from '../types';
import classNames from 'classnames';
import { Text, Subtext, Title, CardName } from '../components';

export type IconCardProps<As extends ElementType = 'a'> = Omit<
ComponentAsPropParams<As>,
'children'
> & {
/** Element of icon */
icon: ReactElement;
/** Smaller icon and less space */
condensed?: boolean;
children:
| React.ReactNode
| ((cardRenderProps: CardRenderProps) => React.ReactNode);
};

function IconCardWithForwardRef<As extends ElementType>(
props: IconCardProps<As>,
ref: ForwardedRef<any>,
) {
const {
as: Comp = 'a',
className,
condensed,
icon,
children,
...rest
} = props;

return (
<Comp
className={classNames(
'ffe-icon-card',
{ 'ffe-icon-card--condensed': condensed },
className,
)}
{...rest}
ref={ref}
>
{React.cloneElement(icon, {
...icon.props,
className: classNames(
'ffe-icon-card__icon',
icon.props.className,
),
})}
<div className="ffe-icon-card__body">
{typeof children === 'function'
? children({ Text, Subtext, Title, CardName })
: children}
</div>
</Comp>
);
}

export const IconCard = fixedForwardRef(IconCardWithForwardRef);
48 changes: 0 additions & 48 deletions packages/ffe-cards-react/src/ImageCard/ImageCard.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import ImageCard from './ImageCard';
import { ImageCard } from './ImageCard';
import { render, screen, within } from '@testing-library/react';

const children = <div>Hello world</div>;
Expand Down Expand Up @@ -35,9 +35,9 @@ describe('ImageCard', () => {
const link = screen.getByRole('link');
const imageEl = link.querySelector('.ffe-image-card__image-container');
expect(
imageEl.querySelector('.ffe-image-card__image-overlay'),
imageEl?.querySelector('.ffe-image-card__image-overlay'),
).toBeTruthy();
expect(imageEl.querySelector('.ffe-image-card__image')).toBeTruthy();
expect(imageEl?.querySelector('.ffe-image-card__image')).toBeTruthy();
});

it('should set alt text on image correctly', () => {
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('ImageCard', () => {
);
const link = screen.getByRole('link');
const body = link.querySelector('.ffe-image-card__body');
expect(body.textContent).toBe('Hello world');
expect(body?.textContent).toBe('Hello world');
});

it('should render children as a function', () => {
Expand All @@ -82,8 +82,8 @@ describe('ImageCard', () => {
);
const link = screen.getByRole('link');
const p = link.querySelector('p');
expect(p.classList.contains('ffe-card-body__text')).toBeTruthy();
expect(p.textContent).toEqual('Hello world');
expect(p?.classList.contains('ffe-card-body__text')).toBeTruthy();
expect(p?.textContent).toEqual('Hello world');
});

it('should render my custom class', () => {
Expand All @@ -101,4 +101,20 @@ describe('ImageCard', () => {
expect(link.classList.contains('ffe-image-card')).toBeTruthy();
expect(link.classList.contains('my-custom-class')).toBeTruthy();
});

it('should set ref', () => {
const ref = React.createRef<HTMLAnchorElement>();
render(
<ImageCard
href="#"
imageAltText="Image alt text"
imageSrc="random/path"
ref={ref}
>
{children}
</ImageCard>,
);
const link = screen.getByRole('link');
expect(link).toBe(ref.current);
});
});
Loading

0 comments on commit f13e402

Please sign in to comment.