Semantic UI modal component onClose with React

血红的双手。 提交于 2019-12-22 23:29:02

问题


I need a way to define behavior on a Semantic modal that gets executed when it closes.

What I'm doing now uses a 'portal', but I think the "onClick" event doesn't work because these html elements are outside of react.

I had:

componentDidMount() {
  console.log('mounting modal', this);
  this.node = React.findDOMNode(this);
  this.$modal = $(this.node);

  this.$icon = $("<i class='close icon' /></i>");

  this.$header = $("<div class='header'></div>").html(this.props.header);
  this.$content = $("<div class='content'></div>");

  this.$modal.append(this.$header);
  this.$modal.append(this.$icon);
  this.$modal.append(this.$content);

  this.renderDialogContent(this.props);
}

componentWillReceiveProps(newProps) {
  this.renderDialogContent(newProps);
}

renderDialogContent(props) {
  props = props || this.props;

  React.render(<div>{props.children}</div>, this.$content[0]);

  if (props.isOpen) {
    this.$modal
        .modal('setting', 'closable', false)
        .modal('show');
  }
   else {
  this.$modal.modal('hide modal');
  }
}

How do I define that behavior?

Here's a fiddle with some similar code.


回答1:


For some reason, I'm unsure why, you can't use the regular React event handlers inside the modal view.

So on the close icon, I registered a jquery onClick handler.

$('#' + id).click(this.props.close);

and I passed close in bound to the parent component.



来源:https://stackoverflow.com/questions/31172613/semantic-ui-modal-component-onclose-with-react

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