How to close semantic ui modal in another react component?

徘徊边缘 提交于 2019-12-12 10:38:54

问题


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' />
        )
    }
}

回答1:


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




回答2:


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>
    )
  }
}



回答3:


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.



来源:https://stackoverflow.com/questions/42366328/how-to-close-semantic-ui-modal-in-another-react-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!