Displaying React native listview items in equal chunks - css

I'm working on a eCommerce React Native project. I have a cart component which contains a Listview of all current items within the Cart. The main issue is the styling. I display each product according to quantiy - product name - price and delete button.
So something like this
As you can see the price text is not aligned equally and varies depending on product name. How can I edit the style so all text and buttons within the list view are equal length?

Each Text field can be assigned a flex value like this:
<Text style={{flex: 0.1}}>{product.quantity}x</Text>
<Text style={{padding: 10, flex:0.5}}>{product.name.toString()}</Text>
<Text style={{flex: 0.2}}>£{(product.quantity * product.price).toFixed(2)}</Text>
<TouchableOpacity style={{flex: 0.2}} onPress={() => { this.removeItem(product) }}>
The total of the 0.* should equal to 1.

Related

How can I insert an image into a <Text> React Native component?

<View style={{flexDirection: 'row', flexWrap: 'wrap'}}>
<Text>{'hello1234567890texthello1234567890texthello1234567890texthello1234567890texthello1234567890text'}</Text>
<Icon iconStyle={{ width: 15, height: 15 }} type={Icon.Type.copy} />
</View>
I have a multiline textbox, and I would like to display an image at the end of the textbox prompt, so when the user starts typing it stays on the end of the textbox value.
Text is multi-line.
I want make the icon follow text, as last word of text.

Material UI grid: how only space between elements on the inside, not outside

I ran into this issue where I have several Material UI Cards in a MUI Grid component. I would like the left side of this grid perfectly vertically aligned with the title above.
I tried putting a spacing={2} on the Grid container, but this causes that the padding of the Grid item also is on the left side, which makes it not aligned with the title above anymore. Is there a way to simply have this padding on the inside between the different Grid items?
<Box my={2}>
<Typography variant='h6'>
Hoeveel spaart u maandelijks tot uw pensioen?
</Typography>
</Box>
<Grid className={classes.scenarioBlock} container spacing={2}>
<Grid item xs={12} sm={6}>
<ScenarioOption selected {...monthlySavingGoal}>
<CustomSlider
onChange={
value =>
setAmounts(state => ({ ...state, monthlySavings: value }))
// should we check the scenario when they move the slider?
// if (!selected[name]) setSelected((state) => ({ ...state, [name]: true }));
}
color='primary'
value={amounts.monthlySavings}
min={monthlySavingGoal.sliderValues.min}
max={monthlySavingGoal.sliderValues.max}
step={monthlySavingGoal.sliderValues.step}
/>
</ScenarioOption>
</Grid></Grid>
As I recently ran into this problem as well, I want to share the solution I found.
In my case, the Grid container (and some other content, in your case the Box) were wrapped in an MUI Stack (for spacing purposes). This Stack was wrapped in an MUI Container (for horizontal centering). So my structure was:
<Container>
<Stack spacing={1}>
...some surrounding content that should be left-aligned
<Grid container spacing={2}>
...some Grid items
</Grid>
</Stack>
</Container>
I solved the indentation/misalignment by simply wrapping the Grid container in any other element, the most trivial example being a div element. Someone with more advanced MUI knowledge can possibly give a better explanation to why this makes the difference. So afterwards, my code looked like:
<Container>
<Stack spacing={1}>
...some surrounding content that is left-aligned to the Grid below
<div>
<Grid container spacing={2}>
...some Grid items
</Grid>
</div>
</Stack>
</Container>
And by that my Grid was (horizontally) aligned to the surrounding content. Hope this helps! If I stumble upon the explanation, I will add it to this answer.
By default material ui will add a gutter for the Grid item component, you just need to modify the grid component and add some style or class prop that removes the padding:
// Rest of the code
<Grid item xs={12} sm={6} style={{padding: 0}}>
</Grid>
You could also use a custom className using material UI makeStyles and just apply the class to the Grid item component.
const useStyles = makeStyles(() => ({
item: {
padding: 0,
},
}))
And in your component:
const classes = useStyles()
return (
{/* Rest of the code*/}
<Grid
item
xs={12}
sm={6}
classes={{
// Here's the override with your custom item class.
item: classes.item,
}}
>
</Grid>
)

ReactNative Layout Process & how flex impacts it?

