1
0
mirror of https://github.com/yldio/copilot.git synced 2025-01-23 01:10:12 +02:00
copilot/ui/src/components/row/index.js

95 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-10-24 23:58:09 +03:00
/*
* based on
* https://github.com/roylee0704/react-flexbox-grid/blob/master/src/components/Row.js
*/
const flatten = require('lodash.flatten');
const classNames = require('classnames');
const React = require('react');
const styles = require('./style.css');
const breakpoints = [
'xs',
'sm',
'md',
'lg'
];
const modifiers = [
'start',
'center',
'end',
'top',
'middle',
'bottom',
'around',
'between',
'first',
'last'
];
const getClasses = (props) => {
return flatten(modifiers.map((name) => {
const value = props[name];
if (!value) {
return;
}
const bps = (() => {
if (value === true) {
return breakpoints;
2016-10-24 23:58:09 +03:00
}
if (Array.isArray(value)) {
return value;
}
return [value];
})();
return flatten(bps.map(bp => styles[`${name}-${bp}`]));
})).filter(Boolean);
};
2016-10-25 22:15:33 +03:00
const Row = (props) => {
2016-10-24 23:58:09 +03:00
const {
className,
reverse,
children,
style
} = props;
const cn = classNames(
className,
styles.row,
reverse ? styles.reverse : '',
...getClasses(props)
);
return (
2016-10-25 22:15:33 +03:00
<div className={cn} style={style}>
2016-10-24 23:58:09 +03:00
{children}
</div>
);
};
const ModificatorType = React.PropTypes.oneOfType([
React.PropTypes.bool,
React.PropTypes.arrayOf(React.PropTypes.oneOf(breakpoints)),
React.PropTypes.oneOf(breakpoints)
]);
Row.propTypes = {
2016-10-25 22:15:33 +03:00
children: React.PropTypes.node,
2016-10-24 23:58:09 +03:00
className: React.PropTypes.string,
2016-10-25 22:15:33 +03:00
reverse: React.PropTypes.bool,
2016-10-24 23:58:09 +03:00
style: React.PropTypes.object,
...modifiers.reduce((all, m) => ({
...all,
[m]: ModificatorType
}), {})
};
2016-10-25 22:15:33 +03:00
module.exports = Row;