How can i split a column of list in different rows in bootstrap - reactjs

I am using bootstrap with reactjs. I am getting data from a list of the array, and that data should be display on screen but when I tried I get a list of data in the vertical direction. I want to make Only 4 element in a row and then it should make another row like buttons in the calculator. I tried to do that using bootstrap grid but it did not work.
This is my code. Here I mapped through and passed data to different component
PadButtons = this.props.currentPadBank.map((drumObj, i, padBankArr) => {
return (
<PadButton
key={padBankArr[i].id}
clipId={padBankArr[i].id}
clip={padBankArr[i].url}
keyTrigger={padBankArr[i].keyTrigger}
keyCode={padBankArr[i].keyCode}
/>
);
Then here I display the data
<div id="display">
<button
className="drum-pad btn btn-secondary btn-lg"
onClick={this.playSound}
id={this.props.clipId}
>
<audio id={this.props.keyTrigger} src={this.props.clip} />
{this.props.keyTrigger}
</button>
</div>
This is my codesandbox Link

Make the following changes
In Drumpad.js
<div className="container">
<div className="row">{PadButtons}</div>
</div>
In PadButton.js
<div id={`display-${this.props.keyTrigger}`} className="col-sm-3">
<button
className="drum-pad"
onClick={this.playSound}
id={this.props.clipId}>
<audio id={this.props.keyTrigger} src={this.props.clip} />
{this.props.keyTrigger}
</button>
</div>
See updated https://codesandbox.io/s/7ymd7
Basically, you had to add col-sm-3 class on the element that is being repeated and it must be inside a wrapper row div.

Related

How to add space between create button and a success message that will show at the bottom after creating?

In my form, I have the following JSX written with React and after I create with the button, the success message will touch the button at the bottom, leaving no space. Is there anyway to add that space between? Thanks again.
<button className="btn btn-primary">Create a hat</button>
</div >
</form >
<div className={successClass} id="success-message">
You have created a new hat!
</div>
I tried googling methods to add properties into the button and message but nothing seems to split them apart.
Try adding padding-top to the success message div.
<button className="btn btn-primary">Create a hat</button>
</div>
</form>
<div className={successClass} id="success-message"
style={{paddingTop :"2rem"}}
>
You have created a new hat!
</div>
`
</form>
<div className={successClass} id="success-message">
The best way to position elements is by use of CSS, avoid things like {' '}
Second, make your page structure clear for example
<div class="form-wrapper" style="padding: 10px; margin-bottom:4px;">
<form>
<!-- put your create button here -->
</form>
</div>
<div class="success-msg-wrapper" style="padding: 10px;">
<!-- put your message div here -->
</div>
Finally, make use of class-based styling instead of inline css for better maintainability of the code.

Iterate and render Cards from Center of Page Outward

I'm using ReactJS and iterating through an array and generating a bootstrap card for every object in the array but the cards start at the far left of the screen and they fill in left to right until they hit 5 and then creates a new row.
I would like to see if there is a way to make the first card, if there is only one, start in the center of the screen, and then populate outward as more cards get added.
Is something like that possible?
<div className='row mt-lg-3 row-cols-xs-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-3 row-cols-xl-5 g-4'>
{viewElements.map((element) => (
<ElementItem key={element._id} element={element} />
))}
</div>
This is how it currently looks when there are 5 cards, and it will start on the next row. I like how that looks but when there are fewer than 5 I want them to begin populating in the center of the page, not from left to right, like the second photo shows.
<section className='mt-3'>
{viewElements ? (
<div className='row mt-lg-3 row-cols-xs-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-3 row-cols-xl-5 g-4'>
{viewElements.map((element) => (
<ElementItem key={element._id} element={element} />
))}
</div>
) : (
<div className='container mt-5'>
<div className='content mt-5'>
<div className='row'>
<div className='row'>
<h3>No Elements To Display</h3>
</div>
</div>
</div>
</div>
)}
</section>
There is a way to arrange cards using react-bootstrap https://react-bootstrap.github.io/components/cards/#grid-cards You can play around with how many cards are in each row. You can also decide the number of rows and columns you want.

React Responsive Carousel returns all items in array on single carousel page

I am trying to utilize React Responsive Carousel. Currently, with my implementation, this code returns all objects in the array on the 1st page of the carousel (rending the point of a carousel useless). I'd like to have it so that each page of the carousel only shows one object in the array and you would need to utilize the "next" or "arrow" button via the carousel to view the next object in the array.
I tried to change profiles.map to profiles.forEach though this doesn't return any object at all.
I'm currently in a coding bootcamp, so forgive me if the code is bad altogether.
const { loading, data } = useQuery(QUERY_PROFILES);
const profiles = data?.profiles || [];
return (
<Carousel>
<div className="flex-row justify-space-between my-4">
{profiles &&
profiles.map((profile) => (
<div className="card col-12 col-xl-6" key={profile._id}>
<div className="card-body">
<h3 className="card-title">
{profile.firstName} {profile.lastName} <br />
<span className="mechLoc">
Location: {profile.location}
</span>
</h3>
<Link
className="seeProfileBtn btn btn-primary"
to={`/profiles/${profile._id}`}
>
See mechanic profile
</Link>
</div>
</div>
))}
</div>
</Carousel>
);
}

ReactJs Create Reusable component with multiple different children

I am new to React and am trying to get up to speed on a few basic concepts, trying to shoehorn it into an existing application (with the intention of slowly converting the entire app into React). So this may be a vary basic question.
I am converting my dialogs first (my app has a bunch of bootstrap modal dialogs), so I am using react-modal library to create these dialogs. What I would like to do is make the outer markup for this dialog reusable across all my dialogs. For instance, with this render method:
render() {
return(
<Modal
className="Modal__Bootstrap modal-dialog"
closeTimeoutMS={150}
isOpen={this.state.modalIsOpen}
onRequestClose={this.handleModalCloseRequest}
>
<div className="modal-content">
<div className="modal-header">
<button type="button" className="close" onClick={this.handleModalCloseRequest}>
<span aria-hidden="true">×</span>
<span className="sr-only">Close</span>
</button>
*******INSERT HEADER COMPONENT*******
</div>
<div className="modal-body">
*******INSERT BODY COMPONENT*******
</div>
<div className="modal-footer">
<button type="button" className="btn btn-default" onClick={this.handleModalCloseRequest}>Cancel</button>
*******INSERT ADDITIONAL FOOTER BUTTONS COMPONENT*******
</div>
</div>
</Modal>
);
}
I would like to have the lines that start with the ******** to be variable components that are appropriate (and different) for each dialog.
Obviously, for each dialog, I could just copy and paste this render method into that dialog's class and have hard coded components for that dialog. Something like (for the body):
<div className="modal-body">
<DialogABody />
</div>
and:
<div className="modal-body">
<DialogBBody />
</div>
But if I do that, I am then duplicating all the markup for the dialog "chrome" in react.
Essentially, I need some sort of dynamic component?
Is this possible?
on your modal react class you want to do this
class Modal extends React.Component {
render() {
return (
<div>
{this.props.header ? <HeaderComponent text={this.props.header} /> : null }
{this.props.children}
{this.props.footer ? <FooterComponent text={this.props.footer} /> : null }
</div>
}
}
then when calling this pass your props
<Modal
className="Modal__Bootstrap modal-dialog"
closeTimeoutMS={150}
isOpen={this.state.modalIsOpen}
onRequestClose={this.handleModalCloseRequest}
header="My Header"
footer="My Footer"
>
as for the body component part you should pass that as a prop to this dialog class when calling it.
instead of this:
<div className="modal-body">
<DialogABody />
</div>
you should have this:
<div className="modal-body">
{this.props.dialogComponent}
</div>
which gets passed through on the parent to this class
Call your model dialog with the wanted components as props.
<model header={headerComponent} body={bodyComponent}/>
In model render just put {this.props.header} and so on.
for one child component you could simply use this.props.children, however, as you have 3 components, you need to put them into separate props:
<DialogBody header={<div>header</div>} body={<div>body</div>} footer={<div>footer</div>}/>

How can I use angular ng-repeat to generate rows of 4?

I have a simple ng-repeat generating image divs. I want them to come out in rows of 4 instead of one long vertical list. How can I do this?
<div ng-repeat="artist in artists">
<div>
<h3> {({ artist.fields.title })} </h3>
<img src="{({ artist.fields.link })}" />
</div>
</div>
I believe you want to do something like this
<div style="width:800px;">
<div ng-repeat="artist in artists" style="width:200px;float:left;">
<div>
<h3> {({ artist.fields.title })} </h3>
<img src="{({ artist.fields.link })}" />
</div>
</div>
</div>
By having a container with a fixed width, and having each element inside be 1/4 of that width you can achieve what you want.
Of course, inline styles shouldn't be used in real code :)
You can use the $index property to add a tag every 4th item. Then use some CSS to display items as desired:
<div ng-repeat="artist in artists">
<div class="someClass">
<h3> {({ artist.fields.title })} </h3>
<img src="{({ artist.fields.link })}" />
</div>
<br ng-if="artist.$index % 4 == 0" />
</div>
One way is to use css properties. attach a class to the div with a float:left attribute, and insert a br tag after every 4th div
I guess you are using bootstrap?
If yes, this thread could do a help:
Bootstrap's grid columns number per row
doc for bootstrap grid layout:
http://getbootstrap.com/css/#grid-example-mixed

Resources