javascript – 关闭React语义UI模式与按钮和关闭图标

前端之家收集整理的这篇文章主要介绍了javascript – 关闭React语义UI模式与按钮和关闭图标前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个模态,用户需要填写一些表格,并通过模态中的按钮保存填写的内容.当用户保存时,我希望关闭模态.通过使用Modal组件上的open prop,我可以完成这项工作.但是,如果我这样做,当我尝试通过closeIcon时,模态不会关闭.

我该怎么做才能让用户通过这两种方法关闭Modal?

这是我目前的模态代码

handleCreateButton (evt) {
    evt.preventDefault()
    // ...
    // code to save whatever was typed in the form
    // ...

    this.setState({showModal: false})
  }

  renderModalForm () {
    const {
      something,showModal
    } = this.state

    // if I have the open props,I get to close the Modal after the button is clicked
    // however,when using the icon or clicking on dimmer it wont work anymore.
    return (
      <Modal closeIcon cloSEOnDimmerClick open={showModal} trigger={<Button onClick={() => this.setState({showModal: true})}><Icon className='plus'/>New Challenge</Button>}>
        <Modal.Header>My Modal</Modal.Header>
        <Modal.Content>
          <Form>
            <Form.Input
              label='Something'
              value={something}
              onChange={(evt) => this.handleChangeForms('something',evt.target.value)}
            />
            <Button onClick={(evt) => this.handleCreateButton(evt)}>Save</Button>
          </Form>
        </Modal.Content>
      </Modal>
    )
  }

解决方法

当你使用open prop时,你也需要使用onClose处理程序prop.
顺便说一下,cloSEOnDimmerClick默认设置为true.

这是一个运行的例子:

const { Modal,Form,Button,Icon } = semanticUIReact;

class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      something: '',showModal: false
    }
  }

  handleChangeForms = (e,{ value }) => {
    this.setState({ something: value });
  }

  handleCreateButton(evt) {
    evt.preventDefault()
    this.closeModal();
  }

  closeModal = () => {
    this.setState({ showModal: false })
  }

  render() {
    const {
      something,showModal
    } = this.state

    return (
      <Modal closeIcon onClose={this.closeModal} open={showModal} trigger={<Button onClick={() => this.setState({ showModal: true })}><Icon className='plus' />New Challenge</Button>}>
        <Modal.Header>My Modal</Modal.Header>
        <Modal.Content>
          <Form>
            <Form.Input
              label='Something'
              value={something}
              onChange={this.handleChangeForms}
            />
            <Button onClick={(evt) => this.handleCreateButton(evt)}>Save</Button>
          </Form>
        </Modal.Content>
      </Modal>
    )
  }
}

ReactDOM.render(<App />,document.getElementById("root"));
<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/semantic-ui-react@0.77.1/dist/umd/semantic-ui-react.min.js"></script>
<div id="root"></div>

猜你在找的JavaScript相关文章