I am trying to render a card and map each card as a row of three (even better if this number is altered responsively).
I'm unsure as to which lines of code are overriding SimpleGrid, and have tried to use Grid and Flex. I do not have any CSS either.
I have attached the code that I am rendering.
return (
<>
{data.map((item: ICard, index: number) => (
<SimpleGrid columns={3} minChildWidth="30%" py={6} ml={10} key={index}>
<Box
maxW={"445px"}
w={"full"}
boxShadow={"2xl"}
rounded={"md"}
p={6}
overflow={"hidden"}
>
<Box
h={"310px"}
bg={"gray.100"}
mt={-6}
mx={-6}
mb={6}
pos={"relative"}
>
<div
style={{
height: 310,
width: 445,
backgroundImage: `url(${
item.edmIsShownBy ?? item.edmPreview
})`,
backgroundSize: "cover",
backgroundPosition: "middle",
}}
></div>
</Box>
<Stack>
<Text
color={"green.500"}
textTransform={"uppercase"}
fontWeight={800}
fontSize={"sm"}
letterSpacing={1.1}
>
{item.dcCreator ?? searchTerm ?? "Unknown Artist"} {item.year}
</Text>
<Heading fontSize={"2xl"} fontFamily={"body"}>
{item.title}
</Heading>
<Text color={"gray.500"} noOfLines={4}>
{item.dcDescription ?? item.dcDescriptionLangAware}
</Text>
</Stack>
<Stack mt={6} direction={"row"} spacing={4} align={"center"}>
<Avatar
src={item.edmPreview ?? item.edmIsShownBy}
alt={"Author"}
/>
<Stack direction={"column"} spacing={0} fontSize={"sm"}>
<Text fontWeight={600}>
{item.dataProvider}, {item.country}
</Text>
<Text color={"gray.500"} noOfLines={3} mx={1}>
Find {item.title ?? "this artwork"} at{" "}
{item.source ?? item.edmIsShownAt}
</Text>
</Stack>
</Stack>
</Box>
</SimpleGrid>
))}
</>
);
You need to take the SimpleGrid out of the data.map. Otherwise you are rendering a SimpleGrid for each record:
This is a quick test I run (forced the data to have something) ==>
import * as React from "react";
import {
Box,
SimpleGrid,
Stack,
Heading,
Text,
Avatar
} from "#chakra-ui/react";
export default function Example() {
const data: any = [
{ id: 1, title: "title1" },
{ id: 2, title: "title2" },
{ id: 3, title: "title3" }
];
return (
<>
<SimpleGrid columns={3} minChildWidth="30%" py={6} ml={10}>
{data.map((item: any, index: number) => {
return (
<Box
maxW={"445px"}
w={"full"}
boxShadow={"2xl"}
rounded={"md"}
p={6}
overflow={"hidden"}
>
<Box
h={"310px"}
bg={"gray.100"}
mt={-6}
mx={-6}
mb={6}
pos={"relative"}
>
<div
style={{
height: 310,
width: 445,
backgroundImage: "",
backgroundSize: "cover",
backgroundPosition: "middle"
}}
></div>
</Box>
<Stack>
<Text
color={"green.500"}
textTransform={"uppercase"}
fontWeight={800}
fontSize={"sm"}
letterSpacing={1.1}
>
{"Unknown Artist"} 2008
</Text>
<Heading fontSize={"2xl"} fontFamily={"body"}>
{item.title}
</Heading>
<Text color={"gray.500"} noOfLines={4}>
{item.dcDescription ?? item.dcDescriptionLangAware}
</Text>
</Stack>
<Stack mt={6} direction={"row"} spacing={4} align={"center"}>
<Avatar
src={item.edmPreview ?? item.edmIsShownBy}
alt={"Author"}
/>
<Stack direction={"column"} spacing={0} fontSize={"sm"}>
<Text fontWeight={600}>
{item.dataProvider}, {item.country}
</Text>
<Text color={"gray.500"} noOfLines={3} mx={1}>
Find {item.title ?? "this artwork"} at{" "}
{item.source ?? item.edmIsShownAt}
</Text>
</Stack>
</Stack>
</Box>
);
})}
</SimpleGrid>
</>
);
}
Related
I'm trying to add styling to child components of a React Native View. Since you cant use the > * selector in React Natives StyleSheet.create function how do you achieve this result?
Here is the code of a form I want to style:
<View style={styles.container}>
<View style={styles.centered}>
<Button mode="contained" onPress={onAddItem} style={styles.default}>
Add Item
</Button>
</View>
{showForm ? (
<View style={styles.form}>
<TextInput
label="Title"
returnKeyType="next"
theme={{ colors: { primary: 'blue',underlineColor:'transparent',}}}
style={{ width: 300 }}
value={title.value}
onChangeText={(text) => setTitle({ value: text, error: '' })}
error={!!title.error}
errorText={title.error}
autoCapitalize="none"
/>
<DropDownPicker
label="Select Type"
open={open}
value={value}
items={items}
setOpen={setOpen}
setValue={setValue}
setItems={setItems}
/>
<DropDownPicker
open={open2}
value={value2}
items={condition}
setOpen={setOpen2}
setValue={setValue2}
setItems={setCondition}
/>
<TextInput
label="Quantity"
returnKeyType="next"
theme={{ colors: { primary: 'blue',underlineColor:'transparent',}}}
style={{ width: 300 }}
value={quantity.value}
onChangeText={(text) => setQuantity({ value: text, error: '' })}
error={!!quantity.error}
errorText={quantity.error}
autoCapitalize="none"
/>
</View>
) : null}
</View>
I am using the MUI V5 in a react project and I am trying to center the logo in an AppBar on extra small screens but no mattter what I do nothing seems to be working. The Typography comonent is wrapped inside a ToolBar wrapped inside an AppBar that only appears on xs screens.
Here's my current code.
<Box sx={{ display: 'flex' }}>
<CssBaseline />
<AppBar
elevation={0}
position="fixed"
sx={{
display: { xs: 'flex', sm: 'none' },
width: { sm: `calc(100% - ${drawerWidth}px)` },
ml: { sm: `${drawerWidth}px` },
bgcolor: 'gray',
}}
>
<Toolbar >
<IconButton
color="inherit"
aria-label="open drawer"
edge="start"
onClick={handleDrawerToggle}
sx={{ mr: 2, display: { sm: 'none' } }}
>
<MenuIcon />
</IconButton>
<Typography
variant="h5"
noWrap
component="a"
href=""
sx={{
flexGrow: 1,
bgcolor: 'red',
display: { xs: 'flex', sm: 'none' },
flexGrow: 1,
fontFamily: 'monospace',
fontWeight: 700,
letterSpacing: '.3rem',
color: 'inherit',
textDecoration: 'none',
}}
>
LOGO
</Typography>
</Toolbar>
</AppBar>
I have throw everything I can at it but nothing seems to work. Any help is highly appreaciated.
Best Way to handle spacing with MUI is using Grid component.
Your code would be like this:
return (
<Box sx={{ display: "flex" }}>
<CssBaseline />
<AppBar
elevation={0}
position="fixed"
sx={{
display: { xs: "flex", sm: "none" },
width: { sm: `calc(100% - ${drawerWidth}px)` },
ml: { sm: `${drawerWidth}px` },
bgcolor: "gray",
}}
>
<Toolbar>
<Grid
container
spacing={1}
sx={{ alignItems: "center", justifyContent: "space-between" }}
>
<Grid item xs={4} sx={{ textAlign: "start" }}>
<IconButton
color="inherit"
aria-label="open drawer"
edge="start"
// onClick={handleDrawerToggle}
sx={{ mr: 2 }}
>
<MenuIcon />
</IconButton>
</Grid>
<Grid item xs sx={{ textAlign: "center" }}>
<Typography
variant="h5"
noWrap
component="a"
href=""
sx={{
bgcolor: "red",
fontFamily: "monospace",
fontWeight: 700,
letterSpacing: ".3rem",
color: "inherit",
textDecoration: "none",
}}
>
LOGO
</Typography>
</Grid>
<Grid item xs={4}></Grid>
</Grid>
</Toolbar>
</AppBar>
</Box>
);
MUI Grid docs
Not a big expert in MUI, but try defining the flex direction, then define the align items or justify content to be centre depending on the direction.
I'm trying to get space around my items in a horizontal flatlist, no matter how I try it doesn't seem to work:
const renderItem = ({item}) => {
return (
<View style={styles.item}>
<Image
source={{uri: content.uri}}
style={styles.image}
/>
<View>
<Text style={styles.contentTitle}>{content.title}</Text>
<Text style={styles.contentText}>{content.releaseDate}</Text>
</View>
</View>
)
}
<View style={styles.container}>
{header}
<View style={{width: '100%', justifyContent: 'space-around', backgroundColor: 'green'}}>
<FlatList
data={data}
pagingEnabled={true}
horizontal={true}
renderItem={renderItem}
keyExtractor={item => item.id}
ref={flatList}
/>
</View>
</View>
const styles = StyleSheet.create({
container: {
width: windowWidth,
marginTop: 15,
paddingBottom: 15,
borderBottomWidth: 1,
borderBottomColor: Colors.borderBlue,
backgroundColor: Colors.backgroundGenericTransparent,
},
item: {
width: windowWidth / 3.1,
backgroundColor: 'red',
},
image: {
width: '100%',
height: 220,
},
})
result:
As you can see the items are all together on the left even though I have added a space-around property on the container. Not show how I can resolve this.
use ItemSeparatorComponent
<FlatList
data={data}
pagingEnabled={true}
horizontal={true}
renderItem={renderItem}
keyExtractor={item => item.id}
ref={flatList}
ItemSeparatorComponent={() => {
return (
<View
style={{
height: "100%",
width: 10,
}} />
);
}}
/>
Use this prop on flatlist:
columnWrapperStyle={{ justifyContent: "space-between" }}
Not sure what you are trying to achieve.
Is that what you want?
snack here
Just add horizontal padding to your container. and no matter how big your items get you will always have spacing left and right. If the item gets too big you can start to scroll.
<View style={styles.messageGalleryContainer}>
<FlatList
data={files}
renderItem={renderImageGalleryItem}
keyExtractor={(item) => item?._id}
horizontal
showsHorizontalScrollIndicator={false}
ItemSeparatorComponent={() => <View style={{ width: 10 }} />} // add this line
contentContainerStyle={styles.messageGalleryItemContainer}
/>
</View>
You should use ItemSeparatorComponent with an empty component or with any element you want.
I have problem on css of react native, I don't know how can I implement 2 icon connect with dash, Really don't know what this called on react native, is this stepper?. I already have sample component, the main problem is how to achieve this dashes with icons.
Here is the the example:
My Actual Output:
Sample Code:
<View style={{ justifyContent: 'flex-end', flex: 1, padding: 20 }}>
<Card style={{ justifyContent: 'center', borderRadius: 20, elevation:4}}>
<CardItem>
<Left>
<Body>
<View style={{flexDirection:'row', padding:20}}>
<View style={{flexDirection:'row'}}>
</View>
<View style={{flexDirection:'column',flex:1}}>
<Text style={{alignSelf:'center', color:'#000', fontWeight:'bold', fontSize:17}} adjustsFontSizeToFit numberOfLines={2}>Pick Up Request {'\n'}</Text>
<Item inlineLabel last>
<Text style={{fontSize:14}}>US Rt 11, Evans Mills NY 13637. 901 Route 110 </Text>
</Item>
<Text>{'\n'}</Text>
<Item inlineLabel last>
<Text style={{fontSize:14}}>Farmingdale NY 11735. 2400 Route 9{'\n'}</Text>
</Item>
</View>
</View>
<View style={{flexDirection:'row'}}>
<View style={{flexDirection:'row',flex:1, justifyContent:'space-around'}}>
<Button small primary title="Message" >
<Text>Message</Text>
</Button>
<Button small warning title="Fetch" >
<Text>Fetch</Text>
</Button>
<Button small light title="Cancel" >
<Text>Cancel</Text>
</Button>
</View>
</View>
</Body>
</Left>
</CardItem>
</Card>
</View>
Working Example: Expo Snack
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import { Card, CardItem, Item, Left, Body, Button } from 'native-base';
export default function App() {
return (
<View style={styles.container}>
<View style={{ justifyContent: 'flex-end', flex: 1, padding: 20 }}>
<Card
style={{ justifyContent: 'center', borderRadius: 20, elevation: 4 }}>
<CardItem>
<Left>
<Body>
<View style={{ flexDirection: 'row', padding: 20 }}>
<View
style={{
flexDirection: 'column',
flex: 1,
justifyContent: 'space-between',
}}>
<View
style={{
position: 'absolute',
left: -25,
top: 50,
zIndex: 200,
}}>
<DotTrail />
</View>
<Text
style={{
alignSelf: 'center',
color: '#000',
fontWeight: 'bold',
fontSize: 17,
}}
adjustsFontSizeToFit
numberOfLines={2}>
Pick Up Request {'\n'}
</Text>
<Item inlineLabel last>
<Text style={{ fontSize: 14 }}>
US Rt 11, Evans Mills NY 13637. 901 Route 110{' '}
</Text>
</Item>
<Text>{'\n'}</Text>
<Item inlineLabel last>
<Text style={{ fontSize: 14 }}>
Farmingdale NY 11735. 2400 Route 9{'\n'}
</Text>
</Item>
</View>
</View>
<View style={{ flexDirection: 'row' }}>
<View
style={{
flexDirection: 'row',
flex: 1,
justifyContent: 'space-around',
}}>
<Button small primary title="Message">
<Text>Message</Text>
</Button>
<Button small warning title="Fetch">
<Text>Fetch</Text>
</Button>
<Button small light title="Cancel">
<Text>Cancel</Text>
</Button>
</View>
</View>
</Body>
</Left>
</CardItem>
</Card>
</View>
</View>
);
}
function DotTrail() {
return (
<>
<Dot radius={10} color={'green'} />
<View>
<Dot radius={3} color={'grey'} backgroundColor={'grey'} margin={5} />
<Dot radius={3} color={'grey'} backgroundColor={'grey'} margin={5} />
<Dot radius={3} color={'grey'} backgroundColor={'grey'} margin={5} />
</View>
<Dot radius={10} color={'red'} />
</>
);
}
function Dot({ color, radius, backgroundColor = null, margin }) {
return (
<View
style={{
width: radius * 2,
height: radius * 2,
borderWidth: 2,
borderColor: color,
borderRadius: radius,
marginRight: 10,
alignSelf: 'center',
backgroundColor: backgroundColor,
margin: margin,
}}></View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
marginBottom: 3,
},
});
I'll show you how u can make it from scratch:
Just replace your icon with these simple characters i did use here!
const MyCustomInputs = ({message, duration}) => {
const [showNotification, setShowNotification] = React.useState(false)
return (
<div style={{position:"relative",width:200, height:50, display:"flex", flexDirection:"column", justifyContent:"space-between"}}>
<div style={{width:200,display:"flex", justifyContent:"space-between"}}>
<MyInputIcon color="orange"/>
<input type="text"/>
</div>
<MyDashes content={"..."}/>
<div style={{width:200,display:"flex", justifyContent:"space-between"}}>
<MyInputIcon color="green"/>
<input type="text"/>
</div>
</div>
)
}
const MyInputIcon = ({color}) => <div style={{color}}>O</div>
const MyDashes = ({content}) => <div style={{position:"absolute",top:17,left:1,writingMode: "vertical-rl"}}>{content}</div>
ReactDOM.render(
<MyCustomInputs />
,document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react"></div>
I am trying to get the background image to take up the entire space on the screen
As you can see from the image this does not seem to be working. I think it may be something to do with the scroll view but unfortunately I cannot figure it out.
Ideally want to use flex box for the solution.
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<View style={styles.container}>
<ImageBackground
source={require('./src/assets/foodflixIMG.jpg')}
style={styles.image}>
<Text style={styles.text}>Inside</Text>
</ImageBackground>
</View>
</ScrollView>
</SafeAreaView>
</>
);
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: 'red',
height: '100%',
},
container: {
flex: 1,
flexDirection: 'column',
},
image: {
flex: 1,
resizeMode: 'cover',
justifyContent: 'center',
},
text: {
color: 'grey',
fontSize: 30,
fontWeight: 'bold',
}
});
export default App;
Check your code something like this:
<ImageBackground
source={require('./assets/main.jpg')}
style={{height:"100%",
width:"100%", flex:1}}
>
<SafeAreaView>
<ScrollView>
<View style={styles.container}>
<Text>Hello</Text>
</View>
</ScrollView>
</SafeAreaView>
</ImageBackground>
Hope this helps!