-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2823b6b
commit 2363da7
Showing
5 changed files
with
192 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,81 @@ | ||
import React, { ReactNode } from 'react' | ||
import clsx from 'clsx' | ||
|
||
import { IComponentBaseProps } from '../types' | ||
import { IComponentBaseProps, ComponentSize } from '../types' | ||
|
||
import CardActions, { CardActionsProps as ActionProps } from './CardActions' | ||
import CardBody, { CardBodyProps as BodyProps } from './CardBody' | ||
import CardTitle, { CardTitleProps as TitleProps } from './CardTitle' | ||
|
||
export type CardActionsProps = ActionProps | ||
export type CardBodyProps = BodyProps | ||
export type CardTitleProps = TitleProps | ||
|
||
export interface CardProps extends IComponentBaseProps { | ||
src?: string | ||
title?: string | ||
description?: string | ||
actions?: ReactNode | ReactNode[] | ||
children?: ReactNode | ||
bordered?: boolean | ||
compact?: boolean | ||
imageFull?: boolean | ||
|
||
// responsive props | ||
normal?: ComponentSize | boolean // Applies default paddings | ||
compact?: ComponentSize | boolean // Applies smaller padding | ||
side?: ComponentSize | boolean // The image in <figure> will be on to the side | ||
} | ||
|
||
interface ModifierMap { | ||
[key: string]: { | ||
[key: string]: string | undefined | ||
} | ||
} | ||
|
||
const DYNAMIC_MODIFIERS: ModifierMap = { | ||
compact: { | ||
true: 'card-compact', | ||
xs: 'xs:card-compact', | ||
sm: 'sm:card-compact', | ||
md: 'md:card-compact', | ||
lg: 'lg:card-compact', | ||
}, | ||
normal: { | ||
true: 'card-normal', | ||
xs: 'xs:card-normal', | ||
sm: 'sm:card-normal', | ||
md: 'md:card-normal', | ||
lg: 'lg:card-normal', | ||
}, | ||
side: { | ||
true: 'card-side', | ||
xs: 'xs:card-side', | ||
sm: 'sm:card-side', | ||
md: 'md:card-side', | ||
lg: 'lg:card-side', | ||
}, | ||
} | ||
|
||
const Card = ({ | ||
src, | ||
title, | ||
description, | ||
actions, | ||
bordered, | ||
compact, | ||
dataTheme, | ||
className, | ||
style, | ||
}: CardProps): JSX.Element => { | ||
const classes = clsx( | ||
'card', | ||
className, | ||
{ | ||
'bordered': bordered, | ||
'compact': compact, | ||
} | ||
) | ||
|
||
return ( | ||
<div | ||
data-theme={dataTheme} | ||
className={classes} | ||
style={style} | ||
> | ||
<div className="card-body"> | ||
<figure> | ||
<img src={src} /> | ||
</figure> | ||
<h2 className="card-title"> | ||
{title} | ||
</h2> | ||
<p>{description}</p> | ||
<div className="card-actions"> | ||
{actions} | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
bordered = true, | ||
imageFull, | ||
normal, | ||
compact, | ||
side, | ||
...props | ||
}: CardProps) => ( | ||
<div | ||
className={clsx('card', className, { | ||
'card-bordered': bordered, | ||
'image-full': imageFull, | ||
[(compact && DYNAMIC_MODIFIERS.compact[compact.toString()]) || '']: | ||
compact, | ||
[(normal && DYNAMIC_MODIFIERS.normal[normal.toString()]) || '']: normal, | ||
[(side && DYNAMIC_MODIFIERS.side[side.toString()]) || '']: side, | ||
})} | ||
{...props} | ||
/> | ||
) | ||
|
||
Card.Actions = CardActions | ||
Card.Body = CardBody | ||
Card.Title = CardTitle | ||
|
||
export default Card |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React, { ReactNode } from 'react' | ||
import clsx from 'clsx' | ||
import { IComponentBaseProps } from '../types' | ||
|
||
export interface CardActionsProps extends IComponentBaseProps { | ||
children?: ReactNode | ||
} | ||
|
||
const CardActions = ({ className, ...props }: CardActionsProps) => ( | ||
<div className={clsx('card-actions', className)} {...props} /> | ||
) | ||
|
||
export default CardActions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React, { ReactNode } from 'react' | ||
import clsx from 'clsx' | ||
import { IComponentBaseProps } from '../types' | ||
|
||
export interface CardBodyProps extends IComponentBaseProps { | ||
children?: ReactNode | ||
} | ||
|
||
const CardBody = ({ className, ...props }: CardBodyProps) => ( | ||
<div className={clsx('card-body', className)} {...props} /> | ||
) | ||
|
||
export default CardBody |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React, { ElementType, ReactNode } from 'react' | ||
import clsx from 'clsx' | ||
import { IComponentBaseProps } from '../types' | ||
|
||
export interface CardTitleProps extends IComponentBaseProps { | ||
children?: ReactNode | ||
tag?: ElementType | ||
} | ||
|
||
const CardTitle = ({ className, tag = 'div', ...props }: CardTitleProps) => { | ||
const Tag = tag | ||
|
||
return <Tag className={clsx('card-title', className)} {...props} /> | ||
} | ||
|
||
export default CardTitle |