|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +const alignItems = (direction, horizontal, vertical) => { |
| 4 | + return (direction === 'row' && vertical) |
| 5 | + || (direction === 'column' && horizontal); |
| 6 | +}; |
| 7 | +const justifyContent = (direction, horizontal, vertical) => { |
| 8 | + return (direction === 'row' && horizontal) |
| 9 | + || (direction === 'column' && vertical); |
| 10 | +}; |
| 11 | + |
| 12 | +const CenterContainer = (props) => { |
| 13 | + const { style, direction, horizontal, vertical, children, ...other } = props; |
| 14 | + const containerStyle = { |
| 15 | + display: 'flex', |
| 16 | + flexDirection: props.direction, |
| 17 | + alignItems: alignItems(direction, horizontal, vertical) ? 'center' : 'stretch', |
| 18 | + justifyContent: justifyContent(direction, horizontal, vertical) ? 'center' : 'stretch', |
| 19 | + ...style, |
| 20 | + }; |
| 21 | + |
| 22 | + return ( |
| 23 | + <div style={containerStyle} {...other}> |
| 24 | + {children} |
| 25 | + </div> |
| 26 | + ); |
| 27 | +}; |
| 28 | +CenterContainer.propTypes = { |
| 29 | + children: React.PropTypes.node, |
| 30 | + style: React.PropTypes.object, |
| 31 | + direction: React.PropTypes.string, |
| 32 | + vertical: React.PropTypes.bool, |
| 33 | + horizontal: React.PropTypes.bool, |
| 34 | +}; |
| 35 | +CenterContainer.defaultProps = { |
| 36 | + style: {}, |
| 37 | + direction: 'row', |
| 38 | + vertical: true, |
| 39 | + horizontal: true, |
| 40 | +}; |
| 41 | + |
| 42 | + |
| 43 | +export default CenterContainer; |
0 commit comments