forked from jrop/material-ui-dialogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-source.js
172 lines (153 loc) · 4.14 KB
/
index-source.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
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { PureComponent } from 'react';
import ReactDOM from 'react-dom';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import Dialog from 'material-ui/Dialog';
import FlatButton from 'material-ui/FlatButton';
import TextField from 'material-ui/TextField';
class Deferred {
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
})
}
}
/**
* Note: the end of material-ui's Dialog.positionDialog must be
*
if (dialogContent.offsetHeight > maxDialogContentHeight) {
dialogContent.style.height = maxDialogContentHeight + 'px';
}
dialogContent.style.maxHeight = maxDialogContentHeight + 'px';
*/
class PromisifiedDialog extends PureComponent {
constructor(props) {
super(props);
this.state = { open: true };
}
cleanup() {
this.setState({ open: false });
//---- cleanup after Dialog closing animation ----//
setTimeout(() => {
ReactDOM.unmountComponentAtNode(this.props.div)
document.body.removeChild(this.props.div)
}, 2000)
}
resolve(value) {
this.props.deferred.resolve(value);
this.cleanup();
}
render() {
const actions = this.props.actions.map(action => React.cloneElement(action, {
onClick: () => action.props.action(value => this.resolve(value) , this._contentComponent)
}));
return (
<MuiThemeProvider muiTheme={getMuiTheme()}>
<Dialog
open={this.state.open}
actions={actions}
title={this.props.title}
bodyStyle={this.props.contentStyle}
autoScrollBodyContent={this.props.autoScrollBodyContent || false}
>
{React.cloneElement(this.props.content, { ref: content => this._contentComponent = content })}
</Dialog>
</MuiThemeProvider>
)
}
}
/**
* @param {object} options :
* {Array<ReactElement>} actions each action contains an 'action' event callback with the signature (resolve, contentComponent):void
* {string} title,
* {ReactElement} content
* {boolean} autoScrollBodyContent
* {object} contentStyle
* @return {Promise}
*/
export function showDialog(options) {
const deferred = new Deferred();
const div = document.createElement('div');
document.body.appendChild(div);
const ref = promisifiedDialog => {
if (options.resolveRef) {
options.resolveRef(value => {
promisifiedDialog.resolve(value);
});
}
}
ReactDOM.render(<PromisifiedDialog ref={ref} deferred={deferred} div={div} {...options} />, div)
return deferred.promise;
}
//------------------------ alert -------------------------//
export function alert({title = 'Alert', content}) {
return showDialog({
title,
actions: [
<FlatButton
label="Ok"
primary={true}
action={ (resolve, contentComponent) => resolve() }
/>
],
content: <div>{content}</div>
});
}
//------------------------ confirm -------------------------//
export function confirm({title = 'Confirmation', message}) {
return showDialog({
title,
actions: [
<FlatButton
label="Cancel"
secondary={true}
action={ (resolve, contentComponent) => resolve(false) } />,
<FlatButton
label="Confirm"
primary={true}
action={ (resolve, contentComponent) => resolve(true) } />
],
content: <div>{message}</div>
});
}
//------------------------ prompt -------------------------//
class PromptComponent extends PureComponent {
componentDidMount() {
setTimeout(() => {
this.refs.text.focus();
}, 200);
}
getValue() {
return this.refs.text.getValue();
}
render() {
return <div>
<div>{this.props.message}</div>
<div>
<TextField
name="promptValue"
fullWidth={true}
defaultValue={this.props.defaultValue}
ref="text"
/>
</div>
</div>
}
}
export function prompt({title = 'Prompt', message, defaultValue = ''}) {
return showDialog({
title,
actions: [
<FlatButton
label="Cancel"
secondary={true}
action={ (resolve, contentComponent) => resolve(null) } />,
<FlatButton
label="Ok"
primary={true}
action={ (resolve, contentComponent) => resolve(contentComponent.getValue()) } />
],
content: <PromptComponent message={message} defaultValue={defaultValue} />
});
}