2019-09-26 18:07:11 +02:00
|
|
|
// @flow
|
|
|
|
import type { Node } from 'react';
|
2020-03-25 04:15:05 +01:00
|
|
|
import React, { useState } from 'react';
|
2019-09-26 18:07:11 +02:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import Icon from 'component/common/icon';
|
2020-03-25 04:15:05 +01:00
|
|
|
import Button from 'component/button';
|
|
|
|
import * as ICONS from 'constants/icons';
|
2019-09-26 18:07:11 +02:00
|
|
|
|
|
|
|
type Props = {
|
2019-09-27 20:56:15 +02:00
|
|
|
title?: string | Node,
|
2019-09-26 18:07:11 +02:00
|
|
|
subtitle?: string | Node,
|
2020-04-10 19:31:36 +02:00
|
|
|
titleActions?: string | Node,
|
2019-09-26 18:07:11 +02:00
|
|
|
body?: string | Node,
|
|
|
|
actions?: string | Node,
|
|
|
|
icon?: string,
|
2019-11-22 22:13:00 +01:00
|
|
|
className?: string,
|
2020-04-01 20:43:50 +02:00
|
|
|
isPageTitle?: boolean,
|
2020-05-21 17:38:28 +02:00
|
|
|
noTitleWrap?: boolean,
|
2020-04-29 21:31:11 +02:00
|
|
|
isBodyList?: boolean,
|
2020-03-25 04:15:05 +01:00
|
|
|
defaultExpand?: boolean,
|
2020-04-13 21:16:07 +02:00
|
|
|
nag?: Node,
|
2020-08-10 22:47:39 +02:00
|
|
|
smallTitle?: boolean,
|
2021-03-30 01:05:18 +02:00
|
|
|
onClick?: () => void,
|
2021-04-14 06:06:11 +02:00
|
|
|
children?: Node,
|
2019-09-26 18:07:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function Card(props: Props) {
|
2020-04-01 20:43:50 +02:00
|
|
|
const {
|
|
|
|
title,
|
|
|
|
subtitle,
|
2020-04-10 19:31:36 +02:00
|
|
|
titleActions,
|
2020-04-01 20:43:50 +02:00
|
|
|
body,
|
|
|
|
actions,
|
|
|
|
icon,
|
|
|
|
className,
|
|
|
|
isPageTitle = false,
|
2020-04-29 21:31:11 +02:00
|
|
|
isBodyList = false,
|
2020-05-21 17:38:28 +02:00
|
|
|
noTitleWrap = false,
|
2020-08-10 22:47:39 +02:00
|
|
|
smallTitle = false,
|
2020-03-25 04:15:05 +01:00
|
|
|
defaultExpand,
|
2020-04-13 21:16:07 +02:00
|
|
|
nag,
|
2021-03-30 01:05:18 +02:00
|
|
|
onClick,
|
|
|
|
children,
|
2020-04-01 20:43:50 +02:00
|
|
|
} = props;
|
2020-03-25 04:15:05 +01:00
|
|
|
const [expanded, setExpanded] = useState(defaultExpand);
|
|
|
|
const expandable = defaultExpand !== undefined;
|
|
|
|
|
2019-09-26 18:07:11 +02:00
|
|
|
return (
|
2021-03-30 01:05:18 +02:00
|
|
|
<section
|
2021-04-23 21:59:48 +02:00
|
|
|
role="button"
|
2021-03-30 01:05:18 +02:00
|
|
|
className={classnames(className, 'card')}
|
2021-04-23 21:59:48 +02:00
|
|
|
onClick={(e) => {
|
2021-03-30 01:05:18 +02:00
|
|
|
if (onClick) {
|
|
|
|
onClick();
|
2021-04-23 21:59:48 +02:00
|
|
|
e.stopPropagation();
|
2021-03-30 01:05:18 +02:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2020-02-13 19:04:50 +01:00
|
|
|
{(title || subtitle) && (
|
2020-05-21 17:38:28 +02:00
|
|
|
<div
|
|
|
|
className={classnames('card__header--between', {
|
|
|
|
'card__header--nowrap': noTitleWrap,
|
|
|
|
})}
|
|
|
|
>
|
2020-08-10 22:47:39 +02:00
|
|
|
<div
|
|
|
|
className={classnames('card__title-section', {
|
|
|
|
'card__title-section--body-list': isBodyList,
|
|
|
|
'card__title-section--small': smallTitle,
|
|
|
|
})}
|
|
|
|
>
|
2020-03-25 04:15:05 +01:00
|
|
|
{icon && <Icon sectionIcon icon={icon} />}
|
|
|
|
<div>
|
|
|
|
{isPageTitle && <h1 className="card__title">{title}</h1>}
|
2020-08-10 22:47:39 +02:00
|
|
|
{!isPageTitle && (
|
|
|
|
<h2 className={classnames('card__title', { 'card__title--small': smallTitle })}>{title}</h2>
|
|
|
|
)}
|
2020-03-25 04:15:05 +01:00
|
|
|
{subtitle && <div className="card__subtitle">{subtitle}</div>}
|
|
|
|
</div>
|
2019-09-26 18:07:11 +02:00
|
|
|
</div>
|
2020-04-10 19:31:36 +02:00
|
|
|
<div>
|
2021-04-08 17:21:45 +02:00
|
|
|
{titleActions && (
|
|
|
|
<div
|
|
|
|
className={classnames('card__title-actions', {
|
|
|
|
'card__title-actions--small': smallTitle,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{titleActions}
|
|
|
|
</div>
|
|
|
|
)}
|
2020-04-10 19:31:36 +02:00
|
|
|
{expandable && (
|
|
|
|
<div className="card__title-actions">
|
|
|
|
<Button
|
2020-09-30 20:46:17 +02:00
|
|
|
button="alt"
|
2020-04-30 17:42:40 +02:00
|
|
|
aria-expanded={expanded}
|
2020-10-02 06:31:54 +02:00
|
|
|
aria-label={expanded ? __('Less') : __('More')}
|
2020-04-15 16:12:54 +02:00
|
|
|
icon={expanded ? ICONS.SUBTRACT : ICONS.ADD}
|
2020-04-10 19:31:36 +02:00
|
|
|
onClick={() => setExpanded(!expanded)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2019-09-26 18:07:11 +02:00
|
|
|
</div>
|
2019-09-27 20:56:15 +02:00
|
|
|
)}
|
2020-03-25 04:15:05 +01:00
|
|
|
{(!expandable || (expandable && expanded)) && (
|
|
|
|
<>
|
|
|
|
{body && (
|
|
|
|
<div
|
|
|
|
className={classnames('card__body', {
|
|
|
|
'card__body--no-title': !title && !subtitle,
|
2020-04-29 21:31:11 +02:00
|
|
|
'card__body--list': isBodyList,
|
2020-03-25 04:15:05 +01:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
{body}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{actions && <div className="card__main-actions">{actions}</div>}
|
2021-03-30 01:05:18 +02:00
|
|
|
{children && <div className="card__main-actions">{children}</div>}
|
2020-03-25 04:15:05 +01:00
|
|
|
</>
|
2019-09-26 18:07:11 +02:00
|
|
|
)}
|
2020-04-13 21:16:07 +02:00
|
|
|
|
|
|
|
{nag}
|
2019-09-26 18:07:11 +02:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|