How to display cols next to each other after mapping in Reactjs? - reactjs

I have this code that maps and displays my website's posts from an array.
import articleContent from '../data/articleContent';
const ArticlesList = () => (
<>
<ArticlesStyle>
<h1 className="article-h1">Nasze artykuły</h1>
{articleContent.map((article, key) =>
<Link className="article-link" key={key} to={`/artykul/${article.name}`}>
<Row className="align-items-center text-center">
<Col xs={4} md={5}>
<h2 style={{ color: '#14B2E6' }}>{article.title}</h2>
</Col>
</Row>
<Row className="align-items-center text-center">
<Col className="text-left" xs={8} md={5}>
<p style={{ color: 'black' }}>{article.content[0].substring(0, 150)}...</p>
</Col>
</Row>
</Link>
)}
</ArticlesStyle>
</>
)
My goal is to automatically display two posts in one row (so two columns in a row I guess) on PC display and one post in a row on mobile. I tried to make some small adjustments to make it work, but I am clueless now.
This is how it looks now on PC, I can't get rid of this marked area - I think it's an issue related to display: flex;.

You can use CSS-grid for this. For web make the number of column 2 and then by using media queries in CSS you can make the number of column 1 in case of mobile width.

Ok, so I managed to solve my own problem.
First of all I added outer (outer to .map function) Row and Col. I wrapped everything inside of .map function into Container.
const ArticlesList = () => (
<>
<ArticlesStyle>
<Row>
<Col className="article-list-grid">
{articleContent.map((article, key) =>
<Container className="wrapper">
<Link className="article-link" key={key} to={`/artykul/${article.name}`}>
<Row className="align-items-center text-center">
<Col xs={true} md={true}>
<h2 style={{ color: '#14B2E6' }}>{article.title}</h2>
</Col>
</Row>
<Row className="align-items-center text-center">
<Col xs={true} md={true}>
<p style={{ color: 'black' }}>{article.content[0].substring(0, 150)}...</p>
</Col>
</Row>
</Link>
</Container>
)}
</Col>
</Row>
</ArticlesStyle>
</>
)
After that everything comes to styling which looks like this:
const ArticlesStyle = styled.div`
.article-list-grid {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.wrapper {
#media (max-width: 600px) {
flex: 100%;
}
#media (min-width: 900px) {
flex: 50%;
}
}
.article-link {
text-decoration: none;
}
`;
The most important thing here is flex: 50% on PC screens which result with 2 columns in a row. Everything looks as I wanted right now:
PC
MOBILE

Related

How to center content inside Row element

