我需要像这样打开/关闭模态
$(element).modal('show')
怎么做?
我需要像这样打开/关闭模态
$(element).modal('show')
怎么做?
您正在寻找的是自定义模态触发器,它使用OverlayMixin
并允许您自己管理模态的状态。您可以控制是否显示模态this.setState({isModalOpen: true})
以实现您在以下示例中的帖子中要求的内容。以下代码来自 react-bootstrap 网站 ( http://react-bootstrap.github.io/components.html#modals ):
const CustomModalTrigger = React.createClass({
mixins: [OverlayMixin],
getInitialState() {
return {
isModalOpen: false
};
},
handleToggle() {
this.setState({
isModalOpen: !this.state.isModalOpen
});
},
render() {
return (
<Button onClick={this.handleToggle} bsStyle='primary'>Launch</Button>
);
},
// This is called by the `OverlayMixin` when this component
// is mounted or updated and the return value is appended to the body.
renderOverlay() {
if (!this.state.isModalOpen) {
return <span/>;
}
return (
<Modal bsStyle='primary' title='Modal heading' onRequestHide={this.handleToggle}>
<div className='modal-body'>
This modal is controlled by our custom trigger component.
</div>
<div className='modal-footer'>
<Button onClick={this.handleToggle}>Close</Button>
</div>
</Modal>
);
}
});
React.render(<CustomModalTrigger />, mountNode);
更新(2015 年 8 月 4 日)
在最新版本的 React-Bootstrap 中,是否显示模态是由show
传递给模态的prop控制的。将OverlayMixin
不再需要控制模式状态。控制模态的状态仍然是通过 完成的setState
,在这个例子中是通过this.setState({ showModal: true })
。以下内容基于 React-Bootstrap 网站上的示例:
const ControlledModalExample = React.createClass({
getInitialState(){
return { showModal: false };
},
close(){
this.setState({ showModal: false });
},
open(){
this.setState({ showModal: true });
},
render() {
return (
<div>
<Button onClick={this.open}>
Launch modal
</Button>
<Modal show={this.state.showModal} onHide={this.close}>
<Modal.Header closeButton>
<Modal.Title>Modal heading</Modal.Title>
</Modal.Header>
<Modal.Body>
<div>Modal content here </div>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.close}>Close</Button>
</Modal.Footer>
</Modal>
</div>
);
}
});
您的模态将有一个show
props和一个onHide
props来确定它何时显示。例如:
<Modal show={this.state.showModal} onHide={this.close}>
该onHide
函数只是更改showModal
状态属性。您的模态是根据父级的状态显示/隐藏的:
close(){
this.setState({ showModal: false });
}
如果您想从模态本身内部关闭模态,您可以onHide
通过props
. 例如,这是模态内某处的一个按钮来关闭它:
<button type="button" className="close" onClick={this.props.onHide}>
<span>×</span>
</button>
从状态以编程方式/动态方式打开和关闭 react-bootstrap 模式:
这里我使用了 ES6 语法类组件语法。
import React, { Component, PropTypes } from 'react';
import { Modal, Button } from 'react-bootstrap';
import './GenericModal.scss';
class GenericModal extends Component {
constructor(props, context) {
super(props, context);
this.state = {
showModal: false
};
this.open = this.open.bind(this);
this.close = this.close.bind(this);
}
open() {
this.setState({showModal: true});
}
close() {
this.setState({showModal: false});
}
render() {
return(
<div>
<div>I am a Bootstrap Modal</div>
<Button onClick={this.open}>Show Modal</Button>
<div>
<Modal className="modal-container"
show={this.state.showModal}
onHide={this.close}
animation={true}
bsSize="small">
<Modal.Header closeButton>
<Modal.Title>Modal title</Modal.Title>
</Modal.Header>
<Modal.Body>
One of fine body.........
</Modal.Body>
<Modal.Footer>
<Button onClick={this.close}>Close</Button>
<Button bsStyle="primary">Save changes</Button>
</Modal.Footer>
</Modal>
</div>
</div>
);
}
}
export default GenericModal;
你可以用 React hooks来做到这一点。(包含在 React 16.8 中)
import React, { useState } from "react";
import { Modal, Button } from "react-bootstrap";
const ComponentWithModal = props => {
const [isModalOpen, setModalStatus] = useState(false);
return (
<div>
<div>I am a Bootstrap Modal</div>
<Button onClick={() => setModalStatus(true)}>Show Modal</Button>
<div>
<Modal
className="modal-container"
show={isModalOpen}
onHide={() => setModalStatus(false)}
>
<Modal.Header closeButton>
<Modal.Title>Modal title</Modal.Title>
</Modal.Header>
<Modal.Body>Modal content here</Modal.Body>
<Modal.Footer>
<Button onClick={() => setModalStatus(false)}>Close</Button>
</Modal.Footer>
</Modal>
</div>
</div>
);
};
export default ComponentWithModal;