1 | import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
|
2 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
3 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
4 | import _inherits from 'babel-runtime/helpers/inherits';
|
5 | import _extends from 'babel-runtime/helpers/extends';
|
6 | import classNames from 'classnames';
|
7 | import events from 'dom-helpers/events';
|
8 | import ownerDocument from 'dom-helpers/ownerDocument';
|
9 | import canUseDOM from 'dom-helpers/util/inDOM';
|
10 | import getScrollbarSize from 'dom-helpers/util/scrollbarSize';
|
11 | import React from 'react';
|
12 | import PropTypes from 'prop-types';
|
13 | import ReactDOM from 'react-dom';
|
14 | import BaseModal from 'react-overlays/lib/Modal';
|
15 | import isOverflowing from 'react-overlays/lib/utils/isOverflowing';
|
16 | import elementType from 'prop-types-extra/lib/elementType';
|
17 |
|
18 | import Fade from './Fade';
|
19 | import Body from './ModalBody';
|
20 | import ModalDialog from './ModalDialog';
|
21 | import Footer from './ModalFooter';
|
22 | import Header from './ModalHeader';
|
23 | import Title from './ModalTitle';
|
24 | import { bsClass, bsSizes, prefix } from './utils/bootstrapUtils';
|
25 | import createChainedFunction from './utils/createChainedFunction';
|
26 | import splitComponentProps from './utils/splitComponentProps';
|
27 | import { Size } from './utils/StyleConfig';
|
28 |
|
29 | var propTypes = _extends({}, BaseModal.propTypes, ModalDialog.propTypes, {
|
30 |
|
31 | |
32 |
|
33 |
|
34 |
|
35 | backdrop: PropTypes.oneOf(['static', true, false]),
|
36 |
|
37 | |
38 |
|
39 |
|
40 |
|
41 | backdropClassName: PropTypes.string,
|
42 |
|
43 | |
44 |
|
45 |
|
46 | keyboard: PropTypes.bool,
|
47 |
|
48 | |
49 |
|
50 |
|
51 | animation: PropTypes.bool,
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 | dialogComponentClass: elementType,
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | autoFocus: PropTypes.bool,
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 | enforceFocus: PropTypes.bool,
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 | restoreFocus: PropTypes.bool,
|
80 |
|
81 | |
82 |
|
83 |
|
84 | show: PropTypes.bool,
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 | onHide: PropTypes.func,
|
91 |
|
92 | |
93 |
|
94 |
|
95 | onEnter: PropTypes.func,
|
96 |
|
97 | |
98 |
|
99 |
|
100 | onEntering: PropTypes.func,
|
101 |
|
102 | |
103 |
|
104 |
|
105 | onEntered: PropTypes.func,
|
106 |
|
107 | |
108 |
|
109 |
|
110 | onExit: PropTypes.func,
|
111 |
|
112 | |
113 |
|
114 |
|
115 | onExiting: PropTypes.func,
|
116 |
|
117 | |
118 |
|
119 |
|
120 | onExited: PropTypes.func,
|
121 |
|
122 | |
123 |
|
124 |
|
125 | container: BaseModal.propTypes.container
|
126 | });
|
127 |
|
128 | var defaultProps = _extends({}, BaseModal.defaultProps, {
|
129 | animation: true,
|
130 | dialogComponentClass: ModalDialog
|
131 | });
|
132 |
|
133 | var childContextTypes = {
|
134 | $bs_modal: PropTypes.shape({
|
135 | onHide: PropTypes.func
|
136 | })
|
137 | };
|
138 |
|
139 |
|
140 | function DialogTransition(props) {
|
141 | return React.createElement(Fade, _extends({}, props, { timeout: Modal.TRANSITION_DURATION }));
|
142 | }
|
143 |
|
144 | function BackdropTransition(props) {
|
145 | return React.createElement(Fade, _extends({}, props, { timeout: Modal.BACKDROP_TRANSITION_DURATION }));
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 | var Modal = function (_React$Component) {
|
151 | _inherits(Modal, _React$Component);
|
152 |
|
153 | function Modal(props, context) {
|
154 | _classCallCheck(this, Modal);
|
155 |
|
156 | var _this = _possibleConstructorReturn(this, _React$Component.call(this, props, context));
|
157 |
|
158 | _this.handleEntering = _this.handleEntering.bind(_this);
|
159 | _this.handleExited = _this.handleExited.bind(_this);
|
160 | _this.handleWindowResize = _this.handleWindowResize.bind(_this);
|
161 | _this.handleDialogClick = _this.handleDialogClick.bind(_this);
|
162 | _this.setModalRef = _this.setModalRef.bind(_this);
|
163 |
|
164 | _this.state = {
|
165 | style: {}
|
166 | };
|
167 | return _this;
|
168 | }
|
169 |
|
170 | Modal.prototype.getChildContext = function getChildContext() {
|
171 | return {
|
172 | $bs_modal: {
|
173 | onHide: this.props.onHide
|
174 | }
|
175 | };
|
176 | };
|
177 |
|
178 | Modal.prototype.componentWillUnmount = function componentWillUnmount() {
|
179 |
|
180 | this.handleExited();
|
181 | };
|
182 |
|
183 | Modal.prototype.setModalRef = function setModalRef(ref) {
|
184 | this._modal = ref;
|
185 | };
|
186 |
|
187 | Modal.prototype.handleDialogClick = function handleDialogClick(e) {
|
188 | if (e.target !== e.currentTarget) {
|
189 | return;
|
190 | }
|
191 |
|
192 | this.props.onHide();
|
193 | };
|
194 |
|
195 | Modal.prototype.handleEntering = function handleEntering() {
|
196 |
|
197 | events.on(window, 'resize', this.handleWindowResize);
|
198 | this.updateStyle();
|
199 | };
|
200 |
|
201 | Modal.prototype.handleExited = function handleExited() {
|
202 |
|
203 | events.off(window, 'resize', this.handleWindowResize);
|
204 | };
|
205 |
|
206 | Modal.prototype.handleWindowResize = function handleWindowResize() {
|
207 | this.updateStyle();
|
208 | };
|
209 |
|
210 | Modal.prototype.updateStyle = function updateStyle() {
|
211 | if (!canUseDOM) {
|
212 | return;
|
213 | }
|
214 |
|
215 | var dialogNode = this._modal.getDialogElement();
|
216 | var dialogHeight = dialogNode.scrollHeight;
|
217 |
|
218 | var document = ownerDocument(dialogNode);
|
219 | var bodyIsOverflowing = isOverflowing(ReactDOM.findDOMNode(this.props.container || document.body));
|
220 | var modalIsOverflowing = dialogHeight > document.documentElement.clientHeight;
|
221 |
|
222 | this.setState({
|
223 | style: {
|
224 | paddingRight: bodyIsOverflowing && !modalIsOverflowing ? getScrollbarSize() : undefined,
|
225 | paddingLeft: !bodyIsOverflowing && modalIsOverflowing ? getScrollbarSize() : undefined
|
226 | }
|
227 | });
|
228 | };
|
229 |
|
230 | Modal.prototype.render = function render() {
|
231 | var _props = this.props,
|
232 | backdrop = _props.backdrop,
|
233 | backdropClassName = _props.backdropClassName,
|
234 | animation = _props.animation,
|
235 | show = _props.show,
|
236 | Dialog = _props.dialogComponentClass,
|
237 | className = _props.className,
|
238 | style = _props.style,
|
239 | children = _props.children,
|
240 | onEntering = _props.onEntering,
|
241 | onExited = _props.onExited,
|
242 | props = _objectWithoutProperties(_props, ['backdrop', 'backdropClassName', 'animation', 'show', 'dialogComponentClass', 'className', 'style', 'children', 'onEntering', 'onExited']);
|
243 |
|
244 | var _splitComponentProps = splitComponentProps(props, BaseModal),
|
245 | baseModalProps = _splitComponentProps[0],
|
246 | dialogProps = _splitComponentProps[1];
|
247 |
|
248 | var inClassName = show && !animation && 'in';
|
249 |
|
250 | return React.createElement(
|
251 | BaseModal,
|
252 | _extends({}, baseModalProps, {
|
253 | ref: this.setModalRef,
|
254 | show: show,
|
255 | containerClassName: prefix(props, 'open'),
|
256 | transition: animation ? DialogTransition : undefined,
|
257 | backdrop: backdrop,
|
258 | backdropTransition: animation ? BackdropTransition : undefined,
|
259 | backdropClassName: classNames(prefix(props, 'backdrop'), backdropClassName, inClassName),
|
260 | onEntering: createChainedFunction(onEntering, this.handleEntering),
|
261 | onExited: createChainedFunction(onExited, this.handleExited)
|
262 | }),
|
263 | React.createElement(
|
264 | Dialog,
|
265 | _extends({}, dialogProps, {
|
266 | style: _extends({}, this.state.style, style),
|
267 | className: classNames(className, inClassName),
|
268 | onClick: backdrop === true ? this.handleDialogClick : null
|
269 | }),
|
270 | children
|
271 | )
|
272 | );
|
273 | };
|
274 |
|
275 | return Modal;
|
276 | }(React.Component);
|
277 |
|
278 | Modal.propTypes = propTypes;
|
279 | Modal.defaultProps = defaultProps;
|
280 | Modal.childContextTypes = childContextTypes;
|
281 |
|
282 | Modal.Body = Body;
|
283 | Modal.Header = Header;
|
284 | Modal.Title = Title;
|
285 | Modal.Footer = Footer;
|
286 |
|
287 | Modal.Dialog = ModalDialog;
|
288 |
|
289 | Modal.TRANSITION_DURATION = 300;
|
290 | Modal.BACKDROP_TRANSITION_DURATION = 150;
|
291 |
|
292 | export default bsClass('modal', bsSizes([Size.LARGE, Size.SMALL], Modal)); |
\ | No newline at end of file |