forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodal.js
More file actions
141 lines (127 loc) · 4.05 KB
/
Copy pathmodal.js
File metadata and controls
141 lines (127 loc) · 4.05 KB
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
import React from 'react'
import $ from 'jquery'
import _ from 'underscore'
import preventDefault from 'compiled/fn/preventDefault'
import ReactModal from 'react-modal'
import ModalContent from './modal-content'
import ModalButtons from './modal-buttons'
const modalOverrides = {
overlay : {
backgroundColor: 'rgba(0,0,0,0.5)'
},
content : {
position: 'static',
top: '0',
left: '0',
right: 'auto',
bottom: 'auto',
borderRadius: '0',
border: 'none',
padding: '0'
}
};
const Modal = React.createClass({
getInitialState() {
return {
modalIsOpen: this.props.isOpen
}
},
getDefaultProps(){
return {
className: "ReactModal__Content--canvas", // Override with "ReactModal__Content--canvas ReactModal__Content--mini-modal" for a mini modal
style: {},
};
},
componentWillReceiveProps(props){
let callback
if (this.props.isOpen && !props.isOpen) callback = this.cleanupAfterClose
this.setState({modalIsOpen: props.isOpen}, callback);
},
openModal() {
this.setState({modalIsOpen: true});
},
cleanupAfterClose () {
if (this.props.onRequestClose) this.props.onRequestClose();
$(this.getAppElement()).removeAttr('aria-hidden');
},
closeModal() {
this.setState({modalIsOpen: false}, this.cleanupAfterClose);
},
closeWithX() {
if(_.isFunction(this.props.closeWithX))
this.props.closeWithX()
this.closeModal();
},
onSubmit(){
const promise = this.props.onSubmit();
$(this.modal).disableWhileLoading(promise);
},
getAppElement () {
// Need to wait for the dom to load before we can get the default #application dom element
return this.props.appElement || document.getElementById('application');
},
processMultipleChildren(props){
let content = null;
let buttons = null;
React.Children.forEach(props.children, function(child){
if(child.type == ModalContent){
content = child;
}
if(child.type == ModalButtons){
buttons = child;
}
});
// Warning if you don't include a component of the right type
if(content == null){
console.warn('You should wrap your content in the modal-content component');
}
if(buttons == null){
console.warn('You should wrap your buttons in the modal-buttons component');
}
if(this.props.onSubmit){
return (
<form className="ModalForm" onSubmit={preventDefault(this.onSubmit)}>
{ [content, buttons] }
</form>
)
}
else
{
return [content, buttons]; // This order needs to be maintained
}
},
render() {
return (
<div className="canvasModal">
<ReactModal
ariaHideApp={!!this.state.modalIsOpen}
isOpen={!!this.state.modalIsOpen}
onRequestClose={this.closeModal}
className={this.props.className}
style={modalOverrides}
overlayClassName={this.props.overlayClassName}
contentLabel={this.props.contentLabel}
appElement={this.getAppElement()}>
<div ref={(c) => { this.modal = c }}
className="ReactModal__Layout"
style={this.props.style}
>
<div className="ReactModal__Header">
<div className="ReactModal__Header-Title">
<h4>{this.props.title}</h4>
</div>
<div className="ReactModal__Header-Actions">
<button ref={(c) => { this.closeBtn = c }} className="Button Button--icon-action" type="button" onClick={this.closeWithX}>
<i className="icon-x"></i>
<span className="screenreader-only">Close</span>
</button>
</div>
</div>
{this.processMultipleChildren(this.props)}
</div>
</ReactModal>
</div>
);
}
});
export default Modal