Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native bulleted lists using flex-wrap

Tags:

react-native

I'm trying to create a bulleted list that looks like this:

enter image description here

But I'm ending up with this instead:

enter image description here

React-native doesn't seem to like my use of nested flex boxes. But I'm not sure how to express the need for all 3 line elements (bullet, bolded text and normal text) to be displayed inline and to wrap to the next line when necessary.

Here is my react-native code:

var styles = StyleSheet.create({
    textWrapper: {
        flexWrap: 'wrap',
        alignItems: 'flex-start',
        flexDirection: 'row',
    },
    textBlock: {
        flexWrap: 'wrap',
        alignItems: 'flex-start',
        flexDirection: 'row'
        position: 'absolute',
        left: 10
    },
    boldText: {
        fontWeight: 'bold',
    },
    normalText: {
    }
});

<View style={ styles.textWrapper }>
    <Text>{'\u2022'}</Text>
    <View style={ styles.textBlock }>
        <Text style={ styles.boldText }>{categoryName + ':'}</Text>
        <Text style={ styles.normalText }>{value}</Text>
    </View>
</View>
like image 444
Wookiem Avatar asked Nov 05 '16 18:11

Wookiem


People also ask

How do you make flex wrap in React Native?

Below, we set a container's flexWrap property to wrap to wrap the boxes inside it into multiple lines: import React, { useState } from "react"; import { StyleSheet, View } from "react-native"; export default function App() { return ( <> <View style={styles.

How do you show bullets in React Native?

Try using Unicode. 2B24, 25CF, 26AB, etc... these are Unicode for black filled dots. USAGE: <Text>{'\u2B24'}</Text> . Save this answer.

What is flexGrow in React Native?

flexGrow describes how any space within a container should be distributed among its children along the main axis. After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.


3 Answers

Here's what I finally came up... I needed to put the bullet and bullet text into separate columns. I'm not sure why I had to specify a width for the column, but leaving width out or using flex: 1 didn't work for me.

Thanks to Nader for suggesting nested Text blocks, which formed part of the solution.

var styles = StyleSheet.create({
    column: {
        flexDirection: 'column',
        alignItems: 'flex-start',
        width: 200
    },
    row: {
        flexDirection: 'row',
        alignItems: 'flex-start',
        flexWrap: 'wrap',
        flex: 1
    },
    bullet: {
        width: 10
    },
    bulletText: {
        flex: 1
    },
    boldText: {
        fontWeight: 'bold'
    },
    normalText: {
    }
});

<View style={ styles.column }>
    <View style={ styles.row }>
        <View style={ styles.bullet }>
            <Text>{'\u2022' + " "}</Text>
        </View>
        <View style={ styles.bulletText }>
            <Text>
                <Text style={ styles.boldText }>{keyString + ": "}</Text>
                <Text style={ styles.normalText }>{textEntry}</Text>
            </Text>
        </View>
    </View>
</View>
like image 56
Wookiem Avatar answered Sep 29 '22 14:09

Wookiem


This is the version of accepted answer which works for me:

bullet(text: String): View {
    return(
      <View style={ styles.row }>
        <View style={ styles.bullet }>
          <Text>{'\u2022' + " "}</Text>
        </View>
        <View style={ styles.bulletText }>
          <Text>{text}</Text>
        </View>
      </View>
    );
}
const styles = StyleSheet.create({
    row: {
        flexDirection: 'row',
        alignItems: 'flex-start',
        flexWrap: 'wrap',
        flex: 1,
        marginVertical: 4
    },
    bullet: {
        width: 10
    },
    bulletText: {
        flex: 1
    }
});

And here is the result:

result

like image 36
Olcay Ertaş Avatar answered Sep 29 '22 13:09

Olcay Ertaş


Try wrapping it all in a single text block:

<View style={ styles.textBlock }>
    <Text>{'\u2022'}
      <Text style={ styles.boldText }>{categoryName + ':'}</Text>
      <Text style={ styles.normalText }>{value}</Text>
    </Text>
</View>
like image 44
Nader Dabit Avatar answered Sep 29 '22 13:09

Nader Dabit