@rjsf/core 5.11.2 → 5.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.umd.js +3464 -0
- package/dist/index.esm.js +3814 -0
- package/dist/index.esm.js.map +7 -0
- package/dist/index.js +3714 -5
- package/dist/index.js.map +7 -0
- package/{dist/index.d.ts → lib/components/Form.d.ts} +321 -337
- package/lib/components/Form.js +474 -0
- package/lib/components/Form.js.map +1 -0
- package/lib/components/fields/ArrayField.d.ts +179 -0
- package/lib/components/fields/ArrayField.js +568 -0
- package/lib/components/fields/ArrayField.js.map +1 -0
- package/lib/components/fields/BooleanField.d.ts +9 -0
- package/lib/components/fields/BooleanField.js +62 -0
- package/lib/components/fields/BooleanField.js.map +1 -0
- package/lib/components/fields/MultiSchemaField.d.ts +47 -0
- package/lib/components/fields/MultiSchemaField.js +129 -0
- package/lib/components/fields/MultiSchemaField.js.map +1 -0
- package/lib/components/fields/NullField.d.ts +8 -0
- package/lib/components/fields/NullField.js +17 -0
- package/lib/components/fields/NullField.js.map +1 -0
- package/lib/components/fields/NumberField.d.ts +21 -0
- package/lib/components/fields/NumberField.js +70 -0
- package/lib/components/fields/NumberField.js.map +1 -0
- package/lib/components/fields/ObjectField.d.ts +73 -0
- package/lib/components/fields/ObjectField.js +222 -0
- package/lib/components/fields/ObjectField.js.map +1 -0
- package/lib/components/fields/SchemaField.d.ts +10 -0
- package/lib/components/fields/SchemaField.js +172 -0
- package/lib/components/fields/SchemaField.js.map +1 -0
- package/lib/components/fields/StringField.d.ts +8 -0
- package/lib/components/fields/StringField.js +25 -0
- package/lib/components/fields/StringField.js.map +1 -0
- package/lib/components/fields/index.d.ts +3 -0
- package/lib/components/fields/index.js +24 -0
- package/lib/components/fields/index.js.map +1 -0
- package/lib/components/templates/ArrayFieldDescriptionTemplate.d.ts +8 -0
- package/lib/components/templates/ArrayFieldDescriptionTemplate.js +18 -0
- package/lib/components/templates/ArrayFieldDescriptionTemplate.js.map +1 -0
- package/lib/components/templates/ArrayFieldItemTemplate.d.ts +7 -0
- package/lib/components/templates/ArrayFieldItemTemplate.js +20 -0
- package/lib/components/templates/ArrayFieldItemTemplate.js.map +1 -0
- package/lib/components/templates/ArrayFieldTemplate.d.ts +7 -0
- package/lib/components/templates/ArrayFieldTemplate.js +22 -0
- package/lib/components/templates/ArrayFieldTemplate.js.map +1 -0
- package/lib/components/templates/ArrayFieldTitleTemplate.d.ts +8 -0
- package/lib/components/templates/ArrayFieldTitleTemplate.js +18 -0
- package/lib/components/templates/ArrayFieldTitleTemplate.js.map +1 -0
- package/lib/components/templates/BaseInputTemplate.d.ts +9 -0
- package/lib/components/templates/BaseInputTemplate.js +39 -0
- package/lib/components/templates/BaseInputTemplate.js.map +1 -0
- package/lib/components/templates/ButtonTemplates/AddButton.d.ts +5 -0
- package/lib/components/templates/ButtonTemplates/AddButton.js +10 -0
- package/lib/components/templates/ButtonTemplates/AddButton.js.map +1 -0
- package/lib/components/templates/ButtonTemplates/IconButton.d.ts +7 -0
- package/lib/components/templates/ButtonTemplates/IconButton.js +24 -0
- package/lib/components/templates/ButtonTemplates/IconButton.js.map +1 -0
- package/lib/components/templates/ButtonTemplates/SubmitButton.d.ts +5 -0
- package/lib/components/templates/ButtonTemplates/SubmitButton.js +12 -0
- package/lib/components/templates/ButtonTemplates/SubmitButton.js.map +1 -0
- package/lib/components/templates/ButtonTemplates/index.d.ts +3 -0
- package/lib/components/templates/ButtonTemplates/index.js +15 -0
- package/lib/components/templates/ButtonTemplates/index.js.map +1 -0
- package/lib/components/templates/DescriptionField.d.ts +7 -0
- package/lib/components/templates/DescriptionField.js +18 -0
- package/lib/components/templates/DescriptionField.js.map +1 -0
- package/lib/components/templates/ErrorList.d.ts +7 -0
- package/lib/components/templates/ErrorList.js +13 -0
- package/lib/components/templates/ErrorList.js.map +1 -0
- package/lib/components/templates/FieldErrorTemplate.d.ts +7 -0
- package/lib/components/templates/FieldErrorTemplate.js +19 -0
- package/lib/components/templates/FieldErrorTemplate.js.map +1 -0
- package/lib/components/templates/FieldHelpTemplate.d.ts +7 -0
- package/lib/components/templates/FieldHelpTemplate.js +18 -0
- package/lib/components/templates/FieldHelpTemplate.js.map +1 -0
- package/lib/components/templates/FieldTemplate/FieldTemplate.d.ts +8 -0
- package/lib/components/templates/FieldTemplate/FieldTemplate.js +18 -0
- package/lib/components/templates/FieldTemplate/FieldTemplate.js.map +1 -0
- package/lib/components/templates/FieldTemplate/Label.d.ts +14 -0
- package/lib/components/templates/FieldTemplate/Label.js +14 -0
- package/lib/components/templates/FieldTemplate/Label.js.map +1 -0
- package/lib/components/templates/FieldTemplate/index.d.ts +2 -0
- package/lib/components/templates/FieldTemplate/index.js +3 -0
- package/lib/components/templates/FieldTemplate/index.js.map +1 -0
- package/lib/components/templates/ObjectFieldTemplate.d.ts +9 -0
- package/lib/components/templates/ObjectFieldTemplate.js +18 -0
- package/lib/components/templates/ObjectFieldTemplate.js.map +1 -0
- package/lib/components/templates/TitleField.d.ts +7 -0
- package/lib/components/templates/TitleField.js +11 -0
- package/lib/components/templates/TitleField.js.map +1 -0
- package/lib/components/templates/UnsupportedField.d.ts +9 -0
- package/lib/components/templates/UnsupportedField.js +28 -0
- package/lib/components/templates/UnsupportedField.js.map +1 -0
- package/lib/components/templates/WrapIfAdditionalTemplate.d.ts +8 -0
- package/lib/components/templates/WrapIfAdditionalTemplate.js +21 -0
- package/lib/components/templates/WrapIfAdditionalTemplate.js.map +1 -0
- package/lib/components/templates/index.d.ts +3 -0
- package/lib/components/templates/index.js +36 -0
- package/lib/components/templates/index.js.map +1 -0
- package/lib/components/widgets/AltDateTimeWidget.d.ts +9 -0
- package/lib/components/widgets/AltDateTimeWidget.js +14 -0
- package/lib/components/widgets/AltDateTimeWidget.js.map +1 -0
- package/lib/components/widgets/AltDateWidget.d.ts +7 -0
- package/lib/components/widgets/AltDateWidget.js +77 -0
- package/lib/components/widgets/AltDateWidget.js.map +1 -0
- package/lib/components/widgets/CheckboxWidget.d.ts +9 -0
- package/lib/components/widgets/CheckboxWidget.js +23 -0
- package/lib/components/widgets/CheckboxWidget.js.map +1 -0
- package/lib/components/widgets/CheckboxesWidget.d.ts +9 -0
- package/lib/components/widgets/CheckboxesWidget.js +31 -0
- package/lib/components/widgets/CheckboxesWidget.js.map +1 -0
- package/lib/components/widgets/ColorWidget.d.ts +8 -0
- package/lib/components/widgets/ColorWidget.js +13 -0
- package/lib/components/widgets/ColorWidget.js.map +1 -0
- package/lib/components/widgets/DateTimeWidget.d.ts +8 -0
- package/lib/components/widgets/DateTimeWidget.js +13 -0
- package/lib/components/widgets/DateTimeWidget.js.map +1 -0
- package/lib/components/widgets/DateWidget.d.ts +8 -0
- package/lib/components/widgets/DateWidget.js +15 -0
- package/lib/components/widgets/DateWidget.js.map +1 -0
- package/lib/components/widgets/EmailWidget.d.ts +7 -0
- package/lib/components/widgets/EmailWidget.js +12 -0
- package/lib/components/widgets/EmailWidget.js.map +1 -0
- package/lib/components/widgets/FileWidget.d.ts +8 -0
- package/lib/components/widgets/FileWidget.js +105 -0
- package/lib/components/widgets/FileWidget.js.map +1 -0
- package/lib/components/widgets/HiddenWidget.d.ts +9 -0
- package/lib/components/widgets/HiddenWidget.js +11 -0
- package/lib/components/widgets/HiddenWidget.js.map +1 -0
- package/lib/components/widgets/PasswordWidget.d.ts +7 -0
- package/lib/components/widgets/PasswordWidget.js +12 -0
- package/lib/components/widgets/PasswordWidget.js.map +1 -0
- package/lib/components/widgets/RadioWidget.d.ts +9 -0
- package/lib/components/widgets/RadioWidget.js +24 -0
- package/lib/components/widgets/RadioWidget.js.map +1 -0
- package/lib/components/widgets/RangeWidget.d.ts +8 -0
- package/lib/components/widgets/RangeWidget.js +11 -0
- package/lib/components/widgets/RangeWidget.js.map +1 -0
- package/lib/components/widgets/SelectWidget.d.ts +9 -0
- package/lib/components/widgets/SelectWidget.js +41 -0
- package/lib/components/widgets/SelectWidget.js.map +1 -0
- package/lib/components/widgets/TextWidget.d.ts +7 -0
- package/lib/components/widgets/TextWidget.js +12 -0
- package/lib/components/widgets/TextWidget.js.map +1 -0
- package/lib/components/widgets/TextareaWidget.d.ts +14 -0
- package/lib/components/widgets/TextareaWidget.js +19 -0
- package/lib/components/widgets/TextareaWidget.js.map +1 -0
- package/lib/components/widgets/TimeWidget.d.ts +8 -0
- package/lib/components/widgets/TimeWidget.js +15 -0
- package/lib/components/widgets/TimeWidget.js.map +1 -0
- package/lib/components/widgets/URLWidget.d.ts +7 -0
- package/lib/components/widgets/URLWidget.js +12 -0
- package/lib/components/widgets/URLWidget.js.map +1 -0
- package/lib/components/widgets/UpDownWidget.d.ts +7 -0
- package/lib/components/widgets/UpDownWidget.js +12 -0
- package/lib/components/widgets/UpDownWidget.js.map +1 -0
- package/lib/components/widgets/index.d.ts +3 -0
- package/lib/components/widgets/index.js +44 -0
- package/lib/components/widgets/index.js.map +1 -0
- package/lib/getDefaultRegistry.d.ts +6 -0
- package/lib/getDefaultRegistry.js +19 -0
- package/lib/getDefaultRegistry.js.map +1 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +6 -0
- package/lib/index.js.map +1 -0
- package/lib/withTheme.d.ts +9 -0
- package/lib/withTheme.js +16 -0
- package/lib/withTheme.js.map +1 -0
- package/package.json +23 -16
- package/src/components/Form.tsx +853 -0
- package/src/components/fields/ArrayField.tsx +875 -0
- package/src/components/fields/BooleanField.tsx +114 -0
- package/src/components/fields/MultiSchemaField.tsx +221 -0
- package/src/components/fields/NullField.tsx +22 -0
- package/src/components/fields/NumberField.tsx +86 -0
- package/src/components/fields/ObjectField.tsx +331 -0
- package/src/components/fields/SchemaField.tsx +360 -0
- package/src/components/fields/StringField.tsx +71 -0
- package/src/components/fields/index.ts +31 -0
- package/src/components/templates/ArrayFieldDescriptionTemplate.tsx +41 -0
- package/src/components/templates/ArrayFieldItemTemplate.tsx +90 -0
- package/src/components/templates/ArrayFieldTemplate.tsx +88 -0
- package/src/components/templates/ArrayFieldTitleTemplate.tsx +43 -0
- package/src/components/templates/BaseInputTemplate.tsx +102 -0
- package/src/components/templates/ButtonTemplates/AddButton.tsx +29 -0
- package/src/components/templates/ButtonTemplates/IconButton.tsx +77 -0
- package/src/components/templates/ButtonTemplates/SubmitButton.tsx +21 -0
- package/src/components/templates/ButtonTemplates/index.ts +22 -0
- package/src/components/templates/DescriptionField.tsx +29 -0
- package/src/components/templates/ErrorList.tsx +35 -0
- package/src/components/templates/FieldErrorTemplate.tsx +33 -0
- package/src/components/templates/FieldHelpTemplate.tsx +29 -0
- package/src/components/templates/FieldTemplate/FieldTemplate.tsx +41 -0
- package/src/components/templates/FieldTemplate/Label.tsx +27 -0
- package/src/components/templates/FieldTemplate/index.ts +3 -0
- package/src/components/templates/ObjectFieldTemplate.tsx +83 -0
- package/src/components/templates/TitleField.tsx +19 -0
- package/src/components/templates/UnsupportedField.tsx +37 -0
- package/src/components/templates/WrapIfAdditionalTemplate.tsx +80 -0
- package/src/components/templates/index.ts +43 -0
- package/src/components/widgets/AltDateTimeWidget.tsx +16 -0
- package/src/components/widgets/AltDateWidget.tsx +198 -0
- package/src/components/widgets/CheckboxWidget.tsx +92 -0
- package/src/components/widgets/CheckboxesWidget.tsx +92 -0
- package/src/components/widgets/ColorWidget.tsx +14 -0
- package/src/components/widgets/DateTimeWidget.tsx +31 -0
- package/src/components/widgets/DateWidget.tsx +17 -0
- package/src/components/widgets/EmailWidget.tsx +13 -0
- package/src/components/widgets/FileWidget.tsx +178 -0
- package/src/components/widgets/HiddenWidget.tsx +15 -0
- package/src/components/widgets/PasswordWidget.tsx +15 -0
- package/src/components/widgets/RadioWidget.tsx +88 -0
- package/src/components/widgets/RangeWidget.tsx +23 -0
- package/src/components/widgets/SelectWidget.tsx +100 -0
- package/src/components/widgets/TextWidget.tsx +13 -0
- package/src/components/widgets/TextareaWidget.tsx +61 -0
- package/src/components/widgets/TimeWidget.tsx +17 -0
- package/src/components/widgets/URLWidget.tsx +13 -0
- package/src/components/widgets/UpDownWidget.tsx +13 -0
- package/src/components/widgets/index.ts +51 -0
- package/src/getDefaultRegistry.ts +24 -0
- package/src/index.ts +8 -0
- package/src/withTheme.tsx +42 -0
- package/dist/core.cjs.development.js +0 -4403
- package/dist/core.cjs.development.js.map +0 -1
- package/dist/core.cjs.production.min.js +0 -2
- package/dist/core.cjs.production.min.js.map +0 -1
- package/dist/core.esm.js +0 -4383
- package/dist/core.esm.js.map +0 -1
- package/dist/core.umd.development.js +0 -4393
- package/dist/core.umd.development.js.map +0 -1
- package/dist/core.umd.production.min.js +0 -2
- package/dist/core.umd.production.min.js.map +0 -1
|
@@ -0,0 +1,875 @@
|
|
|
1
|
+
import { Component, MouseEvent } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
getTemplate,
|
|
4
|
+
getWidget,
|
|
5
|
+
getUiOptions,
|
|
6
|
+
isFixedItems,
|
|
7
|
+
allowAdditionalItems,
|
|
8
|
+
isCustomWidget,
|
|
9
|
+
optionsList,
|
|
10
|
+
ArrayFieldTemplateProps,
|
|
11
|
+
ErrorSchema,
|
|
12
|
+
FieldProps,
|
|
13
|
+
FormContextType,
|
|
14
|
+
IdSchema,
|
|
15
|
+
RJSFSchema,
|
|
16
|
+
StrictRJSFSchema,
|
|
17
|
+
TranslatableString,
|
|
18
|
+
UiSchema,
|
|
19
|
+
ITEMS_KEY,
|
|
20
|
+
} from '@rjsf/utils';
|
|
21
|
+
import cloneDeep from 'lodash/cloneDeep';
|
|
22
|
+
import get from 'lodash/get';
|
|
23
|
+
import isObject from 'lodash/isObject';
|
|
24
|
+
import set from 'lodash/set';
|
|
25
|
+
import { nanoid } from 'nanoid';
|
|
26
|
+
|
|
27
|
+
/** Type used to represent the keyed form data used in the state */
|
|
28
|
+
type KeyedFormDataType<T> = { key: string; item: T };
|
|
29
|
+
|
|
30
|
+
/** Type used for the state of the `ArrayField` component */
|
|
31
|
+
type ArrayFieldState<T> = {
|
|
32
|
+
/** The keyed form data elements */
|
|
33
|
+
keyedFormData: KeyedFormDataType<T>[];
|
|
34
|
+
/** Flag indicating whether any of the keyed form data has been updated */
|
|
35
|
+
updatedKeyedFormData: boolean;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Used to generate a unique ID for an element in a row */
|
|
39
|
+
function generateRowId() {
|
|
40
|
+
return nanoid();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Converts the `formData` into `KeyedFormDataType` data, using the `generateRowId()` function to create the key
|
|
44
|
+
*
|
|
45
|
+
* @param formData - The data for the form
|
|
46
|
+
* @returns - The `formData` converted into a `KeyedFormDataType` element
|
|
47
|
+
*/
|
|
48
|
+
function generateKeyedFormData<T>(formData: T[]): KeyedFormDataType<T>[] {
|
|
49
|
+
return !Array.isArray(formData)
|
|
50
|
+
? []
|
|
51
|
+
: formData.map((item) => {
|
|
52
|
+
return {
|
|
53
|
+
key: generateRowId(),
|
|
54
|
+
item,
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Converts `KeyedFormDataType` data into the inner `formData`
|
|
60
|
+
*
|
|
61
|
+
* @param keyedFormData - The `KeyedFormDataType` to be converted
|
|
62
|
+
* @returns - The inner `formData` item(s) in the `keyedFormData`
|
|
63
|
+
*/
|
|
64
|
+
function keyedToPlainFormData<T>(keyedFormData: KeyedFormDataType<T> | KeyedFormDataType<T>[]): T[] {
|
|
65
|
+
if (Array.isArray(keyedFormData)) {
|
|
66
|
+
return keyedFormData.map((keyedItem) => keyedItem.item);
|
|
67
|
+
}
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The `ArrayField` component is used to render a field in the schema that is of type `array`. It supports both normal
|
|
72
|
+
* and fixed array, allowing user to add and remove elements from the array data.
|
|
73
|
+
*/
|
|
74
|
+
class ArrayField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> extends Component<
|
|
75
|
+
FieldProps<T[], S, F>,
|
|
76
|
+
ArrayFieldState<T>
|
|
77
|
+
> {
|
|
78
|
+
/** Constructs an `ArrayField` from the `props`, generating the initial keyed data from the `formData`
|
|
79
|
+
*
|
|
80
|
+
* @param props - The `FieldProps` for this template
|
|
81
|
+
*/
|
|
82
|
+
constructor(props: FieldProps<T[], S, F>) {
|
|
83
|
+
super(props);
|
|
84
|
+
const { formData = [] } = props;
|
|
85
|
+
const keyedFormData = generateKeyedFormData<T>(formData);
|
|
86
|
+
this.state = {
|
|
87
|
+
keyedFormData,
|
|
88
|
+
updatedKeyedFormData: false,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** React lifecycle method that is called when the props are about to change allowing the state to be updated. It
|
|
93
|
+
* regenerates the keyed form data and returns it
|
|
94
|
+
*
|
|
95
|
+
* @param nextProps - The next set of props data
|
|
96
|
+
* @param prevState - The previous set of state data
|
|
97
|
+
*/
|
|
98
|
+
static getDerivedStateFromProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
99
|
+
nextProps: Readonly<FieldProps<T[], S, F>>,
|
|
100
|
+
prevState: Readonly<ArrayFieldState<T>>
|
|
101
|
+
) {
|
|
102
|
+
// Don't call getDerivedStateFromProps if keyed formdata was just updated.
|
|
103
|
+
if (prevState.updatedKeyedFormData) {
|
|
104
|
+
return {
|
|
105
|
+
updatedKeyedFormData: false,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const nextFormData = Array.isArray(nextProps.formData) ? nextProps.formData : [];
|
|
109
|
+
const previousKeyedFormData = prevState.keyedFormData || [];
|
|
110
|
+
const newKeyedFormData =
|
|
111
|
+
nextFormData.length === previousKeyedFormData.length
|
|
112
|
+
? previousKeyedFormData.map((previousKeyedFormDatum, index) => {
|
|
113
|
+
return {
|
|
114
|
+
key: previousKeyedFormDatum.key,
|
|
115
|
+
item: nextFormData[index],
|
|
116
|
+
};
|
|
117
|
+
})
|
|
118
|
+
: generateKeyedFormData<T>(nextFormData);
|
|
119
|
+
return {
|
|
120
|
+
keyedFormData: newKeyedFormData,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Returns the appropriate title for an item by getting first the title from the schema.items, then falling back to
|
|
125
|
+
* the description from the schema.items, and finally the string "Item"
|
|
126
|
+
*/
|
|
127
|
+
get itemTitle() {
|
|
128
|
+
const { schema, registry } = this.props;
|
|
129
|
+
const { translateString } = registry;
|
|
130
|
+
return get(
|
|
131
|
+
schema,
|
|
132
|
+
[ITEMS_KEY, 'title'],
|
|
133
|
+
get(schema, [ITEMS_KEY, 'description'], translateString(TranslatableString.ArrayItemTitle))
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Determines whether the item described in the schema is always required, which is determined by whether any item
|
|
138
|
+
* may be null.
|
|
139
|
+
*
|
|
140
|
+
* @param itemSchema - The schema for the item
|
|
141
|
+
* @return - True if the item schema type does not contain the "null" type
|
|
142
|
+
*/
|
|
143
|
+
isItemRequired(itemSchema: S) {
|
|
144
|
+
if (Array.isArray(itemSchema.type)) {
|
|
145
|
+
// While we don't yet support composite/nullable jsonschema types, it's
|
|
146
|
+
// future-proof to check for requirement against these.
|
|
147
|
+
return !itemSchema.type.includes('null');
|
|
148
|
+
}
|
|
149
|
+
// All non-null array item types are inherently required by design
|
|
150
|
+
return itemSchema.type !== 'null';
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Determines whether more items can be added to the array. If the uiSchema indicates the array doesn't allow adding
|
|
154
|
+
* then false is returned. Otherwise, if the schema indicates that there are a maximum number of items and the
|
|
155
|
+
* `formData` matches that value, then false is returned, otherwise true is returned.
|
|
156
|
+
*
|
|
157
|
+
* @param formItems - The list of items in the form
|
|
158
|
+
* @returns - True if the item is addable otherwise false
|
|
159
|
+
*/
|
|
160
|
+
canAddItem(formItems: any[]) {
|
|
161
|
+
const { schema, uiSchema, registry } = this.props;
|
|
162
|
+
let { addable } = getUiOptions<T[], S, F>(uiSchema, registry.globalUiOptions);
|
|
163
|
+
if (addable !== false) {
|
|
164
|
+
// if ui:options.addable was not explicitly set to false, we can add
|
|
165
|
+
// another item if we have not exceeded maxItems yet
|
|
166
|
+
if (schema.maxItems !== undefined) {
|
|
167
|
+
addable = formItems.length < schema.maxItems;
|
|
168
|
+
} else {
|
|
169
|
+
addable = true;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return addable;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Returns the default form information for an item based on the schema for that item. Deals with the possibility
|
|
176
|
+
* that the schema is fixed and allows additional items.
|
|
177
|
+
*/
|
|
178
|
+
_getNewFormDataRow = (): T => {
|
|
179
|
+
const { schema, registry } = this.props;
|
|
180
|
+
const { schemaUtils } = registry;
|
|
181
|
+
let itemSchema = schema.items as S;
|
|
182
|
+
if (isFixedItems(schema) && allowAdditionalItems(schema)) {
|
|
183
|
+
itemSchema = schema.additionalItems as S;
|
|
184
|
+
}
|
|
185
|
+
// Cast this as a T to work around schema utils being for T[] caused by the FieldProps<T[], S, F> call on the class
|
|
186
|
+
return schemaUtils.getDefaultFormState(itemSchema) as unknown as T;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/** Callback handler for when the user clicks on the add or add at index buttons. Creates a new row of keyed form data
|
|
190
|
+
* either at the end of the list (when index is not specified) or inserted at the `index` when it is, adding it into
|
|
191
|
+
* the state, and then returning `onChange()` with the plain form data converted from the keyed data
|
|
192
|
+
*
|
|
193
|
+
* @param event - The event for the click
|
|
194
|
+
* @param [index] - The optional index at which to add the new data
|
|
195
|
+
*/
|
|
196
|
+
_handleAddClick(event: MouseEvent, index?: number) {
|
|
197
|
+
if (event) {
|
|
198
|
+
event.preventDefault();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const { onChange } = this.props;
|
|
202
|
+
const { keyedFormData } = this.state;
|
|
203
|
+
const newKeyedFormDataRow: KeyedFormDataType<T> = {
|
|
204
|
+
key: generateRowId(),
|
|
205
|
+
item: this._getNewFormDataRow(),
|
|
206
|
+
};
|
|
207
|
+
const newKeyedFormData = [...keyedFormData];
|
|
208
|
+
if (index !== undefined) {
|
|
209
|
+
newKeyedFormData.splice(index, 0, newKeyedFormDataRow);
|
|
210
|
+
} else {
|
|
211
|
+
newKeyedFormData.push(newKeyedFormDataRow);
|
|
212
|
+
}
|
|
213
|
+
this.setState(
|
|
214
|
+
{
|
|
215
|
+
keyedFormData: newKeyedFormData,
|
|
216
|
+
updatedKeyedFormData: true,
|
|
217
|
+
},
|
|
218
|
+
() => onChange(keyedToPlainFormData(newKeyedFormData))
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Callback handler for when the user clicks on the add button. Creates a new row of keyed form data at the end of
|
|
223
|
+
* the list, adding it into the state, and then returning `onChange()` with the plain form data converted from the
|
|
224
|
+
* keyed data
|
|
225
|
+
*
|
|
226
|
+
* @param event - The event for the click
|
|
227
|
+
*/
|
|
228
|
+
onAddClick = (event: MouseEvent) => {
|
|
229
|
+
this._handleAddClick(event);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/** Callback handler for when the user clicks on the add button on an existing array element. Creates a new row of
|
|
233
|
+
* keyed form data inserted at the `index`, adding it into the state, and then returning `onChange()` with the plain
|
|
234
|
+
* form data converted from the keyed data
|
|
235
|
+
*
|
|
236
|
+
* @param index - The index at which the add button is clicked
|
|
237
|
+
*/
|
|
238
|
+
onAddIndexClick = (index: number) => {
|
|
239
|
+
return (event: MouseEvent) => {
|
|
240
|
+
this._handleAddClick(event, index);
|
|
241
|
+
};
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
/** Callback handler for when the user clicks on the copy button on an existing array element. Clones the row of
|
|
245
|
+
* keyed form data at the `index` into the next position in the state, and then returning `onChange()` with the plain
|
|
246
|
+
* form data converted from the keyed data
|
|
247
|
+
*
|
|
248
|
+
* @param index - The index at which the copy button is clicked
|
|
249
|
+
*/
|
|
250
|
+
onCopyIndexClick = (index: number) => {
|
|
251
|
+
return (event: MouseEvent) => {
|
|
252
|
+
if (event) {
|
|
253
|
+
event.preventDefault();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const { onChange } = this.props;
|
|
257
|
+
const { keyedFormData } = this.state;
|
|
258
|
+
const newKeyedFormDataRow: KeyedFormDataType<T> = {
|
|
259
|
+
key: generateRowId(),
|
|
260
|
+
item: cloneDeep(keyedFormData[index].item),
|
|
261
|
+
};
|
|
262
|
+
const newKeyedFormData = [...keyedFormData];
|
|
263
|
+
if (index !== undefined) {
|
|
264
|
+
newKeyedFormData.splice(index + 1, 0, newKeyedFormDataRow);
|
|
265
|
+
} else {
|
|
266
|
+
newKeyedFormData.push(newKeyedFormDataRow);
|
|
267
|
+
}
|
|
268
|
+
this.setState(
|
|
269
|
+
{
|
|
270
|
+
keyedFormData: newKeyedFormData,
|
|
271
|
+
updatedKeyedFormData: true,
|
|
272
|
+
},
|
|
273
|
+
() => onChange(keyedToPlainFormData(newKeyedFormData))
|
|
274
|
+
);
|
|
275
|
+
};
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
/** Callback handler for when the user clicks on the remove button on an existing array element. Removes the row of
|
|
279
|
+
* keyed form data at the `index` in the state, and then returning `onChange()` with the plain form data converted
|
|
280
|
+
* from the keyed data
|
|
281
|
+
*
|
|
282
|
+
* @param index - The index at which the remove button is clicked
|
|
283
|
+
*/
|
|
284
|
+
onDropIndexClick = (index: number) => {
|
|
285
|
+
return (event: MouseEvent) => {
|
|
286
|
+
if (event) {
|
|
287
|
+
event.preventDefault();
|
|
288
|
+
}
|
|
289
|
+
const { onChange, errorSchema } = this.props;
|
|
290
|
+
const { keyedFormData } = this.state;
|
|
291
|
+
// refs #195: revalidate to ensure properly reindexing errors
|
|
292
|
+
let newErrorSchema: ErrorSchema<T>;
|
|
293
|
+
if (errorSchema) {
|
|
294
|
+
newErrorSchema = {};
|
|
295
|
+
for (const idx in errorSchema) {
|
|
296
|
+
const i = parseInt(idx);
|
|
297
|
+
if (i < index) {
|
|
298
|
+
set(newErrorSchema, [i], errorSchema[idx]);
|
|
299
|
+
} else if (i > index) {
|
|
300
|
+
set(newErrorSchema, [i - 1], errorSchema[idx]);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const newKeyedFormData = keyedFormData.filter((_, i) => i !== index);
|
|
305
|
+
this.setState(
|
|
306
|
+
{
|
|
307
|
+
keyedFormData: newKeyedFormData,
|
|
308
|
+
updatedKeyedFormData: true,
|
|
309
|
+
},
|
|
310
|
+
() => onChange(keyedToPlainFormData(newKeyedFormData), newErrorSchema as ErrorSchema<T[]>)
|
|
311
|
+
);
|
|
312
|
+
};
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
/** Callback handler for when the user clicks on one of the move item buttons on an existing array element. Moves the
|
|
316
|
+
* row of keyed form data at the `index` to the `newIndex` in the state, and then returning `onChange()` with the
|
|
317
|
+
* plain form data converted from the keyed data
|
|
318
|
+
*
|
|
319
|
+
* @param index - The index of the item to move
|
|
320
|
+
* @param newIndex - The index to where the item is to be moved
|
|
321
|
+
*/
|
|
322
|
+
onReorderClick = (index: number, newIndex: number) => {
|
|
323
|
+
return (event: MouseEvent<HTMLButtonElement>) => {
|
|
324
|
+
if (event) {
|
|
325
|
+
event.preventDefault();
|
|
326
|
+
event.currentTarget.blur();
|
|
327
|
+
}
|
|
328
|
+
const { onChange, errorSchema } = this.props;
|
|
329
|
+
let newErrorSchema: ErrorSchema<T>;
|
|
330
|
+
if (errorSchema) {
|
|
331
|
+
newErrorSchema = {};
|
|
332
|
+
for (const idx in errorSchema) {
|
|
333
|
+
const i = parseInt(idx);
|
|
334
|
+
if (i == index) {
|
|
335
|
+
set(newErrorSchema, [newIndex], errorSchema[index]);
|
|
336
|
+
} else if (i == newIndex) {
|
|
337
|
+
set(newErrorSchema, [index], errorSchema[newIndex]);
|
|
338
|
+
} else {
|
|
339
|
+
set(newErrorSchema, [idx], errorSchema[i]);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const { keyedFormData } = this.state;
|
|
345
|
+
function reOrderArray() {
|
|
346
|
+
// Copy item
|
|
347
|
+
const _newKeyedFormData = keyedFormData.slice();
|
|
348
|
+
|
|
349
|
+
// Moves item from index to newIndex
|
|
350
|
+
_newKeyedFormData.splice(index, 1);
|
|
351
|
+
_newKeyedFormData.splice(newIndex, 0, keyedFormData[index]);
|
|
352
|
+
|
|
353
|
+
return _newKeyedFormData;
|
|
354
|
+
}
|
|
355
|
+
const newKeyedFormData = reOrderArray();
|
|
356
|
+
this.setState(
|
|
357
|
+
{
|
|
358
|
+
keyedFormData: newKeyedFormData,
|
|
359
|
+
},
|
|
360
|
+
() => onChange(keyedToPlainFormData(newKeyedFormData), newErrorSchema as ErrorSchema<T[]>)
|
|
361
|
+
);
|
|
362
|
+
};
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
/** Callback handler used to deal with changing the value of the data in the array at the `index`. Calls the
|
|
366
|
+
* `onChange` callback with the updated form data
|
|
367
|
+
*
|
|
368
|
+
* @param index - The index of the item being changed
|
|
369
|
+
*/
|
|
370
|
+
onChangeForIndex = (index: number) => {
|
|
371
|
+
return (value: any, newErrorSchema?: ErrorSchema<T>, id?: string) => {
|
|
372
|
+
const { formData, onChange, errorSchema } = this.props;
|
|
373
|
+
const arrayData = Array.isArray(formData) ? formData : [];
|
|
374
|
+
const newFormData = arrayData.map((item: T, i: number) => {
|
|
375
|
+
// We need to treat undefined items as nulls to have validation.
|
|
376
|
+
// See https://github.com/tdegrunt/jsonschema/issues/206
|
|
377
|
+
const jsonValue = typeof value === 'undefined' ? null : value;
|
|
378
|
+
return index === i ? jsonValue : item;
|
|
379
|
+
});
|
|
380
|
+
onChange(
|
|
381
|
+
newFormData,
|
|
382
|
+
errorSchema &&
|
|
383
|
+
errorSchema && {
|
|
384
|
+
...errorSchema,
|
|
385
|
+
[index]: newErrorSchema,
|
|
386
|
+
},
|
|
387
|
+
id
|
|
388
|
+
);
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
/** Callback handler used to change the value for a checkbox */
|
|
393
|
+
onSelectChange = (value: any) => {
|
|
394
|
+
const { onChange, idSchema } = this.props;
|
|
395
|
+
onChange(value, undefined, idSchema && idSchema.$id);
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
/** Renders the `ArrayField` depending on the specific needs of the schema and uischema elements
|
|
399
|
+
*/
|
|
400
|
+
render() {
|
|
401
|
+
const { schema, uiSchema, idSchema, registry } = this.props;
|
|
402
|
+
const { schemaUtils, translateString } = registry;
|
|
403
|
+
if (!(ITEMS_KEY in schema)) {
|
|
404
|
+
const uiOptions = getUiOptions<T[], S, F>(uiSchema);
|
|
405
|
+
const UnsupportedFieldTemplate = getTemplate<'UnsupportedFieldTemplate', T[], S, F>(
|
|
406
|
+
'UnsupportedFieldTemplate',
|
|
407
|
+
registry,
|
|
408
|
+
uiOptions
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
return (
|
|
412
|
+
<UnsupportedFieldTemplate
|
|
413
|
+
schema={schema}
|
|
414
|
+
idSchema={idSchema}
|
|
415
|
+
reason={translateString(TranslatableString.MissingItems)}
|
|
416
|
+
registry={registry}
|
|
417
|
+
/>
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
if (schemaUtils.isMultiSelect(schema)) {
|
|
421
|
+
// If array has enum or uniqueItems set to true, call renderMultiSelect() to render the default multiselect widget or a custom widget, if specified.
|
|
422
|
+
return this.renderMultiSelect();
|
|
423
|
+
}
|
|
424
|
+
if (isCustomWidget<T[], S, F>(uiSchema)) {
|
|
425
|
+
return this.renderCustomWidget();
|
|
426
|
+
}
|
|
427
|
+
if (isFixedItems(schema)) {
|
|
428
|
+
return this.renderFixedArray();
|
|
429
|
+
}
|
|
430
|
+
if (schemaUtils.isFilesArray(schema, uiSchema)) {
|
|
431
|
+
return this.renderFiles();
|
|
432
|
+
}
|
|
433
|
+
return this.renderNormalArray();
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** Renders a normal array without any limitations of length
|
|
437
|
+
*/
|
|
438
|
+
renderNormalArray() {
|
|
439
|
+
const {
|
|
440
|
+
schema,
|
|
441
|
+
uiSchema = {},
|
|
442
|
+
errorSchema,
|
|
443
|
+
idSchema,
|
|
444
|
+
name,
|
|
445
|
+
disabled = false,
|
|
446
|
+
readonly = false,
|
|
447
|
+
autofocus = false,
|
|
448
|
+
required = false,
|
|
449
|
+
registry,
|
|
450
|
+
onBlur,
|
|
451
|
+
onFocus,
|
|
452
|
+
idPrefix,
|
|
453
|
+
idSeparator = '_',
|
|
454
|
+
rawErrors,
|
|
455
|
+
} = this.props;
|
|
456
|
+
const { keyedFormData } = this.state;
|
|
457
|
+
const title = schema.title === undefined ? name : schema.title;
|
|
458
|
+
const { schemaUtils, formContext } = registry;
|
|
459
|
+
const uiOptions = getUiOptions<T[], S, F>(uiSchema);
|
|
460
|
+
const _schemaItems: S = isObject(schema.items) ? (schema.items as S) : ({} as S);
|
|
461
|
+
const itemsSchema: S = schemaUtils.retrieveSchema(_schemaItems);
|
|
462
|
+
const formData = keyedToPlainFormData(this.state.keyedFormData);
|
|
463
|
+
const canAdd = this.canAddItem(formData);
|
|
464
|
+
const arrayProps: ArrayFieldTemplateProps<T[], S, F> = {
|
|
465
|
+
canAdd,
|
|
466
|
+
items: keyedFormData.map((keyedItem, index) => {
|
|
467
|
+
const { key, item } = keyedItem;
|
|
468
|
+
// While we are actually dealing with a single item of type T, the types require a T[], so cast
|
|
469
|
+
const itemCast = item as unknown as T[];
|
|
470
|
+
const itemSchema = schemaUtils.retrieveSchema(_schemaItems, itemCast);
|
|
471
|
+
const itemErrorSchema = errorSchema ? (errorSchema[index] as ErrorSchema<T[]>) : undefined;
|
|
472
|
+
const itemIdPrefix = idSchema.$id + idSeparator + index;
|
|
473
|
+
const itemIdSchema = schemaUtils.toIdSchema(itemSchema, itemIdPrefix, itemCast, idPrefix, idSeparator);
|
|
474
|
+
return this.renderArrayFieldItem({
|
|
475
|
+
key,
|
|
476
|
+
index,
|
|
477
|
+
name: name && `${name}-${index}`,
|
|
478
|
+
canAdd,
|
|
479
|
+
canMoveUp: index > 0,
|
|
480
|
+
canMoveDown: index < formData.length - 1,
|
|
481
|
+
itemSchema,
|
|
482
|
+
itemIdSchema,
|
|
483
|
+
itemErrorSchema,
|
|
484
|
+
itemData: itemCast,
|
|
485
|
+
itemUiSchema: uiSchema.items,
|
|
486
|
+
autofocus: autofocus && index === 0,
|
|
487
|
+
onBlur,
|
|
488
|
+
onFocus,
|
|
489
|
+
rawErrors,
|
|
490
|
+
totalItems: keyedFormData.length,
|
|
491
|
+
});
|
|
492
|
+
}),
|
|
493
|
+
className: `field field-array field-array-of-${itemsSchema.type}`,
|
|
494
|
+
disabled,
|
|
495
|
+
idSchema,
|
|
496
|
+
uiSchema,
|
|
497
|
+
onAddClick: this.onAddClick,
|
|
498
|
+
readonly,
|
|
499
|
+
required,
|
|
500
|
+
schema,
|
|
501
|
+
title,
|
|
502
|
+
formContext,
|
|
503
|
+
formData,
|
|
504
|
+
rawErrors,
|
|
505
|
+
registry,
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
const Template = getTemplate<'ArrayFieldTemplate', T[], S, F>('ArrayFieldTemplate', registry, uiOptions);
|
|
509
|
+
return <Template {...arrayProps} />;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/** Renders an array using the custom widget provided by the user in the `uiSchema`
|
|
513
|
+
*/
|
|
514
|
+
renderCustomWidget() {
|
|
515
|
+
const {
|
|
516
|
+
schema,
|
|
517
|
+
idSchema,
|
|
518
|
+
uiSchema,
|
|
519
|
+
disabled = false,
|
|
520
|
+
readonly = false,
|
|
521
|
+
autofocus = false,
|
|
522
|
+
required = false,
|
|
523
|
+
hideError,
|
|
524
|
+
placeholder,
|
|
525
|
+
onBlur,
|
|
526
|
+
onFocus,
|
|
527
|
+
formData: items = [],
|
|
528
|
+
registry,
|
|
529
|
+
rawErrors,
|
|
530
|
+
name,
|
|
531
|
+
} = this.props;
|
|
532
|
+
const { widgets, formContext, globalUiOptions, schemaUtils } = registry;
|
|
533
|
+
const { widget, title: uiTitle, ...options } = getUiOptions<T[], S, F>(uiSchema, globalUiOptions);
|
|
534
|
+
const Widget = getWidget<T[], S, F>(schema, widget, widgets);
|
|
535
|
+
const label = uiTitle ?? schema.title ?? name;
|
|
536
|
+
const displayLabel = schemaUtils.getDisplayLabel(schema, uiSchema, globalUiOptions);
|
|
537
|
+
return (
|
|
538
|
+
<Widget
|
|
539
|
+
id={idSchema.$id}
|
|
540
|
+
name={name}
|
|
541
|
+
multiple
|
|
542
|
+
onChange={this.onSelectChange}
|
|
543
|
+
onBlur={onBlur}
|
|
544
|
+
onFocus={onFocus}
|
|
545
|
+
options={options}
|
|
546
|
+
schema={schema}
|
|
547
|
+
uiSchema={uiSchema}
|
|
548
|
+
registry={registry}
|
|
549
|
+
value={items}
|
|
550
|
+
disabled={disabled}
|
|
551
|
+
readonly={readonly}
|
|
552
|
+
hideError={hideError}
|
|
553
|
+
required={required}
|
|
554
|
+
label={label}
|
|
555
|
+
hideLabel={!displayLabel}
|
|
556
|
+
placeholder={placeholder}
|
|
557
|
+
formContext={formContext}
|
|
558
|
+
autofocus={autofocus}
|
|
559
|
+
rawErrors={rawErrors}
|
|
560
|
+
/>
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** Renders an array as a set of checkboxes
|
|
565
|
+
*/
|
|
566
|
+
renderMultiSelect() {
|
|
567
|
+
const {
|
|
568
|
+
schema,
|
|
569
|
+
idSchema,
|
|
570
|
+
uiSchema,
|
|
571
|
+
formData: items = [],
|
|
572
|
+
disabled = false,
|
|
573
|
+
readonly = false,
|
|
574
|
+
autofocus = false,
|
|
575
|
+
required = false,
|
|
576
|
+
placeholder,
|
|
577
|
+
onBlur,
|
|
578
|
+
onFocus,
|
|
579
|
+
registry,
|
|
580
|
+
rawErrors,
|
|
581
|
+
name,
|
|
582
|
+
} = this.props;
|
|
583
|
+
const { widgets, schemaUtils, formContext, globalUiOptions } = registry;
|
|
584
|
+
const itemsSchema = schemaUtils.retrieveSchema(schema.items as S, items);
|
|
585
|
+
const enumOptions = optionsList(itemsSchema);
|
|
586
|
+
const { widget = 'select', title: uiTitle, ...options } = getUiOptions<T[], S, F>(uiSchema, globalUiOptions);
|
|
587
|
+
const Widget = getWidget<T[], S, F>(schema, widget, widgets);
|
|
588
|
+
const label = uiTitle ?? schema.title ?? name;
|
|
589
|
+
const displayLabel = schemaUtils.getDisplayLabel(schema, uiSchema, globalUiOptions);
|
|
590
|
+
return (
|
|
591
|
+
<Widget
|
|
592
|
+
id={idSchema.$id}
|
|
593
|
+
name={name}
|
|
594
|
+
multiple
|
|
595
|
+
onChange={this.onSelectChange}
|
|
596
|
+
onBlur={onBlur}
|
|
597
|
+
onFocus={onFocus}
|
|
598
|
+
options={{ ...options, enumOptions }}
|
|
599
|
+
schema={schema}
|
|
600
|
+
uiSchema={uiSchema}
|
|
601
|
+
registry={registry}
|
|
602
|
+
value={items}
|
|
603
|
+
disabled={disabled}
|
|
604
|
+
readonly={readonly}
|
|
605
|
+
required={required}
|
|
606
|
+
label={label}
|
|
607
|
+
hideLabel={!displayLabel}
|
|
608
|
+
placeholder={placeholder}
|
|
609
|
+
formContext={formContext}
|
|
610
|
+
autofocus={autofocus}
|
|
611
|
+
rawErrors={rawErrors}
|
|
612
|
+
/>
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/** Renders an array of files using the `FileWidget`
|
|
617
|
+
*/
|
|
618
|
+
renderFiles() {
|
|
619
|
+
const {
|
|
620
|
+
schema,
|
|
621
|
+
uiSchema,
|
|
622
|
+
idSchema,
|
|
623
|
+
name,
|
|
624
|
+
disabled = false,
|
|
625
|
+
readonly = false,
|
|
626
|
+
autofocus = false,
|
|
627
|
+
required = false,
|
|
628
|
+
onBlur,
|
|
629
|
+
onFocus,
|
|
630
|
+
registry,
|
|
631
|
+
formData: items = [],
|
|
632
|
+
rawErrors,
|
|
633
|
+
} = this.props;
|
|
634
|
+
const { widgets, formContext, globalUiOptions, schemaUtils } = registry;
|
|
635
|
+
const { widget = 'files', title: uiTitle, ...options } = getUiOptions<T[], S, F>(uiSchema, globalUiOptions);
|
|
636
|
+
const Widget = getWidget<T[], S, F>(schema, widget, widgets);
|
|
637
|
+
const label = uiTitle ?? schema.title ?? name;
|
|
638
|
+
const displayLabel = schemaUtils.getDisplayLabel(schema, uiSchema, globalUiOptions);
|
|
639
|
+
return (
|
|
640
|
+
<Widget
|
|
641
|
+
options={options}
|
|
642
|
+
id={idSchema.$id}
|
|
643
|
+
name={name}
|
|
644
|
+
multiple
|
|
645
|
+
onChange={this.onSelectChange}
|
|
646
|
+
onBlur={onBlur}
|
|
647
|
+
onFocus={onFocus}
|
|
648
|
+
schema={schema}
|
|
649
|
+
uiSchema={uiSchema}
|
|
650
|
+
value={items}
|
|
651
|
+
disabled={disabled}
|
|
652
|
+
readonly={readonly}
|
|
653
|
+
required={required}
|
|
654
|
+
registry={registry}
|
|
655
|
+
formContext={formContext}
|
|
656
|
+
autofocus={autofocus}
|
|
657
|
+
rawErrors={rawErrors}
|
|
658
|
+
label={label}
|
|
659
|
+
hideLabel={!displayLabel}
|
|
660
|
+
/>
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/** Renders an array that has a maximum limit of items
|
|
665
|
+
*/
|
|
666
|
+
renderFixedArray() {
|
|
667
|
+
const {
|
|
668
|
+
schema,
|
|
669
|
+
uiSchema = {},
|
|
670
|
+
formData = [],
|
|
671
|
+
errorSchema,
|
|
672
|
+
idPrefix,
|
|
673
|
+
idSeparator = '_',
|
|
674
|
+
idSchema,
|
|
675
|
+
name,
|
|
676
|
+
disabled = false,
|
|
677
|
+
readonly = false,
|
|
678
|
+
autofocus = false,
|
|
679
|
+
required = false,
|
|
680
|
+
registry,
|
|
681
|
+
onBlur,
|
|
682
|
+
onFocus,
|
|
683
|
+
rawErrors,
|
|
684
|
+
} = this.props;
|
|
685
|
+
const { keyedFormData } = this.state;
|
|
686
|
+
let { formData: items = [] } = this.props;
|
|
687
|
+
const title = schema.title || name;
|
|
688
|
+
const uiOptions = getUiOptions<T[], S, F>(uiSchema);
|
|
689
|
+
const { schemaUtils, formContext } = registry;
|
|
690
|
+
const _schemaItems: S[] = isObject(schema.items) ? (schema.items as S[]) : ([] as S[]);
|
|
691
|
+
const itemSchemas = _schemaItems.map((item: S, index: number) =>
|
|
692
|
+
schemaUtils.retrieveSchema(item, formData[index] as unknown as T[])
|
|
693
|
+
);
|
|
694
|
+
const additionalSchema = isObject(schema.additionalItems)
|
|
695
|
+
? schemaUtils.retrieveSchema(schema.additionalItems as S, formData)
|
|
696
|
+
: null;
|
|
697
|
+
|
|
698
|
+
if (!items || items.length < itemSchemas.length) {
|
|
699
|
+
// to make sure at least all fixed items are generated
|
|
700
|
+
items = items || [];
|
|
701
|
+
items = items.concat(new Array(itemSchemas.length - items.length));
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// These are the props passed into the render function
|
|
705
|
+
const canAdd = this.canAddItem(items) && !!additionalSchema;
|
|
706
|
+
const arrayProps: ArrayFieldTemplateProps<T[], S, F> = {
|
|
707
|
+
canAdd,
|
|
708
|
+
className: 'field field-array field-array-fixed-items',
|
|
709
|
+
disabled,
|
|
710
|
+
idSchema,
|
|
711
|
+
formData,
|
|
712
|
+
items: keyedFormData.map((keyedItem, index) => {
|
|
713
|
+
const { key, item } = keyedItem;
|
|
714
|
+
// While we are actually dealing with a single item of type T, the types require a T[], so cast
|
|
715
|
+
const itemCast = item as unknown as T[];
|
|
716
|
+
const additional = index >= itemSchemas.length;
|
|
717
|
+
const itemSchema =
|
|
718
|
+
additional && isObject(schema.additionalItems)
|
|
719
|
+
? schemaUtils.retrieveSchema(schema.additionalItems as S, itemCast)
|
|
720
|
+
: itemSchemas[index];
|
|
721
|
+
const itemIdPrefix = idSchema.$id + idSeparator + index;
|
|
722
|
+
const itemIdSchema = schemaUtils.toIdSchema(itemSchema, itemIdPrefix, itemCast, idPrefix, idSeparator);
|
|
723
|
+
const itemUiSchema = additional
|
|
724
|
+
? uiSchema.additionalItems || {}
|
|
725
|
+
: Array.isArray(uiSchema.items)
|
|
726
|
+
? uiSchema.items[index]
|
|
727
|
+
: uiSchema.items || {};
|
|
728
|
+
const itemErrorSchema = errorSchema ? (errorSchema[index] as ErrorSchema<T[]>) : undefined;
|
|
729
|
+
|
|
730
|
+
return this.renderArrayFieldItem({
|
|
731
|
+
key,
|
|
732
|
+
index,
|
|
733
|
+
name: name && `${name}-${index}`,
|
|
734
|
+
canAdd,
|
|
735
|
+
canRemove: additional,
|
|
736
|
+
canMoveUp: index >= itemSchemas.length + 1,
|
|
737
|
+
canMoveDown: additional && index < items.length - 1,
|
|
738
|
+
itemSchema,
|
|
739
|
+
itemData: itemCast,
|
|
740
|
+
itemUiSchema,
|
|
741
|
+
itemIdSchema,
|
|
742
|
+
itemErrorSchema,
|
|
743
|
+
autofocus: autofocus && index === 0,
|
|
744
|
+
onBlur,
|
|
745
|
+
onFocus,
|
|
746
|
+
rawErrors,
|
|
747
|
+
totalItems: keyedFormData.length,
|
|
748
|
+
});
|
|
749
|
+
}),
|
|
750
|
+
onAddClick: this.onAddClick,
|
|
751
|
+
readonly,
|
|
752
|
+
required,
|
|
753
|
+
registry,
|
|
754
|
+
schema,
|
|
755
|
+
uiSchema,
|
|
756
|
+
title,
|
|
757
|
+
formContext,
|
|
758
|
+
rawErrors,
|
|
759
|
+
};
|
|
760
|
+
|
|
761
|
+
const Template = getTemplate<'ArrayFieldTemplate', T[], S, F>('ArrayFieldTemplate', registry, uiOptions);
|
|
762
|
+
return <Template {...arrayProps} />;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
/** Renders the individual array item using a `SchemaField` along with the additional properties required to be send
|
|
766
|
+
* back to the `ArrayFieldItemTemplate`.
|
|
767
|
+
*
|
|
768
|
+
* @param props - The props for the individual array item to be rendered
|
|
769
|
+
*/
|
|
770
|
+
renderArrayFieldItem(props: {
|
|
771
|
+
key: string;
|
|
772
|
+
index: number;
|
|
773
|
+
name: string;
|
|
774
|
+
canAdd: boolean;
|
|
775
|
+
canRemove?: boolean;
|
|
776
|
+
canMoveUp: boolean;
|
|
777
|
+
canMoveDown: boolean;
|
|
778
|
+
itemSchema: S;
|
|
779
|
+
itemData: T[];
|
|
780
|
+
itemUiSchema: UiSchema<T[], S, F>;
|
|
781
|
+
itemIdSchema: IdSchema<T[]>;
|
|
782
|
+
itemErrorSchema?: ErrorSchema<T[]>;
|
|
783
|
+
autofocus?: boolean;
|
|
784
|
+
onBlur: FieldProps<T[], S, F>['onBlur'];
|
|
785
|
+
onFocus: FieldProps<T[], S, F>['onFocus'];
|
|
786
|
+
rawErrors?: string[];
|
|
787
|
+
totalItems: number;
|
|
788
|
+
}) {
|
|
789
|
+
const {
|
|
790
|
+
key,
|
|
791
|
+
index,
|
|
792
|
+
name,
|
|
793
|
+
canAdd,
|
|
794
|
+
canRemove = true,
|
|
795
|
+
canMoveUp,
|
|
796
|
+
canMoveDown,
|
|
797
|
+
itemSchema,
|
|
798
|
+
itemData,
|
|
799
|
+
itemUiSchema,
|
|
800
|
+
itemIdSchema,
|
|
801
|
+
itemErrorSchema,
|
|
802
|
+
autofocus,
|
|
803
|
+
onBlur,
|
|
804
|
+
onFocus,
|
|
805
|
+
rawErrors,
|
|
806
|
+
totalItems,
|
|
807
|
+
} = props;
|
|
808
|
+
const { disabled, hideError, idPrefix, idSeparator, readonly, uiSchema, registry, formContext } = this.props;
|
|
809
|
+
const {
|
|
810
|
+
fields: { ArraySchemaField, SchemaField },
|
|
811
|
+
globalUiOptions,
|
|
812
|
+
} = registry;
|
|
813
|
+
const ItemSchemaField = ArraySchemaField || SchemaField;
|
|
814
|
+
const { orderable = true, removable = true, copyable = false } = getUiOptions<T[], S, F>(uiSchema, globalUiOptions);
|
|
815
|
+
const has: { [key: string]: boolean } = {
|
|
816
|
+
moveUp: orderable && canMoveUp,
|
|
817
|
+
moveDown: orderable && canMoveDown,
|
|
818
|
+
copy: copyable && canAdd,
|
|
819
|
+
remove: removable && canRemove,
|
|
820
|
+
toolbar: false,
|
|
821
|
+
};
|
|
822
|
+
has.toolbar = Object.keys(has).some((key: keyof typeof has) => has[key]);
|
|
823
|
+
|
|
824
|
+
return {
|
|
825
|
+
children: (
|
|
826
|
+
<ItemSchemaField
|
|
827
|
+
name={name}
|
|
828
|
+
index={index}
|
|
829
|
+
schema={itemSchema}
|
|
830
|
+
uiSchema={itemUiSchema}
|
|
831
|
+
formData={itemData}
|
|
832
|
+
formContext={formContext}
|
|
833
|
+
errorSchema={itemErrorSchema}
|
|
834
|
+
idPrefix={idPrefix}
|
|
835
|
+
idSeparator={idSeparator}
|
|
836
|
+
idSchema={itemIdSchema}
|
|
837
|
+
required={this.isItemRequired(itemSchema)}
|
|
838
|
+
onChange={this.onChangeForIndex(index)}
|
|
839
|
+
onBlur={onBlur}
|
|
840
|
+
onFocus={onFocus}
|
|
841
|
+
registry={registry}
|
|
842
|
+
disabled={disabled}
|
|
843
|
+
readonly={readonly}
|
|
844
|
+
hideError={hideError}
|
|
845
|
+
autofocus={autofocus}
|
|
846
|
+
rawErrors={rawErrors}
|
|
847
|
+
/>
|
|
848
|
+
),
|
|
849
|
+
className: 'array-item',
|
|
850
|
+
disabled,
|
|
851
|
+
canAdd,
|
|
852
|
+
hasCopy: has.copy,
|
|
853
|
+
hasToolbar: has.toolbar,
|
|
854
|
+
hasMoveUp: has.moveUp,
|
|
855
|
+
hasMoveDown: has.moveDown,
|
|
856
|
+
hasRemove: has.remove,
|
|
857
|
+
index,
|
|
858
|
+
totalItems,
|
|
859
|
+
key,
|
|
860
|
+
onAddIndexClick: this.onAddIndexClick,
|
|
861
|
+
onCopyIndexClick: this.onCopyIndexClick,
|
|
862
|
+
onDropIndexClick: this.onDropIndexClick,
|
|
863
|
+
onReorderClick: this.onReorderClick,
|
|
864
|
+
readonly,
|
|
865
|
+
registry,
|
|
866
|
+
schema: itemSchema,
|
|
867
|
+
uiSchema: itemUiSchema,
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/** `ArrayField` is `React.ComponentType<FieldProps<T[], S, F>>` (necessarily) but the `registry` requires things to be a
|
|
873
|
+
* `Field` which is defined as `React.ComponentType<FieldProps<T, S, F>>`, so cast it to make `registry` happy.
|
|
874
|
+
*/
|
|
875
|
+
export default ArrayField;
|