React how to close a popover with a modal - javascript

With react-bootstrap, I have a popover that contains a list. On click of one of the list items, it opens a modal.
How to close the popover when the modal is opening?
I tried:
rootClose but it's not working
React Bootstrap - How to
manually close OverlayTrigger, that close both, the popover and the modal
class TypeColumn extends React.Component {
constructor(props, context) {
super(props, context);
this.close = this.close.bind(this);
}
close() {
this.refs.overlay.hide();
}
render() {
const popoverClick = (
<Popover id="popover-trigger-click-root-close">
<ul>
<NumberOptions onClick={this.close} />
</ul>
</Popover>
);
return (
<OverlayTrigger
show={show}
trigger="click"
placement="bottom"
overlay={popoverClick}
ref="overlay"
>
<i
className={columnTypeIcon} aria-hidden="true"
/>
</OverlayTrigger>
);
}
}
class NumberOptions extends React.Component {
constructor(props) {
super(props);
this.open = this.open.bind(this);
this.state = {
showModal: false,
};
}
open() {
this.setState({ showModal: true });
this.props.onClick();
}
render() {
return (
<div>
<li
data-value={DATA_TYPES.NUMBER}
onClick={this.open}
>
Options nombre
</li>
<Modal
show={showModal}
dialogClassName={styles.customModal}
>
...
</Modal>
</div>
);
}
}

Since your component Modal is a child of NumberOptions, Modal is closing too when NumberOptions is closing.
So you need to lift Modal component at least to the same level as OverlayTrigger.

Related

Common Delete Modal Component in react with Reactstrap

I am trying to create a common Delete Component and call from other components with reactstrap.
Here is my DeleteModal
class DeleteModal extends Component {
constructor(props) {
super(props);
this.state = {
modal: false
};
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState(prevState => ({
modal: !prevState.modal
}));
}
render() {
return (
<div>
<Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
<ModalHeader toggle={this.toggle}>Delete the item</ModalHeader>
<ModalBody>
Do you want to delete the item ?
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={this.toggle}>yes</Button>
<Button color="secondary" onClick={this.toggle}>No</Button>
</ModalFooter>
</Modal>
</div>
);
}
}
export default DeleteModal;
When the user click the YES button I need to return the ID of the item. So that I can know that delete confirm has been click.
Here is the other component from where I want to call the Modal
class Home extends Component {
handleDeleteClick(id) {
console.log(id);
}
}
handleDeleteClick is the method to call the modal also I need to pass the ID to the deleteModal component and when the user click on the YES button I need to get back the id from the DeleteModal.
How can I achieve this? I tried to research on this but not able to identify the solution.
I changed a bit the way you were doing, instead of sending the id to modal, you just save the deleteID on the state. It makes the DeleteModal more specific, it does not have to know any ID, it just let you know if you have to delete or not and the container component will manage that.
class DeleteModal extends Component {
constructor(props) {
super(props);
this.state = {
modal: false
};
this.toggle = this.toggle.bind(this);
this.deleteItem = this.deleteItem.bind(this);
this.close = this.close.bind(this);
}
toggle() {
this.setState(prevState => ({
modal: !prevState.modal
}));
}
deleteItem(){
this.toggle()
this.props.clickHandler()
}
close(){
this.toggle()
this.props.onClose()
}
render() {
return (
<div>
<Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
<ModalHeader toggle={this.toggle}>Delete the item</ModalHeader>
<ModalBody>
Do you want to delete the item ?
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={this.deleteItem}>yes</Button>
<Button color="secondary" onClick={this.close}>No</Button>
</ModalFooter>
</Modal>
</div>
);
}
}
export default DeleteModal;
import DeleteModal from './DeleteModal'
class Home extends Component {
constructor(props) {
super(props);
this.state = {
deleteID: null
};
this.handleDeleteClick = this.handleDeleteClick.bind(this);
this.onCloseModal = this.onCloseModal.bind(this);
this.openModal = this.openModal.bind(this);
}
handleDeleteClick() {
//delete object
this.onCloseModal()
}
onCloseModal(){
this.setState({deleteID: null})
}
openModal(deleteID){
this.setState({deleteID})
}
return(<div><button onClick={()=> this.openModal(id)}>Delete</button>
{deleteID && <DeleteModal clickHandler={this.handleDeleteClick} onClose={this.onCloseModal}/>}
</div>)
}

how close parent modal when open child modal in Reactjs?

I can not close current modal when open new modal in React js. please help me.
I have parent modal: Register_modal and child of it: RegisterCode_Modal
parent modal is called in header component:
first: Header component
this component call first modal and pass open and close props to it:
import React , {Component} from 'react';
import ReactDOM from 'react-dom';
import {NavLink} from 'react-router-dom';
import Register_Modal from './Register_Modal';
export default class Header extends Component {
constructor() {
super();
this.state = {
modalIsOpen: false
};
this.openModal = this.openModal.bind(this);
this.closeModal = this.closeModal.bind(this);
}
openModal(e) {
e.preventDefault();
this.setState({modalIsOpen: true});
}
closeModal(e) {
e.preventDefault();
this.setState({modalIsOpen: false});
}
render() {
return (
<div>
<div className="button navbar-right">
<button className="navbar-btn nav-button wow bounceInRight login" data-wow-delay="0.45s">ورود</button>
<button className="navbar-btn nav-button wow fadeInRight" data-wow-delay="0.48s" onClick={this.openModal} >ثبت نام</button>
<div >
<Register_Modal open={this.state.modalIsOpen} close={this.closeModal} />
</div>
</div>
);
}
}
---------------------------------------------------------------------------
second: parent component
export default class Register_Modal extends Component {
constructor(props){
super(props);
this.state={
codemodal: false
};
this.openCodeModal=this.openCodeModal.bind(this);
this.closeCodeModal=this.closeCodeModal.bind(this);
}
openCodeModal(e){
e.preventDefault();
this.setState({codemodal: true});
}
closeCodeModal(e){
e.preventDefault();
this.setState({codemodal: false});
}
render() {
return (
<div>
<Modal
isOpen={this.props.open}
onRequestClose={this.props.close}
ariaHideApp={false}
contentLabel="selected option"
isClose={this.props.close}
style={customStyles}
>
<h2>salammmmm</h2>
<button onClick={this.props.close} >انصراف</button>
<button onClick={this.openCodeModal} >بعدی</button>
</Modal>
<div className="ReactModalPortal">
<RegisterCode_Modal open={this.state.codemodal} close={this.closeCodeModal} />
</div>
{this.props.close}
</div>
);}
}
------------------------------------------------------------------
third: child component
export default class RegisterCode_Modal extends Component {
constructor(props){
super(props);
console.log("injaaaaa");
}
render() {
return (
<div>
<Modal
isOpen={this.props.open}
onRequestClose={this.props.close}
ariaHideApp={false}
contentLabel="ورود کد"
isClose={this.props.close}
style={customStyles}
>
<h2>مرحله کد</h2>
<button onClick={this.props.close} >تائید</button>
</Modal>
</div>
);}
}
You can simply achieve this by rendering them conditionally.
I personally so this:
export default class RegisterModal extends Component {
state = {
showBaseModal: true,
codemodal: false,
};
openCodeModal = () => {
this.setState({
codemodal: true,
showBaseModal: false,
});
};
closeCodeModal = () => {
this.setState({ codemodal: false });
};
render() {
return (
<div>
{this.state.showBaseModal && (
<Modal
isOpen
onRequestClose={this.props.close}
ariaHideApp={false}
isClose={this.props.close}
>
<button onClick={this.props.close}>Close</button>
<button onClick={this.openCodeModal}>Next</button>
</Modal>
)}
{this.state.codemodal && (
<RegisterCode_Modal
open={this.state.codemodal}
close={this.closeCodeModal}
/>
)}
</div>
);
}
}
Adding an extra state for base modal. On openCodeModal event, toggle it to false to stop both modals.
Set isOpen always to true for both modals, and then render RegisterModal component conditionally.

Reactstrap dropdownitem not triggering setState

