Created
March 9, 2019 14:27
-
-
Save DRFR0ST/215241fdc169eff430b3aade04997755 to your computer and use it in GitHub Desktop.
Flexbox wrapper for React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { PureComponent } from "react"; | |
import { View } from "react-native"; | |
import PropTypes from "prop-types"; | |
/** | |
* @class | |
* @description Simple Flexbox wrapper. | |
* @author Mike Eling <mike.eling97@gmail.com> | |
*/ | |
class FlexWrapper extends PureComponent { | |
static styleProps = { | |
justifyContent: PropTypes.oneOf([ | |
"flex-start", | |
"flex-end", | |
"center", | |
"space-between", | |
"space-around", | |
"space-evenly", | |
]), | |
alignItems: PropTypes.oneOf([ | |
"strech", | |
"flex-start", | |
"flex-end", | |
"center", | |
"baseline", | |
]), | |
alignContent: PropTypes.oneOf([ | |
"flex-start", | |
"flex-end", | |
"center", | |
"space-between", | |
"space-around", | |
"stretch", | |
]), | |
flexDirection: PropTypes.oneOf([ | |
"row", | |
"row-reverse", | |
"column", | |
"column-reverse", | |
]), | |
flexWrap: PropTypes.oneOf(["nowrap", "wrap", "wrap-reverse"]), | |
flexFlow: PropTypes.string, | |
width: PropTypes.number, | |
height: PropTypes.number, | |
}; | |
static propTypes = { | |
...FlexWrapper.styleProps, | |
}; | |
static defaultProps = { | |
justifyContent: "space-between", | |
flexDirection: "row" | |
}; | |
/** | |
* @description Exclude props which have been already used. | |
* @returns {object} | |
*/ | |
filterProps() { | |
const propTypes = Object.keys(FlexWrapper.propTypes), | |
props = { ...this.props }; | |
// Delete all props defined in PropTypes. | |
for (var i = 0; i < propTypes.length; i++) delete props[propTypes[i]]; | |
return props; | |
} | |
/** | |
* @description Include props which contain flex container style. | |
* @returns {object} | |
*/ | |
composeStyle = () => { | |
let styles = { ...this.props.style, display: "flex" }; // Initial style. | |
const styleProps = Object.keys(FlexWrapper.styleProps); | |
// Adds props to style. | |
for (var i = 0; i < styleProps.length; i++) | |
if (this.props[styleProps[i]]) | |
styles[styleProps[i]] = this.props[styleProps[i]]; | |
return styles; | |
}; | |
render() { | |
return ( | |
<View style={this.composeStyle()} {...this.filterProps()}> | |
{this.props.children} | |
</View> | |
); | |
} | |
} | |
export default FlexWrapper; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment