forked from react-bootstrap/react-bootstrap-bower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModal.js
159 lines (132 loc) · 4.26 KB
/
Modal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
define(function (require, exports, module) {/* global document:false */
var React = require('react');
var joinClasses = require('./utils/joinClasses');
var classSet = require('./utils/classSet');
var BootstrapMixin = require('./BootstrapMixin');
var FadeMixin = require('./FadeMixin');
var EventListener = require('./utils/EventListener');
// TODO:
// - aria-labelledby
// - Add `modal-body` div if only one child passed in that doesn't already have it
// - Tests
var Modal = React.createClass({displayName: 'Modal',
mixins: [BootstrapMixin, FadeMixin],
propTypes: {
title: React.PropTypes.node,
backdrop: React.PropTypes.oneOf(['static', true, false]),
keyboard: React.PropTypes.bool,
closeButton: React.PropTypes.bool,
animation: React.PropTypes.bool,
onRequestHide: React.PropTypes.func.isRequired
},
getDefaultProps: function () {
return {
bsClass: 'modal',
backdrop: true,
keyboard: true,
animation: true,
closeButton: true
};
},
render: function () {
var modalStyle = {display: 'block'};
var dialogClasses = this.getBsClassSet();
delete dialogClasses.modal;
dialogClasses['modal-dialog'] = true;
var classes = {
modal: true,
fade: this.props.animation,
'in': !this.props.animation || !document.querySelectorAll
};
var modal = (
React.createElement("div", React.__spread({},
this.props,
{title: null,
tabIndex: "-1",
role: "dialog",
style: modalStyle,
className: joinClasses(this.props.className, classSet(classes)),
onClick: this.props.backdrop === true ? this.handleBackdropClick : null,
ref: "modal"}),
React.createElement("div", {className: classSet(dialogClasses)},
React.createElement("div", {className: "modal-content"},
this.props.title ? this.renderHeader() : null,
this.props.children
)
)
)
);
return this.props.backdrop ?
this.renderBackdrop(modal) : modal;
},
renderBackdrop: function (modal) {
var classes = {
'modal-backdrop': true,
'fade': this.props.animation
};
classes['in'] = !this.props.animation || !document.querySelectorAll;
var onClick = this.props.backdrop === true ?
this.handleBackdropClick : null;
return (
React.createElement("div", null,
React.createElement("div", {className: classSet(classes), ref: "backdrop", onClick: onClick}),
modal
)
);
},
renderHeader: function () {
var closeButton;
if (this.props.closeButton) {
closeButton = (
React.createElement("button", {type: "button", className: "close", 'aria-hidden': "true", onClick: this.props.onRequestHide}, "×")
);
}
return (
React.createElement("div", {className: "modal-header"},
closeButton,
this.renderTitle()
)
);
},
renderTitle: function () {
return (
React.isValidElement(this.props.title) ?
this.props.title : React.createElement("h4", {className: "modal-title"}, this.props.title)
);
},
iosClickHack: function () {
// IOS only allows click events to be delegated to the document on elements
// it considers 'clickable' - anchors, buttons, etc. We fake a click handler on the
// DOM nodes themselves. Remove if handled by React: https://github.com/facebook/react/issues/1169
this.refs.modal.getDOMNode().onclick = function () {};
this.refs.backdrop.getDOMNode().onclick = function () {};
},
componentDidMount: function () {
this._onDocumentKeyupListener =
EventListener.listen(document, 'keyup', this.handleDocumentKeyUp);
if (this.props.backdrop) {
this.iosClickHack();
}
},
componentDidUpdate: function (prevProps) {
if (this.props.backdrop && this.props.backdrop !== prevProps.backdrop) {
this.iosClickHack();
}
},
componentWillUnmount: function () {
this._onDocumentKeyupListener.remove();
},
handleBackdropClick: function (e) {
if (e.target !== e.currentTarget) {
return;
}
this.props.onRequestHide();
},
handleDocumentKeyUp: function (e) {
if (this.props.keyboard && e.keyCode === 27) {
this.props.onRequestHide();
}
}
});
module.exports = Modal;
});