Merge branch 'master' into 15088-merge-account
[arvados-workbench2.git] / src / components / details-attribute / details-attribute.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import Typography from '@material-ui/core/Typography';
7 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
8 import { ArvadosTheme } from '~/common/custom-theme';
9 import * as classnames from "classnames";
10 import { Link } from 'react-router-dom';
11
12 type CssRules = 'attribute' | 'label' | 'value' | 'lowercaseValue' | 'link';
13
14 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
15     attribute: {
16         display: 'flex',
17         alignItems: 'flex-start',
18         marginBottom: theme.spacing.unit
19     },
20     label: {
21         boxSizing: 'border-box',
22         color: theme.palette.grey["500"],
23         width: '40%'
24     },
25     value: {
26         boxSizing: 'border-box',
27         width: '60%',
28         display: 'flex',
29         alignItems: 'flex-start',
30         textTransform: 'capitalize'
31     },
32     lowercaseValue: {
33         textTransform: 'lowercase'
34     },
35     link: {
36         width: '60%',
37         color: theme.palette.primary.main,
38         textDecoration: 'none',
39         overflowWrap: 'break-word',
40         cursor: 'pointer'
41     }
42 });
43
44 interface DetailsAttributeDataProps {
45     label: string;
46     classLabel?: string;
47     value?: React.ReactNode;
48     classValue?: string;
49     lowercaseValue?: boolean;
50     link?: string;
51     children?: React.ReactNode;
52     onValueClick?: () => void;
53     linkInsideCard?: string;
54 }
55
56 type DetailsAttributeProps = DetailsAttributeDataProps & WithStyles<CssRules>;
57
58 export const DetailsAttribute = withStyles(styles)(
59     ({ label, link, value, children, classes, classLabel, classValue, lowercaseValue, onValueClick, linkInsideCard }: DetailsAttributeProps) =>
60         <Typography component="div" className={classes.attribute}>
61             <Typography component="span" className={classnames([classes.label, classLabel])}>{label}</Typography>
62             {link && <a href={link} className={classes.link} target='_blank'>{value}</a>}
63             {linkInsideCard && <Link to={`/collections/${linkInsideCard}`} className={classes.link}>{value}</Link>}
64             {!link && !linkInsideCard && <Typography
65                 onClick={onValueClick}
66                 component="span"
67                 className={classnames([classes.value, classValue, { [classes.lowercaseValue]: lowercaseValue }])}>
68                 {value}
69                 {children}
70             </Typography>
71             }
72
73         </Typography>
74 );