I have User.js file to submit form and I am trying to submit form using bootstrap modal. I have no problem to submit data into database.
But I have a problem to close bootstrap modal after I trigger onSubmit={createUser}.
I add onClick={handleCloseModal} and set it into false after I clicked on submit button.
But bootstrap modal isn't close. I don't have any idea what I am doing.
Here's my code in User.js.
import axios from 'axios';
import { data } from 'jquery';
import React, { useEffect, useState } from 'react';
import { Redirect, Link } from 'react-router-dom';
const Users = () => {
const [users, setUsers] = useState([]);
const [name, setName] = useState('');
const [closeModal, setCloseModal] = useState(false);
function handleCloseModal(){
document.getElementById("myModal").classList.remove("show", "d-block", "modal-open");
document.getElementsByClassName("modal-backdrop")[0].classList.remove("modal-backdrop");
}
useEffect( () => {
(
async () => {
const {data} = await axios.get('getUsers');
setUsers(data);
}
)()
}, []);
// Add
const createUser = async (e) => {
e.preventDefault();
await axios.post('createUser', {
name : name
}).then(
async () => {
const {data} = await axios.get('getUsers');
setUsers(data);
}
);
setCloseModal(true);
}
// Add
return(
<>
{/* Page Content */}
<div className="container-fluid">
<div className="row">
<div className="col-lg-12 col-md-12">
<div className="row">
<div className="col-lg-12">
<div className="card card-outline-info">
<div className="card-header">
<h4 className="m-b-0 text-white">Users</h4>
</div>
<div className="card-body">
{/* Modal */}
{!closeModal &&
<div id="myModal" className="modal bs-example-modal-lg" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true" style={{display: 'none', overflowY: 'auto'}}>
<div className="modal-dialog modal-lg">
<div className="modal-content">
<div className="modal-header">
<h4 className="modal-title" id="myModalLabel">Fields <font color="red">*</font> asterisk required</h4>
<button type="button" className="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div className="modal-body">
<form className="" id="" action="" onSubmit={createUser}>
<div className="col-md-12">
<div className="form-group row">
<label className="control-label text-right col-md-3"><font color="red">*</font> Name</label>
<div className="col-md-9">
<input type="text" id="name" className="form-control name" name='name' placeholder=""
onChange={e=>setName(e.target.value)}
/>
<small className="form-control-feedback"></small>
</div>
</div>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-info waves-effect" data-dismiss="modal">Close</button>
<button type="submit" className="btn btn-success waves-effect text-left" id="" onClick={handleCloseModal}>Create</button>
</div>
</form>
</div>
</div>
</div>
</div>
}
{/* Modal */}
{/* Content Layout Here */}
<button type="button" data-toggle="modal" data-target="#myModal" className="btn btn-xs btn-success">Add User</button>
<div className="table-responsive">
<table id="table-user">
<thead>
<tr>
<th style={{verticalAlign: 'top'}}>No</th>
<th style={{verticalAlign: 'top'}}>Name</th>
</tr>
</thead>
<tbody>
{users.map((user) => {
return(
<tr key={user.id}>
<td>{user.name}</td>
</tr>
)
})}
</tbody>
</table>
</div>
{/* Content Layout Here */}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{/* Page Content */}
</>
);
}
export default Users;
UPDATE :
Before I submit
After I submit
Am I missing something here ?
Appreciate someone can help me on this issue.
Many thanks.
Try this:
function handleCloseModal(){
document.getElementById("myModal").classList.remove("show");
}
For Bootstrap 4 try this:
function handleCloseModal(){
document.getElementById("myModal").classList.remove("show", "d-block");
}
EDIT
To remove the grey background after the closing of modal, you can do this:
function handleCloseModal(){
document.getElementById("myModal").classList.remove("show", "d-block");
document.querySelectorAll(".modal-backdrop")
.forEach(el => el.classList.remove("modal-backdrop"));
}
You can use data-dismiss="modal" to close Modal as Close Button
If you want use state closeModal, you can do add like this:
{!closeModal && <div id="myModal">{/* ... */}</div>}
Related
I create a todo list system and I use a map to make a loop for all the items I have.
But items are stuck for me in the same row in the table.
This is a system I used to build in js vanila and now for practice I run it in react.
I would be happy for a solution.
In js vanila I would use insertAdjacentHTML
But I'm looking for the solution in react
demo for the app: https://v1-todolist.netlify.app
My Problem All items in one row.
I need not all items to be on the same line I need it to be dropped line like here
Example of how the item should look properly.
This system I built it in js vanila
if i add Div it not work
It does not sit well in the table and I also get a validateDOMNesting (...) error: cannot appear as a child of .
my code App.js
import { useState } from "react";
import "./App.css";
import Form from "./Form";
import Alert from "./Alert";
function App() {
const [nameTask, setNameTask] = useState("");
const [priority, setPriority] = useState("Low");
const [list, setList] = useState([]);
const [alert, setAlert] = useState({ show: false, type: "", msg: "" });
const handlerSubmit = function (e) {
e.preventDefault();
if (!nameTask) return showAlert(true, "danger", "you cannot input empty");
const newList = {
id: new Date().getTime().toString(),
title: nameTask,
priority: priority,
};
setList([...list, newList]);
};
const showAlert = function (show = false, type, msg) {
setAlert({ show, type, msg });
};
return (
<article className="vh-100 gradient-custom-2">
{alert.show && <Alert {...alert} showAlert={showAlert} />}
<Form
list={list}
setNameTask={setNameTask}
setPriority={setPriority}
handlerSubmit={handlerSubmit}
/>
</article>
);
}
export default App;
Form.js
import React from "react";
const Form = function ({ handlerSubmit, setNameTask, setPriority, list }) {
return (
<div className="container py-5 h-100">
<div className="row d-flex justify-content-center align-items-center h-100">
<div className="col-md-12 col-xl-10">
<div className="card mask-custom">
<div className="card-body p-4 text-white">
<div className="text-center pt-3 pb-2">
<img
src="https://mdbootstrap.com/img/Photos/new-templates/bootstrap-todo-list/check1.png"
alt="Check"
width="60"
/>
<h2 className="my-4">Task List</h2>
</div>
<form className="form-task" onSubmit={(e) => handlerSubmit(e)}>
<div className="col-auto">
<input
name="name-task"
type="text"
className="form-control task-input"
id="autoSizingInput"
placeholder="Add Task"
onChange={(e) => setNameTask(e.target.value)}
/>
</div>
<select
className="form-select"
aria-label="Default select example"
onChange={(e) => setPriority(e.target.value)}
>
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
</select>
<button type="submit" className="btn btn-primary">
submit
</button>
</form>
<table className="table text-white mb-0">
<thead>
<tr>
<th scope="col">Task</th>
<th scope="col">Priority</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<tr className="fw-normal">
{list.map((item, index) => {
return (
<React.Fragment key={index}>
<th>
<span className="ms-2" data-name={`${item.title}`}>
{item.title}
</span>
</th>
<td className="align-middle priority-class">
<span className="badge ${value.color}">
{item.priority}
</span>
</td>
<td className="align-middle">
<h6 className="mb-0" data-id="${value.id}">
<a className="remove-link" href="#">
<span className="badge bg-gradient remove">
❌
</span>
</a>
<a className="complete-link" href="#">
<span className="badge bg-gradient complete">
✔
</span>
</a>
</h6>
</td>
</React.Fragment>
);
})}
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
);
};
export default Form;
Check this CodeSandbox
I was able to solve your issu just by adding a flex propriety to the panel that contain your list and by changing React.fragment by a div.
However it would perhaps be better to swap the node with the class fw-normal to a div and change the React.fragment to the node tr.
I was able to use the map method to display my static data in each container. There's a Modal template I created in order to provide more object/values info but if I click the button it opens all the modals of each containers at the same time because Modal tag wasn't assigned with unique index number when I mapped through my static data. Big brothers, please look at my codes and teach me how I can open only one associated Modal.
I created function Modal (template) and import to function Projects
function Modal({closeModal}) {
return (
<div className="modal-main">
<div className='modal-card'>
<img src="" />
<div className='modal-info'>
<div className='modal-title'>
<p className="title">title</p>
</div>
<div className='modal-body'>
<p className="tech">tech</p>
<p className="description">description</p>
<p className="github">github</p>
<p className="website">website</p>
</div >
<Button>detail</Button>
<Button onClick={()=> closeModal(false)}>close</Button>
</div>
</div>
</div>
)
}
export default Modal
function Projects() {
const [openModal, setOpenModal] = useState(false);
const [oneProject, setOneProject]
= useState(list_of_projects)
return (
<div className="section-header text-center mt-5">
<h4>projects</h4>
<p>list of proejcts</p>
<div className="proj-container">
{oneProject.map((item, index)=> {
return(
<div className='proj-map mx-2 my-2'key={index} >
<button type='button' className="btn btn-link " onClick={() => setOpenModal(true)}>
<div className=" proj-card card">
<img className='proj-img' src={item.thumbnail} style={{ width:300, height:300 }}/>
<div className="proj-text card-text">
<h3 className='title'>{item.title}</h3>
<h5 className='description'>description</h5>
<br/>
<h4>+</h4>
</div>
</div>
</button>
{openModal && <**Modal** closeModal={setOpenModal} /> }
</div>
)
})}
</div>
</div>
I created a button that change the state of a variable on click, the problem is: when i click on it, the console.log prints the variable twice, how do i avoid this problem?
My code:
function List(){
const [documents, setDocuments] = useState([])
var [isToggled, setIsToggled] = useState(true)
return (
<div className={ isToggled ? "d-flex" : "d-flex toggled"} id="wrapper">
{ console.log(isToggled)}
<div className="bg-light border-right" id="sidebar-wrapper">
<div className="sidebar-heading"> Rascunhos </div>
<div className="list-group list-group-flush">
<tr className="d-flex conteudo">
<td className="list-group-item descricao">Dashboard</td>
<td className="list-group-item qnt">1997</td>
</tr>
</div>
<div className="btn-group">
<button className="btn btn-primary btnCarregar">Carregar</button>
</div>
</div>
<button className="btn btn-primary btnToggle ml-2 mt-2" onClick={ () => setIsToggled(!isToggled) } id="menu-toggle">></button>
</div>
)
}
I had this issue as well and it had to do with StrictMode being enabled which gets added by default when you create a new React app. At first it only happened on Class Components but now will be default for functional components as well. It is by design when using useState or other Hooks.
https://github.com/facebook/react/issues/15074
I have a component which displays list of jobs. Clicking on each job will open a modal and displays the details of the job. I have a list of candidates displayed in the modal and when i click a button in the modal, the component re renders and the list of candidates are populated twice. how to prevent that. I have called the props below the render() which should not be done. But don't know where to give.
import { connect } from 'react-redux';
import history from '../history'
import moment from 'moment'
import changeStatus from '../store/actions/changeJobStatus'
class EmpJobcontent extends Component {
constructor(props) {
super(props);
this.sendJobId = this.sendJobId.bind(this);
}
state = {
jobid: '', candidatephoneno: '', statusid: ''
}
getJobId = (event) => {
this.setState({
jobid: event.target.getAttribute('name')
}, () => this.sendJobId())
}
closejob = (event) => {
// this.props.changeStatus(event.target.getAttribute("name"))
// var x = document.getElementById("status");
var x = event.target
if (x.value === "close job") {
x.value = "open job";
x.classList.add("btn-primary");
x.classList.remove("btn-danger");
this.props.changeStatus(event.target.getAttribute("name"))
} else {
x.classList.remove("btn-primary");
x.classList.add("btn-danger");
x.value = "close job";
this.props.changeStatus(event.target.getAttribute("name"))
}
}
individualChat = () => {
// history.push('/individualchat')
}
sendJobId = () => {
this.props.empInfo(this.state.jobid)
}
applyJob = () => {
document.getElementById("applyjob").disabled = true;
const { candidatephoneno } = this.props
this.setState({
candidatephoneno: candidatephoneno, jobid: this.state.jobid
}, () => this.sendApplyJob())
}
sendApplyJob = () => {
this.props.applyJob(this.state)
}
render() {
const { joblists } = this.props
const { appliedCandidates } = this.props
return (
<div className=' d-flex flex-wrap justify-content-center' >
{joblists && joblists.map(joblist => {
return (<div >
<div id='cards' ref="jobid" className="card shadow slidercard ">
<div className="row mt-3 d-flex align-items-center" >
<div className="col-md-3">
<div className="profilepic ml-3"></div>
</div>
<div className="text-left col-md-9 ">
<h6>{joblist.companyName}</h6>
</div>
{/* <div className="col-md-3"><i className="far fa-heart heart_color"></i></div> */}
</div>
<div className="m-3">
<h5 className="font-weight-bold pt-3 text-left" id="jobdescription" data-toggle="modal" data-target={`#${joblist.id}`} name={`${joblist.id}`} >{joblist.Description}</h5>
<h5 className="font-weight-bold pt-3 text-left ">{joblist.RatePerHour}</h5>
<div className="row pt-3" id='cardfooter'>
<div className="col-md-2">
<div><i className="fas fa-map-marker-alt"></i></div>
</div>
<div className="col-md-5">
<h6 className="float-left">{joblist.Location}</h6>
</div>
<div className="col-md-5">
<h6 className="float-right">{moment(joblist.postedon.toDate()).fromNow()}</h6>
</div>
</div>
</div>
</div>
<div className="modal fade show" id={joblist.id} ref="jobid" tabIndex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div className="modal-dialog shadow mw-100 w-75" role="document">
<div className="modal-content">
<div className="modal-header">
<button type='button' className="close" data-dismiss="modal">×</button>
</div>
<div className="modal-body">
<div className="jobdesc">
<div className="row mt-3">
<div className="col-md-2">
<div className="jobdescpic ml-3"></div>
</div>
<div className="col-md-8 dialog_body text-left">
<h6>{joblist.companyName}</h6>
<div>
<h5 className="font-weight-bold pt-3 text-left mb-5">{joblist.Title}</h5>
<div className="row d-flex align-items-center">
<div className="col-md-9">
<div className="row d-flex align-items-center border shadow p-2 font-weight-bold " id='rateperhour'>
<div className="col-md-4 text-center">
<p><i className="far fa-clock"></i>1 hour </p>
</div>
<div className="col-md-4 text-center">
<p>{joblist.RatePerHour}</p>
</div>
{/* <div className="col-md-6 ml-auto">
<button type="submit" id='applyjob' className="cta btn btn-primary" onClick={this.applyJob}>apply job</button>
</div> */}
<div className="col-md-4 text-center">
<p>{joblist.status}</p>
</div>
</div>
</div>
<div className="col-md-3">
<div className="d-flex align-items-start justify-content-end">
<h6>{moment(joblist.postedon.toDate()).fromNow()}</h6>
</div>
</div>
</div>
<p className="mt-5">{joblist.Description}</p>
{/* <img src={documentimg} alt="" /> */}
{/* <a href={joblist.Documents} target='_blank'>{joblist.labeltext}</a> */}
</div>
</div>
<div className="col-md-2" id="popupmenu">
<input type='button' id='status' className={joblist.status === "closed" ? "btn btn-primary" : "btn btn-danger"} onClick={this.closejob.bind(this)} name={joblist.id} value={joblist.status === 'closed' ? 'open job' : "close job"} />
{/* <ul>
<li>
<i className="fas fa-ellipsis-h"></i>
<ul>
<li><Link to={`/appliedcandidates`}>status</Link></li>
</ul>
</li>
</ul> */}
</div>
</div>
</div>
</div>
<div class="modal-footer d-block">
<h4 className="text-center p-3 font-weight-bold text-info">applied candidates</h4>
{appliedCandidates && appliedCandidates.map(appliedCandidate => {
if (joblist.id === appliedCandidate.jobid) {
return (
<div className="row pb-3">
<div className="col-md-4 offset-md-1 text-left font-weight-bold">
<p>{appliedCandidate.firstName}</p>
</div>
<div className="col-md-2">
<button type="button" className="btn btn-success card-btn-width">hire</button>
</div>
<div className="col-md-2">
<button type="button" className="btn btn-danger card-btn-width">reject</button>
</div>
<div className="col-md-2">
<button id={appliedCandidate.id} onClick={this.individualChat} type="submit" className="btn btn-info card-btn-width" data-dismiss="modal">chat</button>
</div>
</div>
)
}
})}
</div>
</div>
</div>
</div>
</div>
)
})
}
</div >
)
}
}
const mapStateToProps = (state) => {
console.log(state);
return {
candidatephoneno: state.candidateSignin.phoneno,
appliedCandidates: state.getAppliedCandidates.appliedCandidate
}
}
const mapDispatchToProps = (dispatch) => {
return {
changeStatus: (statusid) => dispatch(changeStatus(statusid)),
}
}
export default connect(mapStateToProps, mapDispatchToProps)(EmpJobcontent)
I am using React 16 with Bootstrap 4.
I am using bootstrap modal to display some values. I need to reset these values whenever modal is closed.
For Modal I have created a separate component. I dont want to use React-Modal as I get all the functionality in the current modal.
I know in plain javascript it is achieved using below code:
$(".modal").on("hidden.bs.modal"){
//reset values here
};
But I dont know how this is achieved in ReactJS?
Below is my code for modal:
<div className="modal fade modal-flex" id="large-Modal-OneUser" tabIndex={-1} role="dialog">
<div className="modal-dialog modal-lg" role="document">
<div className="modal-content">
<div className="modal-header" style={{display: 'block'}}>
<div className="row">
<div className="col-md-6">
<h2 style={{fontWeight:600}}>{newTimelineData.length > 0 ? newTimelineData[0].candidateName : ""}</h2>
</div>
<div className="col-md-6">
<span style={{display: 'inline-flex',alignItems: 'center',float:'right'}}><h4 style={{paddingRight:20}}>{isNotEmpty(this.state.scheduledFor) ? moment(this.state.scheduledFor).format("DD-MMM-YYYY"):this.checkScheduledFor(newTimelineData)}</h4>
<h3 style={{paddingRight: 10}}>{isNotEmpty(this.state.probability) ? this.getProbabilityHTML() : this.checkProbability(newTimelineData)}</h3>
{/*<button type="button" className="close" data-dismiss="modal" aria-label="Close" style={{marginTop: 0,marginBottom: 10}}>
<span aria-hidden="true">×</span>
</button>*/}
</span>
</div>
</div>
</div>
<div className="modal-body">
<div className="col-md-12">
{/*<div className="card">*/}
{/*<div className="card-block">*/}
{/* Horizontal Timeline start */}
<div className="cd-horizontal-timeline">
<div className="timeline">
<div className="events-wrapper">
<div className="events" id="foo">
<ol>
{newTimelineData.map((item, index) => (
<li key={item.id}>
<a
href="#0" onClick={()=> this.setHeaders(item)}
data-date={moment(item.scheduledFor).format('DD/MM/YYYY')}
className={index === 0 ? 'selected' : null}>
<Moment unix format="DD MMM">
{item.scheduledFor / 1000}
</Moment>
</a>
</li>
))}
</ol>
<span className="filling-line" aria-hidden="true" />
</div>
{/* .events */}
</div>
{/* .events-wrapper */}
<ul className="cd-timeline-navigation">
<li>
<a href="#0" className="prev inactive">
Prev
</a>
</li>
<li>
<a href="#0" className="next">
Next
</a>
</li>
</ul>
{/* .cd-timeline-navigation */}
</div>
{/* .timeline */}
<div className="events-content">
<ol>
{newTimelineData.map((item, index) => (
<li
key={item.id}
className={index === 0 ? 'selected' : null}
data-date={moment(item.scheduledFor).format('DD/MM/YYYY')}>
<div className="row">
<div className="col-sm-8" style={{fontSize:'1rem',paddingLeft: 3,paddingRight:0}}><b>Job</b> : {item.jobName}</div>
{isNotEmpty(joiningDate) && <div className="col-sm-4" style={{fontSize:'1rem',paddingLeft: 3,paddingRight:0}}><b>Joined Date : </b>{joiningDate}</div>}
</div>
<div className="row">
<div className="col-sm-8" style={{fontSize:'1rem',padding:0}}><b>Stage</b> : {this.props.stage}</div>
{isNotEmpty(offerDate) && <div className="col-sm-4" style={{fontSize:'1rem',paddingLeft: 0,paddingRight:0}}><b>Offer Date : </b>{offerDate}</div>}
</div>
<br></br>
<div className="row">
<div className="col-sm-12" style={{fontSize:'1rem',paddingLeft: 0}}><b>Comments</b> :<br></br>{item.comments}</div>
</div>
</li>
))}
</ol>
</div>
{/* .events-content */}
</div>
{/* Horizontal Timeline end */}
{/*</div>*/}
{/*</div>*/}
</div>
</div>
<div className="modal-footer">
<button style={{backgroundColor: '#8080808f',borderColor:'#8080808f'}}
type="button"
className="btn btn-primary waves-effect waves-light"
data-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>
Can anyone help?
See below snapshot for the work around I have tried suggested by #Jayavel.
Implemented a small workaround and hope it's fulfills your need, With my understanding you load your modal body with state and user will change something and you store those in the same state.
While closing the modal(close button) you need to reset the initial state i.e reset to default values.
Is that right !!! check this demo
what you need is,
store your default state like below:
const initialState = {
isOpen: false,
value: "defaultvalue"
};
and in component :
class App extends Component {
constructor(props) {
super(props);
this.state = initialState; // stored defaultstate
}
toggleModal = () => {
this.setState({
isOpen: !this.state.isOpen
});
}
toggleModalClose = () => { // modal close to reset input val
this.setState(initialState);
}
handleChange = (e) => {
this.setState({
value: e.target.value //input new value
});
}
render() {
return (
<div className="App">
<button onClick={this.toggleModal}>
Open the modal
</button>
<Modal show={this.state.isOpen}
onClose={this.toggleModalClose}>
<input type="text" value={this.state.value} onChange={this.handleChange} />
</Modal>
</div>
);
}
}
Hope this helps.