What is happening?
Hello, I'm just using reactstrap and react js for a bit and i have some issues regarding the component. I want to trigger show modal when i click on the component inside the component. this is the module that i've been working for a while:
What should be happening?
I expect it to trigger setState when i click the div inside DropdownItem
// Base Account placeholder component <PARENT>
export class Account extends React.Component {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
dropDownOpen : false
};
}
toggle() {
this.setState({
dropDownOpen : !this.state.dropDownOpen
});
}
render() {
return (
<Dropdown isOpen={this.state.dropDownOpen} toggle={this.toggle}>
<DropdownToggle nav caret>
Account
</DropdownToggle>
<DropdownMenu>
<DropdownItem>
<div onClick={() => console.log("fire pew pew")}>
<AccountSettingModal />
</div>
</DropdownItem>
<DropdownItem>
Pricings
</DropdownItem>
<DropdownItem divider />
<DropdownItem>
Logout
</DropdownItem>
</DropdownMenu>
</Dropdown>
);
}
}
this is the modal that i want to show:
// Base account placeholder for Modal Setting
class AccountSettingModal extends React.Component {
constructor(props) {
super(props);
this.state = {
modal: false
};
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState({
modal: !this.state.modal
});
// console.log("modalSetting: " + this.state.modalSetting);
}
render() {
return (
****************! trigger this when i click the dropdownitem !*********************
<div onClick={this.toggle}>
<a>Setting</a>
<Modal isOpen={this.state.modal} toggle={this.toggle} className="account-setting-box">
<ModalHeader toggle={this.toggle}>Setting</ModalHeader>
<ModalBody className="scroll">
</ModalBody>
<ModalFooter className="modal-footer">
</ModalFooter>
</Modal>
</div>
);
}
}
help much appreciated because i've been dealing with this issues for about two days. I can use custommenu for solving this problem, but in the name of my learning process, I'm convinced that i need to ask about this issue that i got.
Thank you very much! 👍
Change the name of the toggle function in the modal as you are using same name function toggle() in parent and child component. Rename toggle() in parent or child component and it will be good to go.
// Account
toggleDropdown() {
this.setState({
dropDownOpen : !this.state.dropDownOpen
});
}
and/or also change:
// AccountSettingModal
toggleState() {
this.setState({
modal: !this.state.modal
});
}

Sharing state from parent to child component in React

