-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathReviewCollapsibleChapter.jsx
218 lines (198 loc) · 7.72 KB
/
ReviewCollapsibleChapter.jsx
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import PropTypes from 'prop-types';
import React from 'react';
import Scroll from 'react-scroll';
import _ from 'lodash/fp';
import classNames from 'classnames';
import ProgressButton from '../components/ProgressButton';
import { focusElement } from '../utilities/ui';
import SchemaForm from '../components/SchemaForm';
import {
getArrayFields,
getNonArraySchema,
} from '../helpers';
import ArrayField from './ArrayField';
const Element = Scroll.Element;
const scroller = Scroll.scroller;
/*
* Displays all the pages in a chapter on the review page
*/
export default class ReviewCollapsibleChapter extends React.Component {
constructor() {
super();
this.handleEdit = this.handleEdit.bind(this);
}
componentWillMount() {
this.id = _.uniqueId();
}
onChange(formData, path = null, index = null) {
let newData = formData;
if (path) {
newData = _.set([path, index], formData, this.props.form.data);
}
this.props.setData(newData);
}
focusOnPage(key) {
const pageDiv = document.querySelector(`#${key}`);
focusElement(pageDiv);
}
handleEdit(key, editing, index = null) {
this.props.onEdit(key, editing, index);
this.scrollToPage(`${key}${index === null ? '' : index}`);
this.focusOnPage(`${key}${index === null ? '' : index}`);
}
handleSubmit = (formData, key, path = null, index = null) => {
// This makes sure defaulted data on a page with no changes is saved
// Probably safe to do this for regular pages, too, but it hasn’t been necessary
if (path) {
const newData = _.set([path, index], formData, this.props.form.data);
this.props.setData(newData);
}
this.handleEdit(key, false, index);
}
scrollToPage(key) {
scroller.scrollTo(`${key}ScrollElement`, window.Forms.scroll || {
duration: 500,
delay: 2,
smooth: true,
});
}
render() {
let pageContent = null;
const {
chapterFormConfig,
expandedPages,
form,
formContext,
pageKeys,
showUnviewedPageWarning,
viewedPages
} = this.props;
const ChapterDescription = chapterFormConfig.reviewDescription;
let chapterTitle = chapterFormConfig.title;
if (typeof chapterFormConfig.title === 'function') {
chapterTitle = chapterFormConfig.title(true);
}
if (chapterFormConfig.reviewTitle) {
chapterTitle = chapterFormConfig.reviewTitle;
}
if (this.props.open) {
pageContent = (
<div className="usa-accordion-content schemaform-chapter-accordion-content" aria-hidden="false">
{ChapterDescription &&
<ChapterDescription
viewedPages={viewedPages}
pageKeys={pageKeys}
formData={form.data}/>}
{expandedPages.map(page => {
const pageState = form.pages[page.pageKey];
let pageSchema;
let pageUiSchema;
let pageData;
let arrayFields;
let editing;
let fullPageKey;
if (page.showPagePerItem) {
editing = pageState.editMode[page.index];
pageSchema = pageState.schema.properties[page.arrayPath].items[page.index];
pageUiSchema = pageState.uiSchema[page.arrayPath].items;
pageData = _.get([page.arrayPath, page.index], form.data);
arrayFields = [];
fullPageKey = `${page.pageKey}${page.index}`;
} else {
editing = pageState.editMode;
// TODO: support array fields inside of an array page?
// Our pattern is to separate out array fields (growable tables) from
// the normal page and display them separately. The review version of
// ObjectField will hide them in the main section.
arrayFields = getArrayFields(pageState, page);
// This will be undefined if there are no fields other than an array
// in a page, in which case we won’t render the form, just the array
pageSchema = getNonArraySchema(pageState.schema, pageState.uiSchema);
pageUiSchema = pageState.uiSchema;
pageData = form.data;
fullPageKey = page.pageKey;
}
const classes = classNames('form-review-panel-page', {
'schemaform-review-page-warning': !viewedPages.has(fullPageKey)
});
return (
<div key={`${fullPageKey}`} className={classes}>
<Element name={`${fullPageKey}ScrollElement`}/>
{pageSchema &&
<SchemaForm
name={page.pageKey}
title={page.reviewTitle || page.title}
data={pageData}
schema={pageSchema}
uiSchema={pageUiSchema}
hideHeaderRow={page.hideHeaderRow}
hideTitle={expandedPages.length === 1}
pagePerItemIndex={page.index}
onBlur={this.props.onBlur}
onEdit={() => this.handleEdit(page.pageKey, !editing, page.index)}
onSubmit={({ formData }) => this.handleSubmit(formData, page.pageKey, page.arrayPath, page.index)}
onChange={(formData) => this.onChange(formData, page.arrayPath, page.index)}
uploadFile={this.props.uploadFile}
reviewMode={!editing}
formContext={formContext}
editModeOnReviewPage={page.editModeOnReviewPage}>
{!editing ? <div/> : <ProgressButton
submitButton
buttonText="Update Page"
buttonClass="usa-button-primary"/>}
</SchemaForm>}
{arrayFields.map(arrayField => (
<div key={arrayField.path} className="form-review-array">
<ArrayField
pageKey={page.pageKey}
pageTitle={page.title}
arrayData={_.get(arrayField.path, form.data)}
formData={form.data}
formContext={formContext}
pageConfig={page}
onBlur={this.props.onBlur}
schema={arrayField.schema}
uiSchema={arrayField.uiSchema}
setData={this.props.setData}
path={arrayField.path}/>
</div>
))}
</div>
);
})}
</div>
);
}
const classes = classNames('usa-accordion-bordered', 'form-review-panel', {
'schemaform-review-chapter-warning': showUnviewedPageWarning
});
return (
<div id={`${this.id}-collapsiblePanel`} className={classes}>
<Element name={`chapter${this.props.chapterKey}ScrollElement`}/>
<ul className="usa-unstyled-list">
<li>
<div className="accordion-header clearfix schemaform-chapter-accordion-header">
<button
className="usa-button-unstyled"
aria-expanded={this.props.open ? 'true' : 'false'}
aria-controls={`collapsible-${this.id}`}
onClick={this.props.toggleButtonClicked}>
{chapterTitle}
</button>
{showUnviewedPageWarning && <span className="schemaform-review-chapter-warning-icon"/>}
</div>
<div id={`collapsible-${this.id}`}>
{pageContent}
</div>
</li>
</ul>
</div>
);
}
}
// TODO: refactor to pass form.data instead of the entire form object
ReviewCollapsibleChapter.propTypes = {
chapterFormConfig: PropTypes.object.isRequired,
form: PropTypes.object.isRequired,
onEdit: PropTypes.func.isRequired
};