Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close semantic ui modal in another react component?

In my main component I can open a modal by clicking on an icon. The content of the modal is a separate component, which is calling a method. If the method call is successful, I want to close the modal. But how can I do this?

Main component

class Example extends Component {
    constructor(props) {
        super(props)
        this.state = {}
    }

    render() {
        return (
            <div>
                <Modal trigger={ <Icon name='tags' /> } >
                    <Modal.Header>
                        <div>
                            <Header floated='left'>Title</Header>
                            <Button floated='right'>A Button</Button>
                        </div>
                    </Modal.Header>
                    <Modal.Content>

                        <ModalContent />

                    </Modal.Content>
                </Modal>
            </div>
        )
    }
}

Modal content

class ModalContent extends Component {
    constructor(props) {
        super(props)
        this.state = {}
    }

    handleClick() {
        method.call(
            { param },
            (error, result) => {
                if (result) {
                    // Now close the modal
                }
            }
        );
    }

    render() {
        return (
            <Button onClick={this.handleClick} content='Save' />
        )
    }
}
like image 868
user3142695 Avatar asked Feb 21 '17 11:02

user3142695


People also ask

How do you close a modal in react?

To close the modal, simply call the handleClose() function inside the onLoginFormSubmit() function body.

How does react handle modal state?

Use props . You should manage the Modal's open state in Products and pass the value to Modal as a prop whenever a product is clicked. Add a onClose prop to Modal and create a function to call setOpen(false) in Product whenever the modal is closed.

Does react have a modal component?

The modal component provides a solid foundation for creating dialogs, lightboxes, popovers, etc. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Modal Component in ReactJS using the following approach.


3 Answers

You should add an onClose property to <Modal> element. See example below:

<Modal
    trigger={<Button onClick={this.handleOpen}>Show Modal</Button>}
    open={this.state.modalOpen}
    onClose={this.handleClose}
  >

Then you can add onClose function to a button in your modal. Full example from the docs: https://react.semantic-ui.com/modules/modal#modal-example-controlled

like image 172
Deividas Karzinauskas Avatar answered Oct 31 '22 10:10

Deividas Karzinauskas


Pass a onSuccess method as a props :

in the parent :

 <ModalContent onSuccess={this.onModalSuccess}/>

in the child component :

handleClick() {
   method.call(
        { param },
        (error, result) => {
            if (result) {
                this.props.onSuccess()
            }
        }
    );
}

In this way you keep your open/close logic in the parent component.

like image 1
Anthony Ivol Avatar answered Oct 31 '22 10:10

Anthony Ivol


semantic-ui have property open. Just set true or false

class Example extends Component {
  constructor(props) {
    super(props)
    this.state = {
      open: false
  }
  open = () => this.setState({ open: true })
  close = () => this.setState({ open: false })
  render() {
    return (
        <div>
            <Modal open={this.state.open} trigger={ <Icon name='tags' /> } >
                <Modal.Header>
                    <div>
                        <Header floated='left'>Title</Header>
                        <Button floated='right'>A Button</Button>
                    </div>
                </Modal.Header>
                <Modal.Content>

                    <ModalContent />

                </Modal.Content>
            </Modal>
        </div>
    )
  }
}
like image 1
Simonov Dmitry Avatar answered Oct 31 '22 11:10

Simonov Dmitry