Created
December 10, 2019 15:38
-
-
Save Landerson352/f886123b9f2db786df7b6889d23826c8 to your computer and use it in GitHub Desktop.
React Native component for inline text and elements
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 from 'react'; | |
import { View } from 'react-native'; | |
import { flatMap, map } from 'lodash'; | |
import { utils } from '../theme'; | |
import Text from './Text'; | |
const InlineTextAndElements = (props) => { | |
const { children, containerProps, ...restProps } = props; | |
const elements = flatMap(children, (child, i) => { | |
if (typeof child === 'string') { | |
const words = child.split(' '); | |
return map(words, (word, j) => ( | |
<Text {...restProps} key={`${i}_${j}`}> | |
{i > 0 && j === 0 && ' '}{word}{' '} | |
</Text> | |
)); | |
} | |
return child; | |
}); | |
return ( | |
<View style={[utils.row, utils.flexWrap, utils.flexEndItems]}> | |
{elements} | |
</View> | |
); | |
}; | |
export default InlineTextAndElements; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment