modal-dialog - ReactJs:使用 Portal 包装语义 UI 模态 "pattern"

标签 modal-dialog reactjs portal

我正在尝试使用描述的门户方法包装语义 UI 模态组件 here

这是我的看法 http://jsfiddle.net/mike_123/2wvfjpy9/ 不过,我遇到了问题,当获取 DOM 引用并将新标记渲染到其中时,似乎仍保留旧引用。

render: function() {
    return <div className="ui modal"/>; <-- the idea at first was to only return <div/>
},

...

        React.render(<div > <----------- originally this element had className="ui modal", but this.node doesn't seem to overtake the original node reference
                    <i className="close icon"></i>
                    <div className="header">test</div>
                    <div className="content">
                        {props.children}
                    </div>
                </div>, <----------- 
                this.node);

如何修复此测试用例的任何指示 http://jsfiddle.net/mike_123/2wvfjpy9/

最佳答案

使用上述方法,您将失去正确的垂直定位,甚至可能失去动画。

相反,您可以将模态组件放置在应用的根组件中,并使用 detachable: false 调用 .modal()。使用此选项,语义不会进行任何 DOM 操作,并且您不会丢失 React DOM 事件监听器。

使用 Webpack/Babel 的示例:

import React, { Component } from 'react'
import $ from 'jquery'

if (typeof window !== 'undefined') {
  window.jQuery = $
  require('semantic-ui/dist/semantic.js')
}

class App extends Component {
  state = {
    showModal: false
  }

  _toggleModal = (e) => {
    e.preventDefault()
    this.toggleModalState()
  }

  toggleModalState = () => {
      this.setState({ showModal: !this.state.showModal })  
  }

  render() {
    return (
      <div>
        <a href="" onClick={this._toggleModal}></a>
        {this.state.showModal
          ? <Modal toggleModalState={this.toggleModalState}/>
          : ''}
      </div>
    ) 
  }
}

class Modal extends Component {
  componentDidMount() {   
    $(this.modal)
      .modal({ detachable: false })
      .modal('show')
  }

  componentWillUnmount() {   
    $(this.modal).modal('hide')
  }

  _close = (e) {
    e.preventDefault()
    alert("Clicked")
    this.props.toggleModalState()
  }

  render() {
    return (
      <div ref={(n) => this.modal = n} className="ui modal">
        <div class="content">
          <a onClick={this._close} href="">Click Me</a>
        </div>
      </div>
    )
  }
 }

关于modal-dialog - ReactJs:使用 Portal 包装语义 UI 模态 "pattern",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063995/

相关文章:

jQuery BlockUI : Is it possible to run a function after a user hits the "enter" key?

javascript - 在 Liferay 5.2.3 中禁用 JavaScript minifier 时出现问题

ios - 如何让将以模态方式呈现的 UIViewController 的 UIView 始终出现在屏幕底部?

c# - 以编程方式查明进程是否需要用户输入

javascript - ReactJS Koans 杂货 list 第 1 部分

javascript - 使用事件监听器响应自定义下拉列表

javascript - 如何使用帖子:title in url instead of :id in React. js

java - 门户网站包含很多静态页面,如何生成这些页面呢?

javascript - 使用 Angular4 创建即插即用插件框架设置

javascript - 购物篮项目未在模式弹出窗口内更新