Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80698 views
1
'use strict';
2
3
// Simple pure-React component so we don't have to remember
4
// Bootstrap's classes
5
var BootstrapButton = React.createClass({
6
render: function() {
7
return (
8
<a {...this.props}
9
href="javascript:;"
10
role="button"
11
className={(this.props.className || '') + ' btn'} />
12
);
13
}
14
});
15
16
var BootstrapModal = React.createClass({
17
// The following two methods are the only places we need to
18
// integrate Bootstrap or jQuery with the components lifecycle methods.
19
componentDidMount: function() {
20
// When the component is added, turn it into a modal
21
$(React.findDOMNode(this))
22
.modal({backdrop: 'static', keyboard: false, show: false});
23
},
24
componentWillUnmount: function() {
25
$(React.findDOMNode(this)).off('hidden', this.handleHidden);
26
},
27
close: function() {
28
$(React.findDOMNode(this)).modal('hide');
29
},
30
open: function() {
31
$(React.findDOMNode(this)).modal('show');
32
},
33
render: function() {
34
var confirmButton = null;
35
var cancelButton = null;
36
37
if (this.props.confirm) {
38
confirmButton = (
39
<BootstrapButton
40
onClick={this.handleConfirm}
41
className="btn-primary">
42
{this.props.confirm}
43
</BootstrapButton>
44
);
45
}
46
if (this.props.cancel) {
47
cancelButton = (
48
<BootstrapButton onClick={this.handleCancel} className="btn-default">
49
{this.props.cancel}
50
</BootstrapButton>
51
);
52
}
53
54
return (
55
<div className="modal fade">
56
<div className="modal-dialog">
57
<div className="modal-content">
58
<div className="modal-header">
59
<button
60
type="button"
61
className="close"
62
onClick={this.handleCancel}>
63
&times;
64
</button>
65
<h3>{this.props.title}</h3>
66
</div>
67
<div className="modal-body">
68
{this.props.children}
69
</div>
70
<div className="modal-footer">
71
{cancelButton}
72
{confirmButton}
73
</div>
74
</div>
75
</div>
76
</div>
77
);
78
},
79
handleCancel: function() {
80
if (this.props.onCancel) {
81
this.props.onCancel();
82
}
83
},
84
handleConfirm: function() {
85
if (this.props.onConfirm) {
86
this.props.onConfirm();
87
}
88
}
89
});
90
91
var Example = React.createClass({
92
handleCancel: function() {
93
if (confirm('Are you sure you want to cancel?')) {
94
this.refs.modal.close();
95
}
96
},
97
render: function() {
98
var modal = null;
99
modal = (
100
<BootstrapModal
101
ref="modal"
102
confirm="OK"
103
cancel="Cancel"
104
onCancel={this.handleCancel}
105
onConfirm={this.closeModal}
106
title="Hello, Bootstrap!">
107
This is a React component powered by jQuery and Bootstrap!
108
</BootstrapModal>
109
);
110
return (
111
<div className="example">
112
{modal}
113
<BootstrapButton onClick={this.openModal} className="btn-default">
114
Open modal
115
</BootstrapButton>
116
</div>
117
);
118
},
119
openModal: function() {
120
this.refs.modal.open();
121
},
122
closeModal: function() {
123
this.refs.modal.close();
124
}
125
});
126
127
React.render(<Example />, document.getElementById('jqueryexample'));
128
129