1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/frontend/src/component/common/index.js

201 lines
4.9 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import {
List,
MenuItem,
Icon,
ListItem,
ListItemText,
ListItemAvatar,
Button,
Avatar,
Typography,
} from '@material-ui/core';
import { Apps } from '@material-ui/icons';
import styles from './common.module.scss';
import ConditionallyRender from './ConditionallyRender/ConditionallyRender';
2016-12-09 22:11:05 +01:00
export { styles };
2016-12-09 22:11:05 +01:00
export const AppsLinkList = ({ apps }) => (
<List>
<ConditionallyRender
condition={apps.length > 0}
show={apps.map(({ appName, description, icon }) => (
<ListItem key={appName} className={styles.listItem}>
<ListItemAvatar>
<Avatar>
<ConditionallyRender
key={`avatar_conditional_${appName}`}
condition={icon}
show={<Icon>{icon}</Icon>}
elseShow={<Apps />}
/>
</Avatar>
</ListItemAvatar>
<ListItemText
primary={
<Link
to={`/applications/${appName}`}
className={[
styles.listLink,
styles.truncate,
].join(' ')}
>
{appName}
</Link>
}
secondary={description || 'No description'}
/>
</ListItem>
))}
/>
2016-12-09 22:11:05 +01:00
</List>
);
AppsLinkList.propTypes = {
apps: PropTypes.array.isRequired,
};
2016-12-09 22:11:05 +01:00
export const DataTableHeader = ({ title, actions }) => (
<div className={styles.dataTableHeader}>
<div className={styles.title}>
<Typography variant="h2" className={styles.titleText}>
{title}
</Typography>
</div>
{actions && <div className={styles.actions}>{actions}</div>}
</div>
);
DataTableHeader.propTypes = {
title: PropTypes.string,
actions: PropTypes.any,
};
export const FormButtons = ({
submitText = 'Create',
onCancel,
primaryButtonTestId,
}) => (
<div>
<Button
data-test={primaryButtonTestId}
type="submit"
color="primary"
variant="contained"
>
{submitText}
</Button>
&nbsp;
<Button type="cancel" onClick={onCancel}>
Cancel
</Button>
</div>
);
FormButtons.propTypes = {
submitText: PropTypes.string,
onCancel: PropTypes.func.isRequired,
primaryButtonTestId: PropTypes.string,
};
2016-12-10 14:02:41 +01:00
export const IconLink = ({ url, icon: IconComponent }) => (
<a
href={url}
target="_blank"
rel="noreferrer"
className="mdl-color-text--grey-600"
>
<IconComponent />
2016-12-10 15:10:03 +01:00
</a>
);
IconLink.propTypes = {
url: PropTypes.string,
icon: PropTypes.object,
};
2016-12-10 15:10:03 +01:00
export const DropdownButton = ({
label,
id,
className = styles.dropdownButton,
title,
icon,
startIcon,
style,
...rest
}) => (
<Button
id={id}
className={className}
title={title}
style={style}
{...rest}
startIcon={startIcon}
endIcon={<Icon>{icon}</Icon>}
>
{label}
</Button>
);
DropdownButton.propTypes = {
label: PropTypes.string,
style: PropTypes.object,
id: PropTypes.string,
2020-11-20 15:35:41 +01:00
title: PropTypes.string,
icon: PropTypes.object,
startIcon: PropTypes.object,
};
export const MenuItemWithIcon = React.forwardRef(
({ icon: IconComponent, label, disabled, ...menuItemProps }, ref) => (
<MenuItem
disabled={disabled}
style={{ display: 'flex', alignItems: 'center' }}
{...menuItemProps}
>
<IconComponent />
{label}
</MenuItem>
)
);
MenuItemWithIcon.propTypes = {
icon: PropTypes.object,
label: PropTypes.string,
disabled: PropTypes.bool,
};
const badNumbers = [NaN, Infinity, -Infinity];
export function calc(value, total, decimal) {
if (
typeof value !== 'number' ||
typeof total !== 'number' ||
typeof decimal !== 'number'
) {
return null;
}
if (total === 0) {
return 0;
}
badNumbers.forEach(number => {
if ([value, total, decimal].indexOf(number) > -1) {
return number;
}
});
return ((value / total) * 100).toFixed(decimal);
2017-02-23 22:18:23 +01:00
}
export const selectStyles = {
control: provided => ({
...provided,
border: '1px solid #607d8b',
boxShadow: '0',
':hover': {
borderColor: '#607d8b',
boxShadow: '0 0 0 1px #607d8b',
},
}),
};