Passing an icon component to another component in ReactJS - reactjs

Lets say I have this CardHeader component and I want to receive a titleIcon props (which is a react component) like this:
export default function CardHeader({ title, cardContent, titleIcon }) {
return (
<S.MiddleContainer>
<div className="white-card">
<div className="campaign-title p-1">
<div className="d-flex justify-content-center align-items-center pt-1">
{titleIcon}
<span id="title">{title}</span>
</div>
<div className="pr-2">
<MdInfo color="#969FAA" size={28} />
</div>
</div>
<p id="border" />
<div className="campaign-information">
{cardContent}
</div>
</div>
</S.MiddleContainer>
This is where I pass the icon prop
import { MdInfo } from 'react-icons/md'
<Col md={3}>
<S.ProductContainer>
<CardHeader
title="Sponsors Channel"
icon="<MdInfo color="#969FAA" size={28} />"/>
<Card title="Mobile notification boost" />
</S.ProductContainer>
</Col>
What is the best way to approach this?

you can pass your icon as a React.Node (not as a string), like so :
<Col md={3}>
<S.ProductContainer>
<CardHeader
title="Sponsors Channel"
icon={<MdInfo color="#969FAA" size={28} />} />
<Card title="Mobile notification boost" />
</S.ProductContainer>
</Col>
And you will have access to it via props in CardHeader :
export default function CardHeader({ title, cardContent, icon }) {
return (
<S.MiddleContainer>
<div className="white-card">
<div className="campaign-title p-1">
<div className="d-flex justify-content-center align-items-center pt-1">
{icon}
<span id="title">{title}</span>
</div>
<div className="pr-2">
<MdInfo color="#969FAA" size={28} />
</div>
</div>
<p id="border" />
<div className="campaign-information">
{cardContent}
</div>
</div>

Here you are:
<CardHeader
title="Sponsors Channel"
titleIcon={<MdInfo color="#969FAA" size={28} />}
/>

Related

How do I get my Modal form to align centered of the screen without css?

I have read the react-bootstrap documentation and I am still running into a problem.
The intended goal is to have Modal form pop up when "New Project" buttton is clicked in the center pf the page by using the "centered" prop but it goes to the bottom left corner of the page.
New Project Button Unclicked New Project Button Clicked
Dasboard.js
import { DataTable } from "primereact/datatable";
import { Column } from "primereact/column";
import { Button } from "primereact/button";
// import ButtonDemo from './ButtonDemo';
import { Chart } from "primereact/chart";
import { InputText } from "primereact/inputtext";
import { InputTextarea } from "primereact/inputtextarea";
import { Modal, ModalFooter, ModalHeader } from "react-bootstrap";
// import ProjectsTable from "./Tables/ProjectsTable";
// import TicketsPieChart from "./Tables/TicketsPieChart"
// import API from
//project table
//eslint-disable no-unused-vars
const TableDemo = () => {
// const toggle = () => {setShow(!show);}
const [show, setShow] = useState(false);
const handleClose = () => setShow(false);
const handleShow = () => setShow(true);
return (
<>
<div className="grid table-demo">
<div className="col-12">
<div className="card">
<h5>Projects</h5>
<div>
<Button label="New Project" className="p-button-rounded mr-2 mb-2 npbutton" onClick={handleShow} />
</div>
<Modal show={show} onHide={handleClose} centered>
<div className="grid p-fluid">
<div className="col-12 md:col-6">
<div className="card">
<ModalHeader>
<h5>Projects</h5>
</ModalHeader>
<div className="grid formgrid">
<div className="col-12 mb-2 lg:col-4 lg:mb-0">
<InputText type="text" placeholder="Enter project name"></InputText>
</div>
</div>
<h5>Project Description</h5>
<InputTextarea placeholder="Enter project description" autoResize rows="3" cols="30" />
<ModalFooter>
<Button label="Submit" className="p-button-rounded p-button-success mr-2 mb-2" />
<Button onClick={handleClose}>Close</Button>
</ModalFooter>
</div>
</div>
</div>
</Modal>
<DataTable
// sortMode="single" sortField="representative.name"
sortOrder={1}
scrollable
scrollHeight="400px"
responsiveLayout="scroll"
>
<Column field="name" header="Name" style={{ minWidth: "200px" }}></Column>
<Column field="description" header="Description" style={{ minWidth: "200px" }}></Column>
<Column field="status" header="Status" style={{ minWidth: "200px" }}></Column>
</DataTable>
</div>
</div>
<div className="grid p-fluid">
<div className="col-12 lg:col-6">
<div className="card flex flex-column align-items-center">
<h5>Tickets by Type</h5>
<Chart type="pie" focus={"type"} />
</div>
</div>
</div>
<div className="grid p-fluid">
<div className="col-12 lg:col-6">
<div className="card flex flex-column align-items-center">
<h5>Tickets by Priority</h5>
<Chart type="pie" focus={"priority"} />
</div>
</div>
</div>
<div className="grid p-fluid">
<div className="col-12 lg:col-6">
<div className="card flex flex-column align-items-center">
<h5>Tickets by Status</h5>
<Chart type="pie" focus={"status"} />
</div>
</div>
</div>
</div>
</>
);
};
export default React.memo(TableDemo); ```

A second React Router Swtich inside a component not working correctly

Hey I have a main route /admin/influencers in my react app. And when I reach this path I want to display a tab button so user can switch to /admin/influencers/active and /admin/influencers/invited for example.
here is my implementation:
return (
<>
<div className="header pb-8 pt-5 pt-md-8">
<Container fluid>
<Card className="tab-container" fluid={true}>
<CardBody className="card-body">
<ButtonGroup className="button-group">
<Button size="lg" outline color="warning" onClick={() => setSelectedTab(1)} active={rSelected === 1}>Active</Button>
<Button size="lg" outline color="warning" onClick={() => setSelectedTab(2)} active={rSelected === 2}>Invited</Button>
<Button size="lg" outline color="warning" onClick={() => setSelectedTab(3)} active={rSelected === 3}>New Verification Request</Button>
</ButtonGroup>
<img onClick={toggle} className="add-influencer" src={Add} alt="add" width="40px" height="40px" />
</CardBody>
</Card>
</Container>
<Switch>
<Route
path="/admin/influencers/active?page=1"
component={ActiveInfluencer}
/>
</Switch>
</div>
<AddInfluencerModal toggle={toggle} isOpen={modal} inviteInfluencers={handleInviteInfluencers} updateInvitedInfluencers={fetchInvitedInfluencers}/>
</>
);
but this isn't displaying anything. Any help would be aprreciated.
Thank you

How to use react-semantic-ui transition.group on map

I have a bunch of cards and I would like to have transition with a delay on each item but couldn't really figure it out on how to implement this.
According to the documentation I have to use something like this:
<Transition.Group
as={List}
duration={200}
divided
size='huge'
verticalAlign='middle'
>
{items.map((item) => (
<List.Item key={item}>
<Image avatar src={`/images/avatar/small/${item}.jpg`} />
<List.Content header={_.startCase(item)} />
</List.Item>
))}
</Transition.Group>
but I didn't really get what is as={List} doing there. That's why couldn't really go forward with my solution. Any help will be appreciated.
Here is what I have:
<Card.Group itemsPerRow={itemsPerRow} centered>
{data.map((e) => {
const dateToFormat = e.created_at;
const userIndex = data.indexOf(e);
return (
<Card raised>
<Image
src={readmeFiles[userIndex].image}
wrapped
size="large"
ui={false}
onClick={() => {
const index = data.indexOf(e);
clickHandler(index);
}}
/>
<Card.Content>
<Card.Header></Card.Header>
<Card.Meta>
<span className="date">{e.name}</span>
</Card.Meta>
<Card.Description>{e.description}</Card.Description>
</Card.Content>
<Card.Content extra>
<a>
<Icon name="calendar alternate outline" />
Created: <Moment date={dateToFormat} fromNow />
</a>
</Card.Content>
</Card>
);
})}
The as={List} tells semantic to render this Transition.Group component as a List component, which serves as a parent to the List.Item components. Those will only render correctly if they have a List as parent. You can check the resulting html out here.
The following snippet:
<Transition.Group
as={List}
duration={200}
divided
size='huge'
verticalAlign='middle'
>
{items.map((item) => (
<List.Item key={item}>
<Image avatar src={`/images/avatar/small/${item}.jpg`} />
<List.Content header={_.startCase(item)} />
</List.Item>
))}
</Transition.Group>
Generates the following html:
<div role="list" class="ui huge divided middle aligned list">
<div role="listitem" class="item fade visible transition">
<img src="/images/avatar/small/ade.jpg" class="ui avatar image" />
<div class="content"><div class="header">Ade</div></div>
</div>
<div role="listitem" class="item fade visible transition">
<img src="/images/avatar/small/chris.jpg" class="ui avatar image" />
<div class="content"><div class="header">Chris</div></div>
</div>
<div role="listitem" class="item fade visible transition">
<img src="/images/avatar/small/christian.jpg" class="ui avatar image" />
<div class="content"><div class="header">Christian</div></div>
</div>
</div>
As you can see the first div says role="List" and has the list css class added. In your case the parent would be the Card.Group component.

I had a question about passing id into my route of details when clicked

So once I click on the details page I would like to pass the id of the product to the url when you click it. So when i click the details page I would like for it to be myurl/details/itemid i found this StackOverflow answer but i cant seem to get it to work. React Router Pass Param to Component.
I would like for when my details page reloads it reloads withe correct items id.
this is my details page
import React, { Component } from "react";
import { ProductConsumer } from "../context";
import { Link } from "react-router-dom";
import { ButtonContainer } from "./Button";
import DropDown from "./Dropdown";
import ItemCategory from "./ItemCategory";
import { Carousel } from "react-responsive-carousel";
import "react-responsive-carousel/lib/styles/carousel.min.css";
import { AwesomeButton } from "react-awesome-button";
export default class Details extends Component {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
dropdownOpen: false
};
}
toggle() {
this.setState(prevState => ({
dropdownOpen: !prevState.dropdownOpen
}));
}
render() {
return (
return (
<div className="container-fluid width-100 bg-white py-5 mt-5 ">
{/* ProductInfo */}
<div className="row">
<div className="col mx-auto col-md-6 my-3 ">
<Carousel autoPlay>
<div>
<img src={img} className="img-fluid" alt="product" />
</div>
<div>
<img src={img2} className="img-fluid" alt="product" />
</div>
<div>
<img src={img3} className="img-fluid" alt="product" />
</div>
<div>
<img src={img4} className="img-fluid" alt="product" />
</div>
</Carousel>
{/* Add a Second Image */}
</div>
{/* Product Text */}
<div className="col mx-auto col-md-6 my-3 text-capitalize">
<h1 className="display-3">{title}</h1>
<h4 className="text-black">
<strong className="text-black">
price : <span>$</span>
{price}
</strong>
</h4>
<h4 className="text-blue">
</h4>
<p className="text-black ">{info}</p>
<p className="text-black ">{fabric}</p>
<small className="text-danger">{luxury}</small>
{/* buttons */}
<div>
<Link to="/all">
<AwesomeButton
className="text-capitalize mx-10"
ripple
size="large"
type="primary"
>
Back To Products
</AwesomeButton>
</Link>
<div className="mt-2">
<AwesomeButton
className="text-capitalize m-auto"
ripple
size="medium"
type="primary"
cart
disabled={inCart ? true : false}
onPress={() => {
value.addToCart(id);
}}
>
{inCart ? "inCart" : "add to cart"}
</AwesomeButton>
</div>
<ItemCategory title={category} />
<div className="mt-2">
<img
src="https://www.paypalobjects.com/digitalassets/c/website/marketing/na/us/logo-center/9_bdg_secured_by_pp_2line.png"
border="0"
alt="Secured by PayPal"
/>
</div>
</div>
</div>
</div>
</div>
);
}}
</ProductConsumer>
);
}
}
<Route path="/details/:id" component={Details} />
and in the component Details you have access
export default class Details extends Component {
render() {
return(
<div>
<h2>{this.props.match.params.id}</h2>
</div>
)
}
}

horizontal align card using map function ReactJS

I'm trying to align horizontal card using grid with react-bootstrap and map function
I'm using redux to bring the Data through the props so I create a JSON to traverse it with a function Map
this is a fraction of my code
render() {
//const { tablaCliente } = this.state
//const { isLoading } = this.state
const { entity } = this.props;
return (
<div>
<Header></Header>
<div className="col-xs-4">
<a className="btn btn-primary" href="/newclient">Nuevo Cliente</a>
</div>
<br />
<center>
<Spinner animation="border" role="status">
<span className="sr-only">Loading...</span>
</Spinner>
</center>
<Container>
{entity && entity.getAllEntities && entity.getAllEntities.length > 0 && entity.getAllEntities.map((item, index) => {
return (
<CardGroup value={item}key={index}>
<Card style={{ width: '1rem' }} >
<Card.Img variant="top" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22286%22%20height%3D%22180%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20286%20180%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_16b0e6bd8ef%20text%20%7B%20fill%3A%23999%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A14pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_16b0e6bd8ef%22%3E%3Crect%20width%3D%22286%22%20height%3D%22180%22%20fill%3D%22%23373940%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22107.203125%22%20y%3D%2296.3%22%3E286x180%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E" />
<Card.Body>
<Card.Title> Cliente {item.name}</Card.Title>
<Card.Text>
{item.description}
</Card.Text>
</Card.Body>
<ListGroup className="list-group-flush">
<ListGroupItem>Estado:{''}{JSON.stringify(item.state)}</ListGroupItem>
<ListGroupItem>Creado por: {item.createby}</ListGroupItem>
</ListGroup>
<Card.Body>
<Card.Link href="/cliente">Card Link</Card.Link>
<Card.Link href="/cliente">Editar</Card.Link>
</Card.Body>
</Card>
</CardGroup>)
})}
</Container>
</div>
);
}
I dont use react-bootstrap but u can achieve it by doing
<Container>
<div class='row'>
{this.map(value => {
return (
<div class='col-md-6'>
<Card />
</div>
)
});}
</div>
</Container>
This should work
Wrap the map function in row and the returning jsx in col
And dont just write a whole logic in jsx use functions or components

Resources