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

144 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-12-09 22:11:05 +01:00
const React = require('react');
import styles from './common.scss';
const {
List, ListItem, ListItemContent,
Button, Icon,
Switch, MenuItem,
} = require('react-mdl');
2016-12-09 22:11:05 +01:00
const { Link } = require('react-router');
export { styles };
2016-12-10 15:39:03 +01:00
export const shorten = (str, len = 50) => (str && str.length > len ? `${str.substring(0, len)}...` : str);
2016-12-09 22:11:05 +01:00
export const AppsLinkList = ({ apps }) => (
<List>
2016-12-09 22:11:05 +01:00
{apps.length > 0 && apps.map(({ appName, description = '-', icon = 'apps' }) => (
<ListItem twoLine key={appName}>
2017-02-14 12:46:09 +01:00
<span className="mdl-list__item-primary-content">
<Icon name={icon} className="mdl-list__item-avatar"/>
<Link to={`/applications/${appName}`} className={[styles.listLink, styles.truncate].join(' ')}>
2016-12-09 22:11:05 +01:00
{appName}
<span className={['mdl-list__item-sub-title', styles.truncate].join(' ')}>{description}</span>
2016-12-09 22:11:05 +01:00
</Link>
2017-02-14 12:46:09 +01:00
</span>
2016-12-09 22:11:05 +01:00
</ListItem>
))}
</List>
);
export const HeaderTitle = ({ title, actions, subtitle }) => (
2016-12-10 12:21:27 +01:00
<div style={{ display: 'flex', borderBottom: '1px solid #f1f1f1', marginBottom: '10px', padding: '16px 20px ' }}>
2016-12-13 20:54:53 +01:00
<div style={{ flex: '2' }}>
2016-12-10 12:21:27 +01:00
<h6 style={{ margin: 0 }}>{title}</h6>
{subtitle && <small>{subtitle}</small>}
</div>
2016-12-13 19:56:52 +01:00
{actions && <div style={{ flex: '1', textAlign: 'right' }}>{actions}</div>}
2016-12-10 12:21:27 +01:00
</div>
);
export const DataTableHeader = ({ title, actions }) => (
<div className={styles.dataTableHeader}>
<div className={styles.title}>
<h2 className={styles.titleText}>{title}</h2>
</div>
{actions &&
<div className={styles.actions}>
{actions}
</div>
}
</div>
);
export const FormButtons = ({ submitText = 'Create', onCancel }) => (
<div>
<Button type="submit" ripple raised primary icon="add">
<Icon name="add" />&nbsp;&nbsp;&nbsp;
{ submitText }
</Button>
&nbsp;
<Button type="cancel" ripple raised onClick={onCancel} style={{ float: 'right' }}>
<Icon name="cancel" />&nbsp;&nbsp;&nbsp;
Cancel
</Button>
</div>
);
2016-12-10 12:21:27 +01:00
export const SwitchWithLabel = ({ onChange, checked, children, ...switchProps }) => (
<span className={styles.switchWithLabel}>
<span className={styles.label}>{children}</span>
<span className={styles.switch}>
<Switch checked={checked} onChange={onChange} {...switchProps} />
2016-12-10 12:21:27 +01:00
</span>
</span>
);
2016-12-10 13:49:22 +01:00
export const TogglesLinkList = ({ toggles }) => (
<List style={{ textAlign: 'left' }} className={styles.truncate}>
2016-12-10 13:49:22 +01:00
{toggles.length > 0 && toggles.map(({ name, description = '-', icon = 'toggle' }) => (
<ListItem twoLine key={name}>
<ListItemContent avatar={icon} subtitle={description}>
2017-01-01 17:34:06 +01:00
<Link key={name} to={`/features/view/${name}`}>
2016-12-10 13:49:22 +01:00
{name}
</Link>
</ListItemContent>
</ListItem>
))}
</List>
);
2016-12-10 14:02:41 +01:00
export function getIcon (type) {
switch (type) {
case 'feature-updated': return 'autorenew';
case 'feature-created': return 'add';
case 'feature-deleted': return 'remove';
case 'feature-archived': return 'archived';
default: return 'star';
}
};
2016-12-10 15:10:03 +01:00
export const IconLink = ({ url, icon }) => (
<a href={url} target="_blank" rel="noopener" className="mdl-color-text--grey-600">
<Icon name={icon}/>
2016-12-10 15:10:03 +01:00
</a>
);
export const DropdownButton = ({ label, id }) => (
<Button id={id} className={styles.dropdownButton}>
{label}
<Icon name="arrow_drop_down" className="mdl-color-text--grey-600"/>
</Button>
);
export const MenuItemWithIcon = ({ icon, label, disabled, ...menuItemProps }) => (
<MenuItem disabled={disabled} style={{ display: 'flex', alignItems: 'center' }} {...menuItemProps}>
<Icon name={icon} style={{ paddingRight: '16px' }}/>
{label}
</MenuItem>
);
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
}