Skip to content

Instantly share code, notes, and snippets.

@Landerson352
Created December 10, 2019 15:38
Show Gist options
  • Save Landerson352/f886123b9f2db786df7b6889d23826c8 to your computer and use it in GitHub Desktop.
Save Landerson352/f886123b9f2db786df7b6889d23826c8 to your computer and use it in GitHub Desktop.
React Native component for inline text and elements
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