I am trying to center some cards inside a row element but I cant get it to work!
<Row className="d-flex justify-content-center">
{cardData.map((data) => {
return (
<Col>
<Card style={{ width: "18rem" }}>
<Card.Img variant="top" src={data.img} />
<Card.Body>
<Card.Title>{data.title}</Card.Title>
<Card.Text>{data.text}</Card.Text>
<Button variant="primary">{data.button}</Button>
</Card.Body>
</Card>
</Col>
);
})}
</Row>
this is how it looks:: (as you can see, there is un-event spacing on the left and right of the card
I tried to put the following classes (mx-auto,d-flex justify-content-center) on the parent row but I cant get it to work
any advice is greatly appreciated
use justify-content: space-between
<Row style={{display:'flex', justifyContent: 'space-between'}}>

How can i delete width in react - bootstrap class?

I'm doing an internet-store, and i have a problem with top panel. I tried everything to fix it, but only when i change class row in dev tools i can get result.
row>* {
flex-shrink: 0;
width: 100%;
max-width: 100%;
padding-right: calc(var(--bs-gutter-x) * .5);
padding-left: calc(var(--bs-gutter-x) * .5);
margin-top: var(--bs-gutter-y);
}
i need to delete width from this class, but i don't know how to do it. If u can help me, it will be cool.
oh, if i replace component for nothing change.
<Container>
<Row className="mt-2">
<Col md={3}>
<TypeBar />
</Col>
<Col md={9} >
<BrandBar />
<DeviceList />
</Col>
</Row>
</Container>
const BrandBar = observer(() => {
const {device} = useContext(Context);
return (
<Row className="d-flex">
{device.brands.map(brand =>
<Card
style={{cursor: 'pointer'}}
key={brand.id}
className='p-2 align-items-center'
onClick={() => device.setSelectedBrand(brand)}
border={brand.id === device.selectedBrand.id ? 'danger' : 'light'}
>
{brand.name}
</Card>
)}
</Row>
)
})
When using bootstrap's containers always follows the Container > Row > Col order.
The class row>* is intended to select the cols, but instead is selecting your card.
Try doing like so
const BrandBar = observer(() => {
const { device } = useContext(Context);
return (
//Add a Container here
<Container>
<Row className="d-flex">
{device.brands.map((brand) => (
//Add a Col here
<Col>
<Card
style={{ cursor: 'pointer' }}
key={brand.id}
className="p-2 align-items-center"
onClick={() => device.setSelectedBrand(brand)}
border={brand.id === device.selectedBrand.id ? 'danger' : 'light'}
>
{brand.name}
</Card>
</Col>
))}
</Row>
</Container>
);
});

react-bootstrap <Row><Col lg='3'></Col></Row> remove default padding - React JS

In a react app I want to remove padding from both left and right sides of each column. I guess this padding of 15px is being given to each column by react-bootstrap because what I saw is as:
padding-right: 15px;
padding-left: 15px;
which is not removable(I tried to do so but failed). My images on a page are showing as below:
Note: showing color Green is padding from both left and right side applied to each column which I want to remove.
My desired output for images on a page is something like as below:
My code:
.js file
import React from 'react'
import './style.scss';
import { Row, Col } from 'react-bootstrap'
import { Images } from '../../../../Shared/Assets';
import ImagesIcon from '../../../../Components/Cells/ImagesIcon'
const ImgGallery = () => {
return (
<>
<div className='ImgGalry'>
<Row>
<Col lg='3'>
<ImagesIcon src={Images.xgallery4} />
</Col>
<Col lg='3'>
<ImagesIcon src={Images.xgallery2} />
</Col>
<Col lg='3'>
<ImagesIcon src={Images.xgallery1} />
</Col>
<Col lg='3'>
<ImagesIcon src={Images.xgallery2} />
</Col>
</Row>
</div>
</>
);
}
export default ImgGallery;
.scss file
.ImgGalry{
overflow: hidden;
background-color: #01FF56;
img{
width: 100% !important;
height: 100% !important;
}
}
To all, is it possible to remove or hide padding of column(s) when created using react-bootstrap? Thanks.
add px-0 class to col
<Row className="no-gutters">
<Col lg='3' className="px-0">
<ImagesIcon src={Images.xgallery4} />
</Col>
<Col lg='3' className="px-0">
<ImagesIcon src={Images.xgallery2} />
</Col>
<Col lg='3' className="px-0">
<ImagesIcon src={Images.xgallery1} />
</Col>
<Col lg='3' className="px-0">
<ImagesIcon src={Images.xgallery2} />
</Col>
</Row>

How to align buttons in reactstrap?

I am building a web application in react and using reactstrap for certain ui design elements. I have arranged elements in row and columns. Everything else works fine but the button element is not getting aligned properly like the other elements in the row are. The button is slightly below than the other elements. Can someone tell me what i am doing wrong? Any help would be appreciated. Thank You.
App.js
<Container>
<Row>
<Col>
<text>{item1.Title}</text>
</Col>
<Col>
<input type="text" onChange={this.handleNameChange}/>
</Col>
<Col>
<Button onClick={()=>this.handleName(this.state.itemName,item1.Title,item.Title)} color="success" size="sm" >Save</Button>
</Col>
</Row>
</Container>
UPDATE
The button column seems to be having some sort of padding.
The textbox doesnt have any padding
give the 3rd Col a className and use the following css:
.class-col {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
height: 100%;
}
.main-container {
display: flex;
}
And you code:
<Container>
<Row className="main-container">
<Col className="class-col">
<text>{item1.Title}</text>
</Col>
<Col className="class-col">
<input type="text" onChange={this.handleNameChange}/>
</Col>
<Col className="class-col">
<Button onClick={()=>this.handleName(this.state.itemName,item1.Title,item.Title)} color="success" size="sm" >Save</Button>
</Col>
</Row>
</Container>
Give this a try.

React-bootstrap centering checkboxes in columns

I have a ListGroup with ListGroupItems that get populated by some data. Part of that data is a description, which obviously varies in length.
<ListGroup className="signal-info-container">
{
signalConstants[this.props.signalType].map((signal, i) => (
<ListGroupItem key={i}>
<Row>
<Col xs={3}>
{signal.name}
</Col>
<Col xs={5}>
{signal.description}
</Col>
<Col xs={3} className="aggregation">
<Checkbox inline={true}></Checkbox>
<Checkbox inline={true}></Checkbox>
<Checkbox inline={true}></Checkbox>
<Checkbox inline={true}></Checkbox>
</Col>
</Row>
</ListGroupItem>
))
}
</ListGroup>
Sometimes that description needs to wrap (as seen below).
What I would like to have happen is for the checkboxes in the right column vertically align in the center of the col.
I have tried to override react-bootstrap with some custom css. The following css results in the previous image.
.aggregation {
display: flex;
align-items: center;
}
Not sure how relevant it is, but the columns have a height of 0px. What confuses me is why the single-line descriptions are centered, but multi-line descriptions are not. Here is an image of the 0px cols:
I have noticed that there seems to be some sort of label applied to the checkbox which is getting centered by the applied CSS, but I need it applied on the actual checkbox instead. I have just removed the title attribute from my checkboxes. The effect is still the same (labels weren't showing up anyways).
You need to apply display: flex; align-items: center; to the entire <tr> so it recognizes the multi-line descriptions. So you should add a class to the <Row> that uses these rules.
Try modifying your component as so:
<ListGroup className="signal-info-container">
{
signalConstants[this.props.signalType].map((signal, i) => (
<ListGroupItem key={i}>
<Row>
<Col xs={3}>
{signal.name}
</Col>
<Col xs={5}>
{signal.description}
</Col>
<Col xs={3}>
<CheckboxRow>
<Checkbox title='min' inline={true}></Checkbox>
<Checkbox title='max' inline={true}></Checkbox>
<Checkbox title='avg' inline={true}></Checkbox>
<Checkbox title='sum' inline={true}></Checkbox>
</CheckboxRow>
</Col>
</Row>
</ListGroupItem>
))
}
</ListGroup>
Where CheckboxRow is
const CheckboxRow = ({children}) => (
<div style={
"display": "flex",
"justify-content": "center",
"align-items": "center"
}>
{children}
</div>
)
or extract that CSS out to a stylesheet if you have one.
Reason for adding the extra div layer is that it's safer than trying to override styles on the column components, since it's purpose is not to lay elements our horizontally.
The 'justify-content: center' centers the items horizontally, 'align-items: center' centers the items vertically.

Resources