I am having an issue binding the state of a parent component to the state of a child. A look at the code:
Parent Component:
class ParentForm extends React.Component {
constructor(){
super();
this.state = {
showDialog: false
};
}
toggleDialog() {
this.setState({showDialog: !this.state.showDialog});
}
return (
<div >
<Button color='primary' onClick={() => this.toggleDialog()}></Button>
<MyDialog open={this.state.showDialog}/>
</div>
);
}
Child Component:
export default class MyDialog extends Component {
constructor(props){
super(props);
this.state = {
open: this.props.open
};
}
handleRequestClose = () => {
this.setState({ open: false });
};
render() {
return (
<div>
<Dialog
fullScreen
open={this.state.open}
onRequestClose={() => this.handleRequestClose()}
transition={<Slide direction="up" />}
>
<DialogTitle>{'Title'}</DialogTitle>
<DialogContent>
<DialogContentText>
This is my dialog
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={() => this.handleRequestClose()} color="primary">Close</Button>
</DialogActions>
</Dialog>
</div>
);
}
}
In the Parent Component, if I make the state.showDialog property true, the dialog will open when the page loads. But once I close it one time, I am never able to open it again. If I have it set to false, it doesn't load when the page loads, and I am never able to open the dialog, even when I click the button on the parent component. Thank you in advance for taking your time to help.
Since you are setting the local state based on the parent, you need to make use of componentWillReceiveProps before v16.3.0 or getDerivedStateFromProps/memoization/key modification thereafter, since your state is set only on the first time and never thereafter. However you don't even need a local state in MyDialog component, you can just make use of Props and communicate from child to the parent component.
Parent
class ParentForm extends React.Component {
constructor(){
super();
this.state = {
showDialog: false
};
}
toggleDialog() {
this.setState({showDialog: !this.state.showDialog});
}
closeDialog() {
this.setState({showDialog: false})
}
return (
<div >
<Button color='primary' onClick={ this.toggleDialog}></Button>
<MyDialog open={this.state.showDialog} closeDialog={this.closeDialog}/>
</div>
);
}
MyDialog (child)
export default class MyDialog extends Component {
constructor(props){
super(props);
}
render() {
return (
<div>
<Dialog fullScreen open={this.props.open} onRequestClose={this.props.closeDialog} transition={<Slide direction="up" />}>
<DialogTitle>{'Title'}</DialogTitle>
<DialogContent>
<DialogContentText>
This is my dialog
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={this.props.closeDialog} color="primary">Close</Button>
</DialogActions>
</Dialog>
</div>
);
}
}
handleRequestClose method should be like this.
handleRequestClose = () => {
this.setState({ open: this.props.open});
};
Edit 1.
You also need to update the parent state when you close the dialog.
toggleDialog(val) {
if(val){
this.setState({showDialog: val});
}else {
this.setState({showDialog: !this.state.showDialog});
}
}
return (
<div >
<Button color='primary' onClick={() => this.toggleDialog()}></Button>
<MyDialog toggleDialog = {this.toggleDialog} open={this.state.showDialog}/>
</div>
);
And,
componentWillRecieveProps(nextProps) {//Lifecycle method to get the updated props
this.setState({ open: nextProps.open });
}
handleRequestClose = () => {
this.setState({ open: !this.state.open},()=>{
this.props.toggleDialog (this.state.open);
});
};
Your child component currently only receives the parent's showDialog prop value once, only when it's initiated in the constructor.
You need to use componentWillRecieveProps and setState of the child's component with the updated value.
So:
componentWillRecieveProps(nextProps) {
this.setState({ open: nextProps.open });
}
EDIT: Need to use nextProps, not this.props
class ParentForm extends React.Component {
constructor(){
super();
this.state = {
showDialog: false
};
}
toggleDialog() {
this.setState({showDialog: !this.state.showDialog});
}
closeDialog() {
this.setState({showDialog: false});
}
return (
<div >
<Button color='primary' onClick={() => this.toggleDialog()}></Button>
<MyDialog open={this.state.showDialog} closeDialog={() => this.closeDialog()/>
</div>
);
}
export default class MyDialog extends Component {
handleRequestClose = () => {
this.props.closeDialog();
};
render() {
return (
<div>
<Dialog
fullScreen
open={this.state.open}
onRequestClose={() => this.handleRequestClose()}
transition={<Slide direction="up" />}
>
<DialogTitle>{'Title'}</DialogTitle>
<DialogContent>
<DialogContentText>
This is my dialog
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={() => this.handleRequestClose()} color="primary">Close</Button>
</DialogActions>
</Dialog>
</div>
);
}
}

Creating Modal Component with Separate Modal Trigger Button

I'm new to React so bear with me.
I'm trying to create a modal component that will be triggered from a onClick() function from any global element i.e: link, button, span or whatever throughout the whole app.
import React from 'react';
import ReactDOM from 'react-dom';
const display = {
display: 'block'
};
const hide = {
display: 'none'
};
class Modal extends React.Component {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
toggle: false
}
}
toggle(event) {
this.setState(prevState => ({
toggle: !prevState.toggle
}));
}
render() {
return (
<div>
<button onClick={this.toggle}>Show Modal</button>
<div className="modal" style={this.state.toggle ? display : hide} >
<div className="modal-content">
{this.props.children}
<button onClick={this.toggle}>Close</button>
</div>
</div>
</div>
);
}
}
module.exports = Modal;
You can use react-bootstrap-modal:
npm install --save react-bootstrap-modal
Then in your component:
import React from 'react';
import Modal from 'react-bootstrap-modal';
export default class ModalExample extends React.Component {
constructor(props){
super(props);
this.state = {
open: false,
}
}
openModal = () => this.setState({ open: true })
closeModal = () => this.setState({ open: false })
render(){
return (
<div>
<button type='button' onClick={this.openModal}>Launch modal</button>
<Modal
show={this.state.open}
onHide={this.closeModal}
aria-labelledby="ModalHeader"
>
<Modal.Header>
<Modal.Title id='ModalHeader'>A Title Goes here</Modal.Title>
<div onClick={this.closeModal}>CLOSE HERE</div>
</Modal.Header>
<Modal.Body>
<p>Some Content here</p>
</Modal.Body>
<Modal.Footer>
// If you don't have anything fancy to do you can use
// the convenient `Dismiss` component, it will
// trigger `onHide` when clicked
<Modal.Dismiss className='btn btn-default'>Cancel</Modal.Dismiss>
// Or you can create your own dismiss buttons
<button className='btn btn-primary' onClick={this.closeModal}>
CLOSE HERE TOO
</button>
</Modal.Footer>
</Modal>
</div>
)
}
}
For further reference, please go here:
https://github.com/jquense/react-bootstrap-modal
You may also need to include bootstrap-CSS file if necessary! Please post here some errors if any, thanks

Categories