forked from superdesk/superdesk-planning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
168 lines (150 loc) · 4.64 KB
/
index.tsx
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
import React from 'react';
import PropTypes from 'prop-types';
import {get} from 'lodash';
import {gettext} from '../../utils';
import {Modal} from '../index';
import {Button} from '../UI';
import './style.scss';
import {KEYCODES} from '../../constants';
export class ModalWithForm extends React.Component {
constructor(props) {
super(props);
this.state = {submitting: false};
this.submit = this.submit.bind(this);
this.onHide = this.onHide.bind(this);
this.dom = {form: null};
this.handleKeydown = this.handleKeydown.bind(this);
}
componentDidMount() {
document.addEventListener('keydown', this.handleKeydown);
}
componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeydown);
}
handleKeydown(event) {
if (event.keyCode === KEYCODES.ESCAPE) {
event.preventDefault();
this.onHide();
}
}
getFormInstance() {
return this.dom.form;
}
submit() {
this.setState({submitting: true});
// Call the submit method of the form Component
this.getFormInstance()
.submit()
.then(
this.props.onHide,
() => {
this.props.enableSaveInModal();
this.setState({submitting: false});
}
);
}
onHide() {
const form = this.getFormInstance();
// If the form Component has an 'onHide' property,
// then call that now
if ('onHide' in form.props) {
form.props.onHide(
this.props.original,
get(this.props, 'modalProps') || {}
);
}
this.props.onHide();
}
render() {
const {
show,
onHide,
large,
fill,
fullscreen,
white,
title,
initialValues,
original,
updates,
enableSaveInModal,
disableSaveInModal,
cancelButtonText,
canSave,
saveButtonText,
form,
modalProps,
resolve,
} = this.props;
const Form = form;
return (
<Modal
show={show}
onHide={onHide}
large={large}
fill={fill}
fullscreen={fullscreen}
white={white}
removeTabIndexAttribute={true}
>
<Modal.Header>
<h3 className="modal__heading">{title}</h3>
<a className="icn-btn" aria-label={gettext('Close')} onClick={this.onHide}>
<i className="icon-close-small" />
</a>
</Modal.Header>
<Modal.Body>
<Form
initialValues={initialValues}
original={original}
updates={updates}
enableSaveInModal={enableSaveInModal}
disableSaveInModal={disableSaveInModal}
submitting={this.state.submitting}
ref={(node) => this.dom.form = node}
modalProps={modalProps}
resolve={resolve}
/>
</Modal.Body>
<Modal.Footer>
<Button
onClick={this.onHide}
disabled={this.state.submitting}
text={cancelButtonText || gettext('Close')}
/>
<Button
color="primary"
onClick={this.submit}
disabled={!canSave || this.state.submitting}
text={saveButtonText || gettext('Save')}
/>
</Modal.Footer>
</Modal>
);
}
}
ModalWithForm.propTypes = {
form: PropTypes.func.isRequired,
initialValues: PropTypes.object,
original: PropTypes.object,
updates: PropTypes.object,
title: PropTypes.string,
show: PropTypes.bool,
enableSaveInModal: PropTypes.func,
disableSaveInModal: PropTypes.func,
onHide: PropTypes.func,
canSave: PropTypes.bool,
cancelButtonText: PropTypes.string,
saveButtonText: PropTypes.string,
large: PropTypes.bool,
fill: PropTypes.bool,
fullscreen: PropTypes.bool,
white: PropTypes.bool,
modalProps: PropTypes.object,
resolve: PropTypes.func,
};
ModalWithForm.defaultProps = {
large: false,
fill: false,
fullscreen: false,
};