Good evening I am learning modals but I ran into a problem, the openModal() function works just fine but I cant get it to close...
Code:
import React, { Component } from 'react';
import './Navbar.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Container, Navbar, NavbarBrand, Nav, InputGroup, FormControl, Modal, Button, ButtonGroup } from 'react-bootstrap';
import { library } from '#fortawesome/fontawesome-svg-core';
import { fab } from '#fortawesome/free-brands-svg-icons';
import Foot from './Footer'
import Today from '../Main/Home'
import Account from '../AccountPage/Account'
import Pool from '../PoolPage/Pool'
import Stats from '../StatsPage/Stats'
import BondingCard from '../BondPage/BondCard'
import AdCard from '../AdPage/AdHome'
library.add(fab)
class Navy extends Component {
constructor(props) {
super(props);
this.state = {
content: <Today />,
showlogin: false
}
this.homepage = this.homepage.bind(this);
this.accountpage = this.accountpage.bind(this);
this.statspage = this.statspage.bind(this);
this.poolpage = this.poolpage.bind(this);
this.loanpage = this.loanpage.bind(this);
this.adpage = this.adpage.bind(this);
this.openModal = this.openModal.bind(this);
this.closeModal = this.closeModal.bind(this);
}
homepage() {
this.setState({content: <Today/>});
}
accountpage() {
this.setState({content: <Account/>})
}
statspage() {
this.setState({content: <Stats/>})
}
poolpage() {
this.setState({content: <Pool/>})
}
loanpage() {
this.setState({content: <BondingCard/>})
}
adpage() {
this.setState({content: <AdCard/>})
}
openModal() {
this.setState({ showlogin: true });
}
closeModal() {
this.setState({ showlogin: false });
}
render() {
return (
<div>
<Navbar bg='light' variant='light' >
<div className='logo'>
<Container >
<NavbarBrand className='brandcolor'>
<h1>ETHER<span>SHARE<i className="fab fa-ethereum"></i></span></h1>
</NavbarBrand>
</Container>
</div>
<div className='wbtn' onClick={this.openModal}>
<h3><i class="fas fa-exclamation-triangle"></i>Connect|Login<i class='fas fa-exchange-alt'></i></h3>
<Modal show={this.state.showlogin} onHide={this.closeModal} size='lg' centered>
<Modal.Header closeButton>
<Modal.Title className='cent'>
<ButtonGroup size="sm" className="mb-2">
<Button variant='dark'>Login</Button>
<Button variant='dark'>Register</Button>
</ButtonGroup>
</Modal.Title>
</Modal.Header>
<Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={this.closeModal}>
Close
</Button>
<Button variant="primary" onClick={this.closeModal}>
Save Changes
</Button>
</Modal.Footer>
</Modal>
</div>
<Container>
<InputGroup className="mb-3">
<InputGroup.Text id="basic-addon1"><i class="fas fa-user-plus" aria-hidden="true"></i></InputGroup.Text>
<FormControl
placeholder="#username (search)"
aria-label="Username"
aria-describedby="basic-addon1"
/>
</InputGroup>
</Container>
<Container >
<Nav >
<ul className='menuItems'>
<div className='mainmenu'>
<Nav.Link className='linkitem'>
<li onClick={this.homepage}>
<i class="fa fa-home" aria-hidden="true"></i>
Home
<div className='arrow'>
<i class="fa fa-angle-double-right" ></i>
</div>
</li>
</Nav.Link>
<Nav.Link className='linkitem'>
<li onClick={this.accountpage}>
<i class="fa fa-cog" aria-hidden="true"></i>
Account
<div className='arrow'>
<i class="fa fa-angle-right"></i>
</div>
</li>
</Nav.Link>
<Nav.Link className='linkitem'>
<li onClick={this.poolpage}>
<i class="fa fa-rocket" aria-hidden="true"></i>
Pool
<div className='arrow'>
<i class="fa fa-angle-right"></i>
</div>
</li>
</Nav.Link>
<Nav.Link className='linkitem'>
<li onClick={this.loanpage}>
<i class="fa fa-university" aria-hidden="true"></i>
Loan|Borrow
<div className='arrow'>
<i class="fa fa-angle-right"></i>
</div>
</li>
</Nav.Link>
<Nav.Link className='linkitem'>
<li onClick={this.adpage}>
<i class="fa fa-plus" aria-hidden="true"></i>
Ads(earn crypto)
<div className='arrow'>
<i class="fa fa-angle-right"></i>
</div>
</li>
</Nav.Link>
<Nav.Link className='linkitem'>
<li onClick={this.statspage}>
<i class="fa fa-signal" aria-hidden="true"></i>
Stats
<div className='arrow'>
<i class="fa fa-angle-right"></i>
</div>
</li>
</Nav.Link>
</div>
</ul>
</Nav>
</Container>
<div className='footy'>
< Foot />
</div>
</Navbar>
{this.state.content}
</div>
)
}
};
export default Navy;
Does anyone know what I did wrong? Error in my 2 functions? This is the entire file, everything works but closeModal()...
dddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddd
ddddddddddd
You need to use setTimeOut, please try my experiment
Here
closeModal() {
let vm = this;
setTimeout(() => {
vm.setState({ showlogin: false });
}, 100);
}
Related
i am trying to make a nav bar when a navlink is clicked i want the activelink to change background-color
i have used NavLink but it is not woeking for some reasome pls help
export class side extends Component {
render() {
return (
<div className="l-navbar " id="nav-bar">
<nav className="nav">
<NavLink
activeClassName="active3"
className="porpssidebar"
exact={true}
to="/"
>
<div>
<a href="#" className="nav_logo">
<img className="slogo" src={forur} />
<i className="fas fa-angry"></i>{" "}
</a>
</div>
</NavLink>
<NavLink activeClassName="active3" exact={true} to="/orders">
<div>
{" "}
<a href="#" className="nav_logo">
<Orders />
<i className="fas fa-angry"></i>{" "}
</a>
</div>
</NavLink>
<NavLink to="/customers" activeClassName="active3">
<div>
{" "}
<a href="#" className="nav_logo">
<Package />
<i className="fas fa-angry"></i>{" "}
</a>
</div>
</NavLink>
</div>
);
}
}
export default side;
in app.css i have a class called active3 for changing color
.active3 {
background: black;
}
Hello I would like to use bootstrap 5 without react-bootstrap and reactstrap when I create my modal I end up with this error:
TypeError: undefined is not an object (evaluating 'this._element.classList')
Do you have any idea because all the examples I am viewing are still with reactstrap or react-bootstrap, do you have any idea how to do this, thanks for your help.
import Document, { Html, Head, Main, NextScript } from "next/document";
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx);
return { ...initialProps };
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
<div id="modal-root"></div>
</body>
</Html>
);
}
}
export default MyDocument;
My modal
import { useState, useEffect } from "react";
import ReactDom from "react-dom";
const Modal = ({ show, onClose, children, title, targetModal }) => {
const [isBrowser, setIsBrowser] = useState(false);
useEffect(() => setIsBrowser(true));
const handleCloseModal = (e) => {
e.preventDefault();
let myModalEl = document.getElementById(targetModal);
console.log(myModalEl);
onClose();
};
const modalContent = show ? (
<div
className="modal fade py-3"
tabIndex="-1"
role="dialog"
id={targetModal}
>
<div className="modal-dialog" role="document">
<div className="modal-content border-0 rounded-6 shadow-blue-sm">
<div className="modal-header p-4 pb-4 border-bottom-0">
<h4 className="fw-bold mb-0 hstack text-secondary">
{title && (
<span>
<i className="ri-user-search-line me-2"></i> {title}
</span>
)}
</h4>
<button
type="button"
className="btn-close"
data-bs-dismiss="modal"
onClick={handleCloseModal}
aria-label="Close"
></button>
</div>
<div className="modal-body p-4 pt-0">{children}</div>
</div>
</div>
</div>
) : null;
if (isBrowser) {
return ReactDom.createPortal(
modalContent,
document.getElementById("modal-root")
);
} else {
return null;
}
};
export default Modal;
My Page
import { useState } from "react";
import Layouts from "#/components/Layouts";
import Modal from "#/components/Modal";
const EditEventsPage = () => {
const [showModal, setShowModal] = useState(false);
return (
<Layouts title="Edit event.">
<button
type="submit"
className="btn btn-secondary mt-4 hstack"
data-bs-toggle="modal"
data-bs-target="#uploadImage"
onClick={() => setShowModal(true)}
>
<i className="ri-image-line "></i> Ajouter image{" "}
</button>
<Modal
show={showModal}
onClose={() => setShowModal(false)}
targetModal="uploadImage"
></Modal>
</Layouts>
);
};
export default EditEventsPage;
Thansk for your helps.
I have not been able to achieve this using your method.
However, this works for me, and you could expand on the example.
in your _app.js file add this line of code, you should wrap it in the useEffect function as so
useEffect(() => {
typeof document !== undefined
? require("bootstrap/dist/js/bootstrap")
: null;
},[]);
This will make bootstrap available across your project. So where ever you want to trigger your modal.
just do this instead
<button type="button" data-bs-toggle="modal" data-bs-target="#myModal">Launch modal</button>
Your Bootstrap modal
<div
className="modal modal-alert bg-secondary py-5"
tabIndex="-1"
role="dialog"
id="myModal"
>
<div className="modal-dialog modal-dialog-centered" role="document">
<div className="modal-content rounded-4 shadow">
<div className="modal-body p-4 text-center">
<h5 className="mb-0">Enable this setting?</h5>
<p className="mb-0">
You can always change your mind in your account settings.
</p>
</div>
<div className="modal-footer flex-nowrap p-0">
<button
type="button"
className="btn btn-lg btn-link fs-6 text-decoration-none col-6 m-0 rounded-0 border-right"
>
<strong>Yes, enable</strong>
</button>
<button
type="button"
className="btn btn-lg btn-link fs-6 text-decoration-none col-6 m-0 rounded-0"
data-bs-dismiss="modal"
>
No thanks
</button>
</div>
</div>
</div>
</div>
Hope this helps.
I have this navbar where the user can navigate through the site and logged in users can make posts on the main page or news posts on the news page. However, i have 3 levels of permissions made in the backend where the visitors are allowed to navigate the website, then authenticated users can make normal posts or news posts but the last one is admin only permission to make a program post. the question is how can i show the third button (which is for program post) not show for authenticated users and only pop up when an admin logs in
these buttons can be found in the code bellow under authLinks
import { Link, NavLink } from 'react-router-dom';
import { connect } from 'react-redux';
import { logout } from '../actions/auth';
import Alert from './Alert';
import PropTypes from 'prop-types';
import {Navbar, NavDropdown} from "react-bootstrap";
import Logo from '../assets/images/logo.png';
const navbar = ({ auth:{isAuthenticated, loading}, logout}) => {
const authLinks = (
<Fragment>
<NavLink className='btn btn-primary btn-sm m-2' to='/create-post'>Nytt innlegg</NavLink>
<NavLink className='btn btn-primary btn-sm m-2' to='/create-nyhet'>Nytt nyhetsinnlegg</NavLink>
<NavLink className='btn btn-primary btn-sm m-2' to='/create-program'>Nytt arrangement</NavLink>
<a className='btn btn-primary btn-sm m-2' onClick={logout} href='#!'>Logg ut</a>
</Fragment>
);
const guestLinks = (
<NavLink className='btn btn-primary btn-sm m-2' to='/login'>Logg inn</NavLink>
);
return(
<Fragment>
<Navbar bg="dark" variant="dark" expand="md" sticky="top">
<div className="container-fluid">
<Navbar.Brand href='/'><img className="img-responsive" src={Logo} width="220" height="55" alt="Sammfunnet Logo" /></Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<ul className="navbar-nav p-2">
<li className="nav-item active">
<NavLink className="nav-link" exact to='/'>Hjem</NavLink>
</li>
<li className="nav-item">
<NavLink className="nav-link" exact to='/nyhet'>Nyhet</NavLink>
</li>
<li className="nav-item">
<NavLink className="nav-link" exact to='/program'>Program</NavLink>
</li>
<NavDropdown title="Kontakt Oss" id="basic-nav-dropdown">
<NavDropdown.Item href="/bli-frivillig">Bli Frivillig</NavDropdown.Item>
<NavDropdown.Item href="/presse">Presse</NavDropdown.Item>
<NavDropdown.Item href="#action/3.3">Samarbeid</NavDropdown.Item>
</NavDropdown>
<li className="nav-item">
<NavLink className="nav-link" exact to='/omoss'>Om oss</NavLink>
</li>
</ul>
</Navbar.Collapse>
<span className="nav_link">
{ !loading && (<Fragment>{ isAuthenticated ? authLinks : guestLinks }</Fragment>) }
</span>
</div>
</Navbar>
<Alert />
</Fragment>
);
};
navbar.propTypes = {
logout: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired
}
const mapStateToProps = state => ({
auth: state.auth
});
export default connect(mapStateToProps, { logout })(navbar);
Do this then should work
const authLinks = (
<Fragment>
<NavLink className='btn btn-primary btn-sm m-2' to='/create-post'>Nytt innlegg</NavLink>
<NavLink className='btn btn-primary btn-sm m-2' to='/create-nyhet'>Nytt nyhetsinnlegg</NavLink>
{ isAuthenticated && (<NavLink className='btn btn-primary btn-sm m-2' to='/create-program'>Nytt arrangement</NavLink> ) }
<a className='btn btn-primary btn-sm m-2' onClick={logout} href='#!'>Logg ut</a>
</Fragment>
);
This is my code for the queue please only answer if you personally know me and are fimiliar with the project.
import React, { Fragment } from 'react';
import { Link } from 'react-router-dom'
import InputQ from './InputQ';
import ListQ from './ListQ';
import './Admin.css';
const AdminQueue = () => {
return (
<Fragment>
<InputQ />
<Link to="/">
<i className="fas fa-home float-right mt-3 mb-3" aria-hidden="true" />
</Link>
<Link to="/signin">
<i className="fa fa-lock float-right mt-3 mb-3 mr-3" aria-hidden="true" />
</Link>
<ListQ />
</Fragment>
)
}
export default AdminQueue;
All you have to do is import this make sure download with npm:
import SGF from 'react-sgf-dom';
And Wrap what you want to SGF with the tags:
const AdminQueue = () => {
return (
<SGF>
<InputQ />
<Link to="/">
<i className="fas fa-home float-right mt-3 mb-3" aria-hidden="true" />
</Link>
<Link to="/signin">
<i className="fa fa-lock float-right mt-3 mb-3 mr-3" aria-hidden="true" />
</Link>
<ListQ />
</SGF>
)
}
I have a bootstrap navigation menu and I am trying to simulate a click of a nav item on a nav dropdown in React with Jest. I have the following dropdown:
Navbar.js
export default props => {
const { user, email, group } = props;
return (
<Nav className="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
<button
type="button"
id="sidebarToggleTop"
className="btn btn-link d-md-none rounded-circle mr-3"
>
<FontAwesomeIcon icon={faBars} />
</button>
<ul className="navbar-nav ml-auto">
<div className="topbar-divider d-none d-sm-block" />
<NavDropdown
data-testid="profile-dropdown"
className="no-arrow"
title={
<>
<span className="mr-2 d-none d-lg-inline text-gray-600 small">
{user}
</span>
{email && (
<Gravatar
email={email}
className="img-profile rounded-circle"
/>
)}
</>
}
id="userDropdown"
>
<div className="dropdown-item">Profile: {group}</div>
<Link className="dropdown-item" to="/refresh">
Refresh
</Link>
<NavDropdown.Divider />
<NavDropdown.Item data-testid="sign-out-button" eventKey="logout" onSelect={() => Auth.signOut()}>
SignOut
</NavDropdown.Item>
</NavDropdown>
</ul>
</Nav>
);
};
Can anyone help? The item is not present in the DOM on render so I cannot use data-testid.