I am trying to understand why the below layouts work differently.
Can someone explain how the internal layout process happen, in terms of rules? And why below examples behave differently. Except just saying that this is how it is.
Example1:
return (
<View style = {{borderWidth : 1, borderColor : 'red'}}>
<View style = {{borderWidth : 1, borderColor : 'black'}}>
<Text>TestText</Text>
</View>
</View>
)
This renders the red box around the black box around the Text. Which means that View resizes based on its content. inner view resizes to consider the text, outer view considers inner view(which already has considered inner text to determine its dimensions), hence everything is as expected.
Example2:
return (
<View style = {{borderWidth : 1, borderColor : 'red'}}>
<View style = {{flex:1,borderWidth : 1, borderColor : 'black'}}>
<Text>TestText</Text>
</View>
</View>
)
The text now comes outside both the Views. The inner view still rests inside the outer view though.
Now i tried a little different variation:
Example3:
return <View style = {{borderWidth:1, borderColor:'red', height:10}}>
<View style = {{flex:1, borderWidth:1, borderColor:'black'}}>
<Text>HelloWorld</Text>
</View>
</View>
This one renders a little better, i.e the text atleast seems to start inside both the views. But still not as expected.
Questions i have:
1) in case #2 above, Why does the text flow outside both the views? Whereas inner View still stays inside outer?
Can some content ever go beyond the bounds of its parent View? Shouldn't the parent View expand to contain it, as long as we haven't provided specific dimensions to it?
2)In case #3, things behave a little bit as expected..Text is inside the views somewhat, but still overflows..
3)How does this rendering happen in ReactNative internally & how does it determine the effective heights of these views above?
Example with flex
flex: 1, obligates child to follow parent's height, "Black view" will fill all the space available of the "Red view", which is height:60.
<View style={{ borderColor: "red", height: 60 }}>
<View style={{ borderColor: "black", flex: 1 }}>
<Text>HelloWorld</Text>
...
</View>
</View>
Example without flex:1
Here without flex, "Black view" will occupy all space of its children, it won't matter height of the parent ("Red view").
<View style={{ borderColor: "red", height: 60 }}>
<View style={{ borderColor: "black" }}>
<Text>HelloWorld</Text>
...
</View>
</View>
The thing is in simple terms the outline misbehaves in case of 2nd one , i.e :
<View style = {{borderWidth : 1, borderColor : 'red'}}>
<View style = {{flex:1,borderWidth : 1, borderColor : 'black'}}>
<Text>TestText</Text>
</View>
</View>
Is because the parent View component doesnt have a flex property so it doesnt take up whole the space and since you have added flex:1 in the child View it tries to take up as whole space as its alloted , hence black border Dominates and TestText is below it.
But if you provide flex:1 , to parent View you will see the same structure as it was with no flex given at all. Try :
<View style = {{flex:1,borderWidth : 1, borderColor : 'red'}}>
<View style = {{flex:1,borderWidth : 1, borderColor : 'black'}}>
<Text>TestText</Text>
</View>
</View>
Hope it helps . feel free for doubts

react native set state to text value from flatlist

In my react native app I have a sqlite storage solution and on this page I select a record from my table and use a flatlist to display the value, which works perfectly:
<FlatList
data={this.state.FlatListActivityItems}
ItemSeparatorComponent={this.ListViewItemSeparator}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => (
<View key={item.id}>
<Text style={styles.textUnderlines}>{item.activity} <Text style={styles.recordsTypes}>
Minutes
</Text></Text>
</View>
)}
/>
The problem is, I need to use the same value of item.activity in another place in this page as a data object which currently uses state
this.state = {activity: ''}
Is there a way on page load that I can just take the value {item.activity} and set it to the activity state so that I can use it anywhere else in the page with the state call?
So where you are getting the data of flatlist, just there get the value and setstate it like below
vartemp = this.state.FlatListActivityItems[0].activity
this.setState({activity:vartemp})
I hope this helps....Thanks :)

How to put button over row In react-native?

I have text and image in one row. and now I want to put button over the row with exact size of row.
used flexDirection: 'row'
Any help?
You can wrap your text and image inside the TouchableOpacity component
<TouchableOpacity onPress={() => doYourThing()}>
<Text>{yourText}</Text>
<Image source={{}} />
</TouchableOpacity>
If the text and image are already wrapped in a View, you can just replace the View component with the TouchableOpacity component. It takes all the props which View takes.

Resources