-
Notifications
You must be signed in to change notification settings - Fork 125
/
ReviewCardField.jsx
374 lines (339 loc) · 11.1 KB
/
ReviewCardField.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import React from 'react';
import PropTypes from 'prop-types';
import * as Sentry from '@sentry/browser';
import {
getDefaultFormState,
getDefaultRegistry,
} from '@department-of-veterans-affairs/react-jsonschema-form/lib/utils';
import recordEvent from 'platform/monitoring/record-event';
import { errorSchemaIsValid } from 'platform/forms-system/src/js/validation';
import set from '../../../platform/utilities/data/set';
import get from '../../../platform/utilities/data/get';
import omit from '../../../platform/utilities/data/omit';
/**
* Displays a review card if the information inside is valid.
*
* For use on a schema of type 'object' or 'array'.
* Intended to wrap objects or arrays to avoid duplicate functionality here.
*
* ui:options available:
* viewComponent - ReactNode that should be shown instead of edit fields
* It's passed the same formData the field is
* startInEdit - Either a function or a value that will be evaluated as truthy or not
* If a function is used, it's passed the formData and expects a boolean return value
* volatileData - If this is truthy, the component pattern changes slightly so only completely new
* data can be entered, but not edited.
* This is useful for bank account information.
* reviewTitle - The title shown on the review card. Defaults to ui:title
* editTitle - The title shown on the edit card. Defaults to ui:title
* itemName - The name of the set of data in the card. This shows up on the "New X" button if
* volatileData is set to true.
*/
export default class ReviewCardField extends React.Component {
static defaultProps = {
uiSchema: {},
errorSchema: {},
idSchema: {},
registry: getDefaultRegistry(),
required: false,
disabled: false,
readonly: false,
};
constructor(props) {
super(props);
// Throw an error if there’s no viewComponent (should be React component)
if (
typeof get('ui:options.viewComponent', this.props.uiSchema) !== 'function'
) {
throw new Error(
`No viewComponent found in uiSchema for ReviewCardField ${
this.props.idSchema.$id
}.`,
);
}
const acceptedTypes = ['object', 'array'];
if (!acceptedTypes.includes(this.props.schema.type)) {
throw new Error(
`Unknown schema type in ReviewCardField. Expected one of [${acceptedTypes.join(
', ',
)}], but got ${this.props.schema.type}.`,
);
}
const invalidInitialData = !errorSchemaIsValid(props.errorSchema);
const startInEditConfigOption = get(
'ui:options.startInEdit',
this.props.uiSchema,
false,
);
// There are times when the data isn't invalid, but we want to start in edit mode anyhow
let shouldStartInEdit = startInEditConfigOption;
if (typeof startInEditConfigOption === 'function') {
shouldStartInEdit = startInEditConfigOption(this.props.formData);
}
const editing = invalidInitialData || shouldStartInEdit;
this.state = {
// Set initial state based on whether all the data is valid
editing,
canCancel: !editing, // If we start in the edit state, we can't cancel
oldData: undefined,
};
}
onPropertyChange(name) {
return value => {
const formData = Object.keys(this.props.formData || {}).length
? this.props.formData
: getDefaultFormState(
this.props.schema,
undefined,
this.props.registry.definitions,
);
this.props.onChange(set(name, value, formData));
};
}
getTitle = () => {
const { uiSchema, formData } = this.props;
return typeof uiSchema['ui:title'] === 'function'
? uiSchema['ui:title'](formData)
: uiSchema['ui:title'];
};
getSubtitle = () => {
const { uiSchema, formData } = this.props;
return typeof uiSchema['ui:subtitle'] === 'function'
? uiSchema['ui:subtitle'](formData)
: uiSchema['ui:subtitle'];
};
getDescription = () => {
const {
uiSchema: { 'ui:description': description },
formData,
} = this.props;
if (!description) {
return null;
}
return typeof description === 'function' ? (
description(formData)
) : (
<p>{description}</p>
);
};
/**
* Much of this is taken from ArrayField & ObjectField.
*
* Renders a SchemaField for each property it wraps.
*/
getEditView = () => {
const {
disabled,
errorSchema,
formData,
idSchema,
onBlur,
onChange,
readonly,
registry,
required,
schema,
} = this.props;
const { SchemaField } = registry.fields;
// We've already used the ui:field and ui:title
const uiSchema = omit(
['ui:field', 'ui:title', 'ui:description'],
this.props.uiSchema,
);
const { volatileData, editTitle } = this.props.uiSchema['ui:options'];
const title = editTitle || this.getTitle();
const subtitle = this.getSubtitle();
return (
<div className="review-card">
<div className="review-card--body va-growable-background">
<h4 className="review-card--title">{title}</h4>
<div className="input-section">
{subtitle && (
<div className="review-card--subtitle">{subtitle}</div>
)}
<SchemaField
name={idSchema.$id}
required={required}
schema={schema}
uiSchema={uiSchema}
errorSchema={errorSchema}
idSchema={idSchema}
formData={formData}
onChange={onChange}
onBlur={onBlur}
registry={registry}
disabled={disabled}
readonly={readonly}
/>
</div>
<button
className="usa-button-primary update-button"
onClick={this.update}
>
{volatileData ? 'Save' : 'Done'}
</button>
{volatileData &&
this.state.canCancel && (
<button
className="usa-button-secondary cancel-button"
onClick={this.cancelUpdate}
>
Cancel
</button>
)}
</div>
</div>
);
};
getReviewView = () => {
if (this.props.formContext.onReviewPage) {
// Check the data type and use the appropriate review field
const dataType = this.props.schema.type;
if (dataType === 'object') {
const { ObjectField } = this.props.registry.fields;
return <ObjectField {...this.props} />;
} else if (dataType === 'array') {
const { ArrayField } = this.props.registry.fields;
return <ArrayField {...this.props} />;
}
// Not having the right type should have been caught in the constructor, but...
Sentry.withScope(scope => {
scope.setExtra('message', `Expected object or array, got ${dataType}`);
Sentry.captureMessage('ReviewCardField-bad-type-on-review');
});
// Fall back to the ViewComponent
}
const {
viewComponent: ViewComponent,
volatileData,
reviewTitle,
itemName,
itemNameAction,
} = this.props.uiSchema['ui:options'];
const title = reviewTitle || this.getTitle();
return (
<div className="review-card">
<div className="review-card--header">
<h4 className="review-card--title">{title}</h4>
{!volatileData && (
<button
className="usa-button-primary edit-button"
onClick={this.startEditing}
aria-label={`Edit ${title}`}
>
Edit
</button>
)}
</div>
<div className="review-card--body">
<ViewComponent formData={this.props.formData} />
</div>
{volatileData && (
<button
className="usa-button-primary edit-button"
onClick={this.startEditing}
aria-label={`${itemNameAction || 'New'} ${itemName || title}`}
>
{itemNameAction || 'New'} {itemName || title}
</button>
)}
</div>
);
};
startEditing = () => {
const newState = { editing: true };
// If the data is volatile, cache the original data before clearing it out so we
// have the option to cancel later
if (this.props.uiSchema['ui:options'].volatileData) {
newState.oldData = this.props.formData;
this.resetFormData();
}
this.setState(newState);
};
cancelUpdate = event => {
// Don't act like the continue button
if (event) {
// Apparently the unit tests don't send this event to the onClick handler
event.preventDefault();
}
this.props.onChange(this.state.oldData);
this.setState({ editing: false });
};
/**
* Resets the form data to either the oldData passed in or the default data
* @param {Any} oldData - Optional. The data to replace the current formData with
*/
resetFormData = oldData => {
const formData =
oldData !== undefined
? oldData
: getDefaultFormState(
this.props.schema,
undefined,
this.props.registry.definitions,
);
this.props.onChange(formData);
};
isRequired = name => {
const { schema } = this.props;
const schemaRequired =
Array.isArray(schema.required) && schema.required.indexOf(name) !== -1;
if (schemaRequired) {
return schemaRequired;
}
return false;
};
update = event => {
// Don't act like the continue button
if (event) {
// Apparently the unit tests don't send this event to the onClick handler
event.preventDefault();
}
if (!errorSchemaIsValid(this.props.errorSchema)) {
// Show validation errors
this.props.formContext.onError();
} else {
this.setState({ editing: false, canCancel: true });
if (this.props.uiSchema.saveClickTrackEvent) {
recordEvent(this.props.uiSchema.saveClickTrackEvent);
}
}
};
render() {
const description = this.getDescription();
const viewOrEditCard = this.state.editing
? this.getEditView()
: this.getReviewView();
return (
<div>
{description}
{viewOrEditCard}
</div>
);
}
}
ReviewCardField.propTypes = {
uiSchema: PropTypes.shape({
'ui:options': PropTypes.shape({
viewComponent: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
.isRequired,
}).isRequired,
'ui:description': PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
'ui:subtitle': PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
saveClickTrackEvent: PropTypes.object,
}).isRequired,
schema: PropTypes.object.isRequired,
errorSchema: PropTypes.object.isRequired,
idSchema: PropTypes.object.isRequired,
registry: PropTypes.shape({
fields: PropTypes.shape({
SchemaField: PropTypes.func.isRequired,
}),
definitions: PropTypes.object.isRequired,
}).isRequired,
formData: PropTypes.object.isRequired,
onBlur: PropTypes.func.isRequired,
formContext: PropTypes.shape({
onError: PropTypes.func.isRequired,
}).isRequired,
};