From df409e195bcb8f7134cb1fe6928bf0a6721c98af Mon Sep 17 00:00:00 2001 From: Magnus Revheim Martinsen Date: Tue, 29 Oct 2024 15:07:35 +0100 Subject: [PATCH] make dumb card component --- .../Card/Card.module.css} | 0 src/app-components/Card/Card.tsx | 46 ++++++ src/layout/Cards/Cards.tsx | 151 +++++++----------- 3 files changed, 106 insertions(+), 91 deletions(-) rename src/{layout/Cards/Cards.module.css => app-components/Card/Card.module.css} (100%) create mode 100644 src/app-components/Card/Card.tsx diff --git a/src/layout/Cards/Cards.module.css b/src/app-components/Card/Card.module.css similarity index 100% rename from src/layout/Cards/Cards.module.css rename to src/app-components/Card/Card.module.css diff --git a/src/app-components/Card/Card.tsx b/src/app-components/Card/Card.tsx new file mode 100644 index 000000000..6c038e810 --- /dev/null +++ b/src/app-components/Card/Card.tsx @@ -0,0 +1,46 @@ +import React from 'react'; + +import { Card, Heading, Paragraph } from '@digdir/designsystemet-react'; +import Grid from '@material-ui/core/Grid'; + +import classes from 'src/app-components/Card/Card.module.css'; + +type AppCardProps = { + title?: React.ReactNode; + description?: React.ReactNode; + footer?: React.ReactNode; + media?: React.ReactNode; + mediaPosition?: 'top' | 'bottom'; + color?: Parameters[0]['color']; + children?: React.ReactNode; +}; + +export function AppCard({ title, description, footer, media, color, mediaPosition = 'top', children }: AppCardProps) { + return ( + + {media && mediaPosition === 'top' && {media}} + {(title || description) && ( + + {title && {title}} + {description && {description}} + + )} + {children && ( + + + {children} + + + )} + {footer && ( + + {footer} + + )} + {media && mediaPosition === 'bottom' && {media}} + + ); +} diff --git a/src/layout/Cards/Cards.tsx b/src/layout/Cards/Cards.tsx index 061af9163..5b6c1a79b 100644 --- a/src/layout/Cards/Cards.tsx +++ b/src/layout/Cards/Cards.tsx @@ -1,19 +1,16 @@ import React from 'react'; import type { CSSProperties } from 'react'; -import { Card, Heading, Paragraph } from '@digdir/designsystemet-react'; -import Grid from '@material-ui/core/Grid'; - +import { AppCard } from 'src/app-components/Card/Card'; +import { ConditionalWrapper } from 'src/components/ConditionalWrapper'; import { Lang } from 'src/features/language/Lang'; import { CardProvider } from 'src/layout/Cards/CardContext'; -import classes from 'src/layout/Cards/Cards.module.css'; import { ComponentStructureWrapper } from 'src/layout/ComponentStructureWrapper'; import { GenericComponent } from 'src/layout/GenericComponent'; import { useNodeItem } from 'src/utils/layout/useNodeItem'; import { typedBoolean } from 'src/utils/typing'; import type { PropsFromGenericComponent } from 'src/layout'; -import type { CardInternal } from 'src/layout/Cards/CardsPlugin'; -import type { LayoutNode } from 'src/utils/layout/LayoutNode'; +import type { BaseLayoutNode, LayoutNode } from 'src/utils/layout/LayoutNode'; type ICardsProps = PropsFromGenericComponent<'Cards'>; @@ -37,102 +34,74 @@ export const Cards = ({ node }: ICardsProps) => {
{cardsInternal.map((card, idx) => ( - } + description={card.description && } + footer={card.footer && } color={color} + mediaPosition={mediaPosition} + media={ + card.media && ( + + ) + } > - {mediaPosition === 'top' && ( - - )} - {(card.title || card.description) && ( - - {card.title && ( - - - - )} - {card.description && ( - - - - )} - - )} - {card.children && card.children.length > 0 && ( - - - - {card.children.filter(typedBoolean).map((childNode, idx) => ( - - ))} - - - - )} - {card.footer && ( - - - - - - )} - {mediaPosition === 'bottom' && ( - - )} - + {card?.children && + card.children?.length > 0 && + card.children?.filter(typedBoolean).map((childNode, idx) => ( + + ))} + ))}
); }; -interface MediaProps { - card: CardInternal; - node: LayoutNode<'Cards'>; - minMediaHeight: string | undefined; -} - -function Media({ card, node, minMediaHeight }: MediaProps) { - if (!card.media) { - return null; - } +type CardItemProps = { + node: LayoutNode; + parentNode: BaseLayoutNode<'Cards'>; + isMedia: boolean; + minMediaHeight?: string; +}; +function CardItem({ node, parentNode, isMedia, minMediaHeight }: CardItemProps) { return ( - - + ( +
+ {children} +
+ )} > -
- -
-
-
+ + + ); }