【发布时间】:2015-04-06 12:30:15
【问题描述】:
我需要像这样打开/关闭模态
$(element).modal('show')
怎么做?
【问题讨论】:
标签: twitter-bootstrap reactjs react-bootstrap
我需要像这样打开/关闭模态
$(element).modal('show')
怎么做?
【问题讨论】:
标签: twitter-bootstrap reactjs react-bootstrap
您正在寻找的是自定义模态触发器,它使用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 属性控制。不再需要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>
);
}
});
【讨论】:
refs访问模式中的表单元素,则不起作用
您的模态将有一个show 属性和一个onHide 属性来确定它何时显示。例如:
<Modal show={this.state.showModal} onHide={this.close}>
onHide 函数只是更改showModal 状态属性。您的模式会根据父母的状态显示/隐藏:
close(){
this.setState({ showModal: false });
}
如果您想从模态本身内部关闭模态,您可以通过props 触发父级上定义的onHide 函数。例如,这是模态框内某处关闭它的按钮:
<button type="button" className="close" onClick={this.props.onHide}>
<span>×</span>
</button>
Here is a fiddle 模拟此工作流程。
【讨论】:
从状态以编程/动态方式打开和关闭 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;
【讨论】: