@rjsf/core 5.24.4 → 5.24.7
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 +602 -601
- package/dist/index.esm.js +602 -601
- package/dist/index.esm.js.map +2 -2
- package/dist/index.js +602 -601
- package/dist/index.js.map +2 -2
- package/lib/components/Form.d.ts +1 -0
- package/lib/components/Form.d.ts.map +1 -0
- package/lib/components/Form.js +293 -293
- package/lib/components/fields/ArrayField.d.ts +1 -0
- package/lib/components/fields/ArrayField.d.ts.map +1 -0
- package/lib/components/fields/ArrayField.js +190 -194
- package/lib/components/fields/BooleanField.d.ts +1 -0
- package/lib/components/fields/BooleanField.d.ts.map +1 -0
- package/lib/components/fields/BooleanField.js +3 -5
- package/lib/components/fields/MultiSchemaField.d.ts +1 -0
- package/lib/components/fields/MultiSchemaField.d.ts.map +1 -0
- package/lib/components/fields/MultiSchemaField.js +30 -31
- package/lib/components/fields/NullField.d.ts +1 -0
- package/lib/components/fields/NullField.d.ts.map +1 -0
- package/lib/components/fields/NullField.js +0 -1
- package/lib/components/fields/NumberField.d.ts +1 -0
- package/lib/components/fields/NumberField.d.ts.map +1 -0
- package/lib/components/fields/NumberField.js +0 -1
- package/lib/components/fields/ObjectField.d.ts +1 -0
- package/lib/components/fields/ObjectField.d.ts.map +1 -0
- package/lib/components/fields/ObjectField.js +139 -145
- package/lib/components/fields/SchemaField.d.ts +1 -0
- package/lib/components/fields/SchemaField.d.ts.map +1 -0
- package/lib/components/fields/SchemaField.js +7 -9
- package/lib/components/fields/StringField.d.ts +1 -0
- package/lib/components/fields/StringField.d.ts.map +1 -0
- package/lib/components/fields/StringField.js +1 -3
- package/lib/components/fields/index.d.ts +1 -0
- package/lib/components/fields/index.d.ts.map +1 -0
- package/lib/components/fields/index.js +8 -9
- package/lib/components/templates/ArrayFieldDescriptionTemplate.d.ts +1 -0
- package/lib/components/templates/ArrayFieldDescriptionTemplate.d.ts.map +1 -0
- package/lib/components/templates/ArrayFieldDescriptionTemplate.js +0 -1
- package/lib/components/templates/ArrayFieldItemTemplate.d.ts +1 -0
- package/lib/components/templates/ArrayFieldItemTemplate.d.ts.map +1 -0
- package/lib/components/templates/ArrayFieldItemTemplate.js +0 -1
- package/lib/components/templates/ArrayFieldTemplate.d.ts +1 -0
- package/lib/components/templates/ArrayFieldTemplate.d.ts.map +1 -0
- package/lib/components/templates/ArrayFieldTemplate.js +0 -1
- package/lib/components/templates/ArrayFieldTitleTemplate.d.ts +1 -0
- package/lib/components/templates/ArrayFieldTitleTemplate.d.ts.map +1 -0
- package/lib/components/templates/ArrayFieldTitleTemplate.js +0 -1
- package/lib/components/templates/BaseInputTemplate.d.ts +1 -0
- package/lib/components/templates/BaseInputTemplate.d.ts.map +1 -0
- package/lib/components/templates/BaseInputTemplate.js +0 -1
- package/lib/components/templates/ButtonTemplates/AddButton.d.ts +1 -0
- package/lib/components/templates/ButtonTemplates/AddButton.d.ts.map +1 -0
- package/lib/components/templates/ButtonTemplates/AddButton.js +1 -2
- package/lib/components/templates/ButtonTemplates/IconButton.d.ts +1 -0
- package/lib/components/templates/ButtonTemplates/IconButton.d.ts.map +1 -0
- package/lib/components/templates/ButtonTemplates/IconButton.js +0 -1
- package/lib/components/templates/ButtonTemplates/SubmitButton.d.ts +1 -0
- package/lib/components/templates/ButtonTemplates/SubmitButton.d.ts.map +1 -0
- package/lib/components/templates/ButtonTemplates/SubmitButton.js +0 -1
- package/lib/components/templates/ButtonTemplates/index.d.ts +1 -0
- package/lib/components/templates/ButtonTemplates/index.d.ts.map +1 -0
- package/lib/components/templates/ButtonTemplates/index.js +3 -4
- package/lib/components/templates/DescriptionField.d.ts +1 -0
- package/lib/components/templates/DescriptionField.d.ts.map +1 -0
- package/lib/components/templates/DescriptionField.js +0 -1
- package/lib/components/templates/ErrorList.d.ts +1 -0
- package/lib/components/templates/ErrorList.d.ts.map +1 -0
- package/lib/components/templates/ErrorList.js +0 -1
- package/lib/components/templates/FieldErrorTemplate.d.ts +1 -0
- package/lib/components/templates/FieldErrorTemplate.d.ts.map +1 -0
- package/lib/components/templates/FieldErrorTemplate.js +0 -1
- package/lib/components/templates/FieldHelpTemplate.d.ts +1 -0
- package/lib/components/templates/FieldHelpTemplate.d.ts.map +1 -0
- package/lib/components/templates/FieldHelpTemplate.js +0 -1
- package/lib/components/templates/FieldTemplate/FieldTemplate.d.ts +1 -0
- package/lib/components/templates/FieldTemplate/FieldTemplate.d.ts.map +1 -0
- package/lib/components/templates/FieldTemplate/FieldTemplate.js +1 -2
- package/lib/components/templates/FieldTemplate/Label.d.ts +1 -0
- package/lib/components/templates/FieldTemplate/Label.d.ts.map +1 -0
- package/lib/components/templates/FieldTemplate/Label.js +0 -1
- package/lib/components/templates/FieldTemplate/index.d.ts +2 -1
- package/lib/components/templates/FieldTemplate/index.d.ts.map +1 -0
- package/lib/components/templates/FieldTemplate/index.js +1 -2
- package/lib/components/templates/ObjectFieldTemplate.d.ts +1 -0
- package/lib/components/templates/ObjectFieldTemplate.d.ts.map +1 -0
- package/lib/components/templates/ObjectFieldTemplate.js +0 -1
- package/lib/components/templates/TitleField.d.ts +1 -0
- package/lib/components/templates/TitleField.d.ts.map +1 -0
- package/lib/components/templates/TitleField.js +0 -1
- package/lib/components/templates/UnsupportedField.d.ts +1 -0
- package/lib/components/templates/UnsupportedField.d.ts.map +1 -0
- package/lib/components/templates/UnsupportedField.js +0 -1
- package/lib/components/templates/WrapIfAdditionalTemplate.d.ts +1 -0
- package/lib/components/templates/WrapIfAdditionalTemplate.d.ts.map +1 -0
- package/lib/components/templates/WrapIfAdditionalTemplate.js +1 -2
- package/lib/components/templates/index.d.ts +1 -0
- package/lib/components/templates/index.d.ts.map +1 -0
- package/lib/components/templates/index.js +15 -16
- package/lib/components/widgets/AltDateTimeWidget.d.ts +1 -0
- package/lib/components/widgets/AltDateTimeWidget.d.ts.map +1 -0
- package/lib/components/widgets/AltDateTimeWidget.js +0 -1
- package/lib/components/widgets/AltDateWidget.d.ts +1 -0
- package/lib/components/widgets/AltDateWidget.d.ts.map +1 -0
- package/lib/components/widgets/AltDateWidget.js +0 -1
- package/lib/components/widgets/CheckboxWidget.d.ts +1 -0
- package/lib/components/widgets/CheckboxWidget.d.ts.map +1 -0
- package/lib/components/widgets/CheckboxWidget.js +1 -3
- package/lib/components/widgets/CheckboxesWidget.d.ts +1 -0
- package/lib/components/widgets/CheckboxesWidget.d.ts.map +1 -0
- package/lib/components/widgets/CheckboxesWidget.js +0 -1
- package/lib/components/widgets/ColorWidget.d.ts +1 -0
- package/lib/components/widgets/ColorWidget.d.ts.map +1 -0
- package/lib/components/widgets/ColorWidget.js +0 -1
- package/lib/components/widgets/DateTimeWidget.d.ts +1 -0
- package/lib/components/widgets/DateTimeWidget.d.ts.map +1 -0
- package/lib/components/widgets/DateTimeWidget.js +0 -1
- package/lib/components/widgets/DateWidget.d.ts +1 -0
- package/lib/components/widgets/DateWidget.d.ts.map +1 -0
- package/lib/components/widgets/DateWidget.js +0 -1
- package/lib/components/widgets/EmailWidget.d.ts +1 -0
- package/lib/components/widgets/EmailWidget.d.ts.map +1 -0
- package/lib/components/widgets/EmailWidget.js +0 -1
- package/lib/components/widgets/FileWidget.d.ts +1 -0
- package/lib/components/widgets/FileWidget.d.ts.map +1 -0
- package/lib/components/widgets/FileWidget.js +1 -3
- package/lib/components/widgets/HiddenWidget.d.ts +1 -0
- package/lib/components/widgets/HiddenWidget.d.ts.map +1 -0
- package/lib/components/widgets/HiddenWidget.js +0 -1
- package/lib/components/widgets/PasswordWidget.d.ts +1 -0
- package/lib/components/widgets/PasswordWidget.d.ts.map +1 -0
- package/lib/components/widgets/PasswordWidget.js +0 -1
- package/lib/components/widgets/RadioWidget.d.ts +1 -0
- package/lib/components/widgets/RadioWidget.d.ts.map +1 -0
- package/lib/components/widgets/RadioWidget.js +0 -1
- package/lib/components/widgets/RangeWidget.d.ts +1 -0
- package/lib/components/widgets/RangeWidget.d.ts.map +1 -0
- package/lib/components/widgets/RangeWidget.js +0 -1
- package/lib/components/widgets/SelectWidget.d.ts +1 -0
- package/lib/components/widgets/SelectWidget.d.ts.map +1 -0
- package/lib/components/widgets/SelectWidget.js +0 -1
- package/lib/components/widgets/TextWidget.d.ts +1 -0
- package/lib/components/widgets/TextWidget.d.ts.map +1 -0
- package/lib/components/widgets/TextWidget.js +0 -1
- package/lib/components/widgets/TextareaWidget.d.ts +1 -0
- package/lib/components/widgets/TextareaWidget.d.ts.map +1 -0
- package/lib/components/widgets/TextareaWidget.js +0 -1
- package/lib/components/widgets/TimeWidget.d.ts +1 -0
- package/lib/components/widgets/TimeWidget.d.ts.map +1 -0
- package/lib/components/widgets/TimeWidget.js +0 -1
- package/lib/components/widgets/URLWidget.d.ts +1 -0
- package/lib/components/widgets/URLWidget.d.ts.map +1 -0
- package/lib/components/widgets/URLWidget.js +0 -1
- package/lib/components/widgets/UpDownWidget.d.ts +1 -0
- package/lib/components/widgets/UpDownWidget.d.ts.map +1 -0
- package/lib/components/widgets/UpDownWidget.js +0 -1
- package/lib/components/widgets/index.d.ts +1 -0
- package/lib/components/widgets/index.d.ts.map +1 -0
- package/lib/components/widgets/index.js +19 -20
- package/lib/getDefaultRegistry.d.ts +1 -0
- package/lib/getDefaultRegistry.d.ts.map +1 -0
- package/lib/getDefaultRegistry.js +3 -4
- package/lib/index.d.ts +4 -3
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +3 -4
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/withTheme.d.ts +2 -1
- package/lib/withTheme.d.ts.map +1 -0
- package/lib/withTheme.js +6 -8
- package/package.json +31 -9
- package/src/tsconfig.json +14 -3
- package/lib/components/Form.js.map +0 -1
- package/lib/components/fields/ArrayField.js.map +0 -1
- package/lib/components/fields/BooleanField.js.map +0 -1
- package/lib/components/fields/MultiSchemaField.js.map +0 -1
- package/lib/components/fields/NullField.js.map +0 -1
- package/lib/components/fields/NumberField.js.map +0 -1
- package/lib/components/fields/ObjectField.js.map +0 -1
- package/lib/components/fields/SchemaField.js.map +0 -1
- package/lib/components/fields/StringField.js.map +0 -1
- package/lib/components/fields/index.js.map +0 -1
- package/lib/components/templates/ArrayFieldDescriptionTemplate.js.map +0 -1
- package/lib/components/templates/ArrayFieldItemTemplate.js.map +0 -1
- package/lib/components/templates/ArrayFieldTemplate.js.map +0 -1
- package/lib/components/templates/ArrayFieldTitleTemplate.js.map +0 -1
- package/lib/components/templates/BaseInputTemplate.js.map +0 -1
- package/lib/components/templates/ButtonTemplates/AddButton.js.map +0 -1
- package/lib/components/templates/ButtonTemplates/IconButton.js.map +0 -1
- package/lib/components/templates/ButtonTemplates/SubmitButton.js.map +0 -1
- package/lib/components/templates/ButtonTemplates/index.js.map +0 -1
- package/lib/components/templates/DescriptionField.js.map +0 -1
- package/lib/components/templates/ErrorList.js.map +0 -1
- package/lib/components/templates/FieldErrorTemplate.js.map +0 -1
- package/lib/components/templates/FieldHelpTemplate.js.map +0 -1
- package/lib/components/templates/FieldTemplate/FieldTemplate.js.map +0 -1
- package/lib/components/templates/FieldTemplate/Label.js.map +0 -1
- package/lib/components/templates/FieldTemplate/index.js.map +0 -1
- package/lib/components/templates/ObjectFieldTemplate.js.map +0 -1
- package/lib/components/templates/TitleField.js.map +0 -1
- package/lib/components/templates/UnsupportedField.js.map +0 -1
- package/lib/components/templates/WrapIfAdditionalTemplate.js.map +0 -1
- package/lib/components/templates/index.js.map +0 -1
- package/lib/components/widgets/AltDateTimeWidget.js.map +0 -1
- package/lib/components/widgets/AltDateWidget.js.map +0 -1
- package/lib/components/widgets/CheckboxWidget.js.map +0 -1
- package/lib/components/widgets/CheckboxesWidget.js.map +0 -1
- package/lib/components/widgets/ColorWidget.js.map +0 -1
- package/lib/components/widgets/DateTimeWidget.js.map +0 -1
- package/lib/components/widgets/DateWidget.js.map +0 -1
- package/lib/components/widgets/EmailWidget.js.map +0 -1
- package/lib/components/widgets/FileWidget.js.map +0 -1
- package/lib/components/widgets/HiddenWidget.js.map +0 -1
- package/lib/components/widgets/PasswordWidget.js.map +0 -1
- package/lib/components/widgets/RadioWidget.js.map +0 -1
- package/lib/components/widgets/RangeWidget.js.map +0 -1
- package/lib/components/widgets/SelectWidget.js.map +0 -1
- package/lib/components/widgets/TextWidget.js.map +0 -1
- package/lib/components/widgets/TextareaWidget.js.map +0 -1
- package/lib/components/widgets/TimeWidget.js.map +0 -1
- package/lib/components/widgets/URLWidget.js.map +0 -1
- package/lib/components/widgets/UpDownWidget.js.map +0 -1
- package/lib/components/widgets/index.js.map +0 -1
- package/lib/getDefaultRegistry.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/withTheme.js.map +0 -1
package/dist/index.esm.js
CHANGED
|
@@ -72,189 +72,6 @@ var ArrayField = class extends Component {
|
|
|
72
72
|
*/
|
|
73
73
|
constructor(props) {
|
|
74
74
|
super(props);
|
|
75
|
-
/** Returns the default form information for an item based on the schema for that item. Deals with the possibility
|
|
76
|
-
* that the schema is fixed and allows additional items.
|
|
77
|
-
*/
|
|
78
|
-
this._getNewFormDataRow = () => {
|
|
79
|
-
const { schema, registry } = this.props;
|
|
80
|
-
const { schemaUtils } = registry;
|
|
81
|
-
let itemSchema = schema.items;
|
|
82
|
-
if (isFixedItems(schema) && allowAdditionalItems(schema)) {
|
|
83
|
-
itemSchema = schema.additionalItems;
|
|
84
|
-
}
|
|
85
|
-
return schemaUtils.getDefaultFormState(itemSchema);
|
|
86
|
-
};
|
|
87
|
-
/** Callback handler for when the user clicks on the add button. Creates a new row of keyed form data at the end of
|
|
88
|
-
* the list, adding it into the state, and then returning `onChange()` with the plain form data converted from the
|
|
89
|
-
* keyed data
|
|
90
|
-
*
|
|
91
|
-
* @param event - The event for the click
|
|
92
|
-
*/
|
|
93
|
-
this.onAddClick = (event) => {
|
|
94
|
-
this._handleAddClick(event);
|
|
95
|
-
};
|
|
96
|
-
/** Callback handler for when the user clicks on the add button on an existing array element. Creates a new row of
|
|
97
|
-
* keyed form data inserted at the `index`, adding it into the state, and then returning `onChange()` with the plain
|
|
98
|
-
* form data converted from the keyed data
|
|
99
|
-
*
|
|
100
|
-
* @param index - The index at which the add button is clicked
|
|
101
|
-
*/
|
|
102
|
-
this.onAddIndexClick = (index) => {
|
|
103
|
-
return (event) => {
|
|
104
|
-
this._handleAddClick(event, index);
|
|
105
|
-
};
|
|
106
|
-
};
|
|
107
|
-
/** Callback handler for when the user clicks on the copy button on an existing array element. Clones the row of
|
|
108
|
-
* keyed form data at the `index` into the next position in the state, and then returning `onChange()` with the plain
|
|
109
|
-
* form data converted from the keyed data
|
|
110
|
-
*
|
|
111
|
-
* @param index - The index at which the copy button is clicked
|
|
112
|
-
*/
|
|
113
|
-
this.onCopyIndexClick = (index) => {
|
|
114
|
-
return (event) => {
|
|
115
|
-
if (event) {
|
|
116
|
-
event.preventDefault();
|
|
117
|
-
}
|
|
118
|
-
const { onChange, errorSchema } = this.props;
|
|
119
|
-
const { keyedFormData } = this.state;
|
|
120
|
-
let newErrorSchema;
|
|
121
|
-
if (errorSchema) {
|
|
122
|
-
newErrorSchema = {};
|
|
123
|
-
for (const idx in errorSchema) {
|
|
124
|
-
const i = parseInt(idx);
|
|
125
|
-
if (i <= index) {
|
|
126
|
-
set(newErrorSchema, [i], errorSchema[idx]);
|
|
127
|
-
} else if (i > index) {
|
|
128
|
-
set(newErrorSchema, [i + 1], errorSchema[idx]);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
const newKeyedFormDataRow = {
|
|
133
|
-
key: generateRowId(),
|
|
134
|
-
item: cloneDeep(keyedFormData[index].item)
|
|
135
|
-
};
|
|
136
|
-
const newKeyedFormData = [...keyedFormData];
|
|
137
|
-
if (index !== void 0) {
|
|
138
|
-
newKeyedFormData.splice(index + 1, 0, newKeyedFormDataRow);
|
|
139
|
-
} else {
|
|
140
|
-
newKeyedFormData.push(newKeyedFormDataRow);
|
|
141
|
-
}
|
|
142
|
-
this.setState(
|
|
143
|
-
{
|
|
144
|
-
keyedFormData: newKeyedFormData,
|
|
145
|
-
updatedKeyedFormData: true
|
|
146
|
-
},
|
|
147
|
-
() => onChange(keyedToPlainFormData(newKeyedFormData), newErrorSchema)
|
|
148
|
-
);
|
|
149
|
-
};
|
|
150
|
-
};
|
|
151
|
-
/** Callback handler for when the user clicks on the remove button on an existing array element. Removes the row of
|
|
152
|
-
* keyed form data at the `index` in the state, and then returning `onChange()` with the plain form data converted
|
|
153
|
-
* from the keyed data
|
|
154
|
-
*
|
|
155
|
-
* @param index - The index at which the remove button is clicked
|
|
156
|
-
*/
|
|
157
|
-
this.onDropIndexClick = (index) => {
|
|
158
|
-
return (event) => {
|
|
159
|
-
if (event) {
|
|
160
|
-
event.preventDefault();
|
|
161
|
-
}
|
|
162
|
-
const { onChange, errorSchema } = this.props;
|
|
163
|
-
const { keyedFormData } = this.state;
|
|
164
|
-
let newErrorSchema;
|
|
165
|
-
if (errorSchema) {
|
|
166
|
-
newErrorSchema = {};
|
|
167
|
-
for (const idx in errorSchema) {
|
|
168
|
-
const i = parseInt(idx);
|
|
169
|
-
if (i < index) {
|
|
170
|
-
set(newErrorSchema, [i], errorSchema[idx]);
|
|
171
|
-
} else if (i > index) {
|
|
172
|
-
set(newErrorSchema, [i - 1], errorSchema[idx]);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
const newKeyedFormData = keyedFormData.filter((_, i) => i !== index);
|
|
177
|
-
this.setState(
|
|
178
|
-
{
|
|
179
|
-
keyedFormData: newKeyedFormData,
|
|
180
|
-
updatedKeyedFormData: true
|
|
181
|
-
},
|
|
182
|
-
() => onChange(keyedToPlainFormData(newKeyedFormData), newErrorSchema)
|
|
183
|
-
);
|
|
184
|
-
};
|
|
185
|
-
};
|
|
186
|
-
/** Callback handler for when the user clicks on one of the move item buttons on an existing array element. Moves the
|
|
187
|
-
* row of keyed form data at the `index` to the `newIndex` in the state, and then returning `onChange()` with the
|
|
188
|
-
* plain form data converted from the keyed data
|
|
189
|
-
*
|
|
190
|
-
* @param index - The index of the item to move
|
|
191
|
-
* @param newIndex - The index to where the item is to be moved
|
|
192
|
-
*/
|
|
193
|
-
this.onReorderClick = (index, newIndex) => {
|
|
194
|
-
return (event) => {
|
|
195
|
-
if (event) {
|
|
196
|
-
event.preventDefault();
|
|
197
|
-
event.currentTarget.blur();
|
|
198
|
-
}
|
|
199
|
-
const { onChange, errorSchema } = this.props;
|
|
200
|
-
let newErrorSchema;
|
|
201
|
-
if (errorSchema) {
|
|
202
|
-
newErrorSchema = {};
|
|
203
|
-
for (const idx in errorSchema) {
|
|
204
|
-
const i = parseInt(idx);
|
|
205
|
-
if (i == index) {
|
|
206
|
-
set(newErrorSchema, [newIndex], errorSchema[index]);
|
|
207
|
-
} else if (i == newIndex) {
|
|
208
|
-
set(newErrorSchema, [index], errorSchema[newIndex]);
|
|
209
|
-
} else {
|
|
210
|
-
set(newErrorSchema, [idx], errorSchema[i]);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
const { keyedFormData } = this.state;
|
|
215
|
-
function reOrderArray() {
|
|
216
|
-
const _newKeyedFormData = keyedFormData.slice();
|
|
217
|
-
_newKeyedFormData.splice(index, 1);
|
|
218
|
-
_newKeyedFormData.splice(newIndex, 0, keyedFormData[index]);
|
|
219
|
-
return _newKeyedFormData;
|
|
220
|
-
}
|
|
221
|
-
const newKeyedFormData = reOrderArray();
|
|
222
|
-
this.setState(
|
|
223
|
-
{
|
|
224
|
-
keyedFormData: newKeyedFormData
|
|
225
|
-
},
|
|
226
|
-
() => onChange(keyedToPlainFormData(newKeyedFormData), newErrorSchema)
|
|
227
|
-
);
|
|
228
|
-
};
|
|
229
|
-
};
|
|
230
|
-
/** Callback handler used to deal with changing the value of the data in the array at the `index`. Calls the
|
|
231
|
-
* `onChange` callback with the updated form data
|
|
232
|
-
*
|
|
233
|
-
* @param index - The index of the item being changed
|
|
234
|
-
*/
|
|
235
|
-
this.onChangeForIndex = (index) => {
|
|
236
|
-
return (value, newErrorSchema, id) => {
|
|
237
|
-
const { formData, onChange, errorSchema } = this.props;
|
|
238
|
-
const arrayData = Array.isArray(formData) ? formData : [];
|
|
239
|
-
const newFormData = arrayData.map((item, i) => {
|
|
240
|
-
const jsonValue = typeof value === "undefined" ? null : value;
|
|
241
|
-
return index === i ? jsonValue : item;
|
|
242
|
-
});
|
|
243
|
-
onChange(
|
|
244
|
-
newFormData,
|
|
245
|
-
errorSchema && errorSchema && {
|
|
246
|
-
...errorSchema,
|
|
247
|
-
[index]: newErrorSchema
|
|
248
|
-
},
|
|
249
|
-
id
|
|
250
|
-
);
|
|
251
|
-
};
|
|
252
|
-
};
|
|
253
|
-
/** Callback handler used to change the value for a checkbox */
|
|
254
|
-
this.onSelectChange = (value) => {
|
|
255
|
-
const { onChange, idSchema } = this.props;
|
|
256
|
-
onChange(value, void 0, idSchema && idSchema.$id);
|
|
257
|
-
};
|
|
258
75
|
const { formData = [] } = props;
|
|
259
76
|
const keyedFormData = generateKeyedFormData(formData);
|
|
260
77
|
this.state = {
|
|
@@ -329,6 +146,18 @@ var ArrayField = class extends Component {
|
|
|
329
146
|
}
|
|
330
147
|
return addable;
|
|
331
148
|
}
|
|
149
|
+
/** Returns the default form information for an item based on the schema for that item. Deals with the possibility
|
|
150
|
+
* that the schema is fixed and allows additional items.
|
|
151
|
+
*/
|
|
152
|
+
_getNewFormDataRow = () => {
|
|
153
|
+
const { schema, registry } = this.props;
|
|
154
|
+
const { schemaUtils } = registry;
|
|
155
|
+
let itemSchema = schema.items;
|
|
156
|
+
if (isFixedItems(schema) && allowAdditionalItems(schema)) {
|
|
157
|
+
itemSchema = schema.additionalItems;
|
|
158
|
+
}
|
|
159
|
+
return schemaUtils.getDefaultFormState(itemSchema);
|
|
160
|
+
};
|
|
332
161
|
/** Callback handler for when the user clicks on the add or add at index buttons. Creates a new row of keyed form data
|
|
333
162
|
* either at the end of the list (when index is not specified) or inserted at the `index` when it is, adding it into
|
|
334
163
|
* the state, and then returning `onChange()` with the plain form data converted from the keyed data
|
|
@@ -372,6 +201,177 @@ var ArrayField = class extends Component {
|
|
|
372
201
|
() => onChange(keyedToPlainFormData(newKeyedFormData), newErrorSchema)
|
|
373
202
|
);
|
|
374
203
|
}
|
|
204
|
+
/** Callback handler for when the user clicks on the add button. Creates a new row of keyed form data at the end of
|
|
205
|
+
* the list, adding it into the state, and then returning `onChange()` with the plain form data converted from the
|
|
206
|
+
* keyed data
|
|
207
|
+
*
|
|
208
|
+
* @param event - The event for the click
|
|
209
|
+
*/
|
|
210
|
+
onAddClick = (event) => {
|
|
211
|
+
this._handleAddClick(event);
|
|
212
|
+
};
|
|
213
|
+
/** Callback handler for when the user clicks on the add button on an existing array element. Creates a new row of
|
|
214
|
+
* keyed form data inserted at the `index`, adding it into the state, and then returning `onChange()` with the plain
|
|
215
|
+
* form data converted from the keyed data
|
|
216
|
+
*
|
|
217
|
+
* @param index - The index at which the add button is clicked
|
|
218
|
+
*/
|
|
219
|
+
onAddIndexClick = (index) => {
|
|
220
|
+
return (event) => {
|
|
221
|
+
this._handleAddClick(event, index);
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
/** Callback handler for when the user clicks on the copy button on an existing array element. Clones the row of
|
|
225
|
+
* keyed form data at the `index` into the next position in the state, and then returning `onChange()` with the plain
|
|
226
|
+
* form data converted from the keyed data
|
|
227
|
+
*
|
|
228
|
+
* @param index - The index at which the copy button is clicked
|
|
229
|
+
*/
|
|
230
|
+
onCopyIndexClick = (index) => {
|
|
231
|
+
return (event) => {
|
|
232
|
+
if (event) {
|
|
233
|
+
event.preventDefault();
|
|
234
|
+
}
|
|
235
|
+
const { onChange, errorSchema } = this.props;
|
|
236
|
+
const { keyedFormData } = this.state;
|
|
237
|
+
let newErrorSchema;
|
|
238
|
+
if (errorSchema) {
|
|
239
|
+
newErrorSchema = {};
|
|
240
|
+
for (const idx in errorSchema) {
|
|
241
|
+
const i = parseInt(idx);
|
|
242
|
+
if (i <= index) {
|
|
243
|
+
set(newErrorSchema, [i], errorSchema[idx]);
|
|
244
|
+
} else if (i > index) {
|
|
245
|
+
set(newErrorSchema, [i + 1], errorSchema[idx]);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const newKeyedFormDataRow = {
|
|
250
|
+
key: generateRowId(),
|
|
251
|
+
item: cloneDeep(keyedFormData[index].item)
|
|
252
|
+
};
|
|
253
|
+
const newKeyedFormData = [...keyedFormData];
|
|
254
|
+
if (index !== void 0) {
|
|
255
|
+
newKeyedFormData.splice(index + 1, 0, newKeyedFormDataRow);
|
|
256
|
+
} else {
|
|
257
|
+
newKeyedFormData.push(newKeyedFormDataRow);
|
|
258
|
+
}
|
|
259
|
+
this.setState(
|
|
260
|
+
{
|
|
261
|
+
keyedFormData: newKeyedFormData,
|
|
262
|
+
updatedKeyedFormData: true
|
|
263
|
+
},
|
|
264
|
+
() => onChange(keyedToPlainFormData(newKeyedFormData), newErrorSchema)
|
|
265
|
+
);
|
|
266
|
+
};
|
|
267
|
+
};
|
|
268
|
+
/** Callback handler for when the user clicks on the remove button on an existing array element. Removes the row of
|
|
269
|
+
* keyed form data at the `index` in the state, and then returning `onChange()` with the plain form data converted
|
|
270
|
+
* from the keyed data
|
|
271
|
+
*
|
|
272
|
+
* @param index - The index at which the remove button is clicked
|
|
273
|
+
*/
|
|
274
|
+
onDropIndexClick = (index) => {
|
|
275
|
+
return (event) => {
|
|
276
|
+
if (event) {
|
|
277
|
+
event.preventDefault();
|
|
278
|
+
}
|
|
279
|
+
const { onChange, errorSchema } = this.props;
|
|
280
|
+
const { keyedFormData } = this.state;
|
|
281
|
+
let newErrorSchema;
|
|
282
|
+
if (errorSchema) {
|
|
283
|
+
newErrorSchema = {};
|
|
284
|
+
for (const idx in errorSchema) {
|
|
285
|
+
const i = parseInt(idx);
|
|
286
|
+
if (i < index) {
|
|
287
|
+
set(newErrorSchema, [i], errorSchema[idx]);
|
|
288
|
+
} else if (i > index) {
|
|
289
|
+
set(newErrorSchema, [i - 1], errorSchema[idx]);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const newKeyedFormData = keyedFormData.filter((_, i) => i !== index);
|
|
294
|
+
this.setState(
|
|
295
|
+
{
|
|
296
|
+
keyedFormData: newKeyedFormData,
|
|
297
|
+
updatedKeyedFormData: true
|
|
298
|
+
},
|
|
299
|
+
() => onChange(keyedToPlainFormData(newKeyedFormData), newErrorSchema)
|
|
300
|
+
);
|
|
301
|
+
};
|
|
302
|
+
};
|
|
303
|
+
/** Callback handler for when the user clicks on one of the move item buttons on an existing array element. Moves the
|
|
304
|
+
* row of keyed form data at the `index` to the `newIndex` in the state, and then returning `onChange()` with the
|
|
305
|
+
* plain form data converted from the keyed data
|
|
306
|
+
*
|
|
307
|
+
* @param index - The index of the item to move
|
|
308
|
+
* @param newIndex - The index to where the item is to be moved
|
|
309
|
+
*/
|
|
310
|
+
onReorderClick = (index, newIndex) => {
|
|
311
|
+
return (event) => {
|
|
312
|
+
if (event) {
|
|
313
|
+
event.preventDefault();
|
|
314
|
+
event.currentTarget.blur();
|
|
315
|
+
}
|
|
316
|
+
const { onChange, errorSchema } = this.props;
|
|
317
|
+
let newErrorSchema;
|
|
318
|
+
if (errorSchema) {
|
|
319
|
+
newErrorSchema = {};
|
|
320
|
+
for (const idx in errorSchema) {
|
|
321
|
+
const i = parseInt(idx);
|
|
322
|
+
if (i == index) {
|
|
323
|
+
set(newErrorSchema, [newIndex], errorSchema[index]);
|
|
324
|
+
} else if (i == newIndex) {
|
|
325
|
+
set(newErrorSchema, [index], errorSchema[newIndex]);
|
|
326
|
+
} else {
|
|
327
|
+
set(newErrorSchema, [idx], errorSchema[i]);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const { keyedFormData } = this.state;
|
|
332
|
+
function reOrderArray() {
|
|
333
|
+
const _newKeyedFormData = keyedFormData.slice();
|
|
334
|
+
_newKeyedFormData.splice(index, 1);
|
|
335
|
+
_newKeyedFormData.splice(newIndex, 0, keyedFormData[index]);
|
|
336
|
+
return _newKeyedFormData;
|
|
337
|
+
}
|
|
338
|
+
const newKeyedFormData = reOrderArray();
|
|
339
|
+
this.setState(
|
|
340
|
+
{
|
|
341
|
+
keyedFormData: newKeyedFormData
|
|
342
|
+
},
|
|
343
|
+
() => onChange(keyedToPlainFormData(newKeyedFormData), newErrorSchema)
|
|
344
|
+
);
|
|
345
|
+
};
|
|
346
|
+
};
|
|
347
|
+
/** Callback handler used to deal with changing the value of the data in the array at the `index`. Calls the
|
|
348
|
+
* `onChange` callback with the updated form data
|
|
349
|
+
*
|
|
350
|
+
* @param index - The index of the item being changed
|
|
351
|
+
*/
|
|
352
|
+
onChangeForIndex = (index) => {
|
|
353
|
+
return (value, newErrorSchema, id) => {
|
|
354
|
+
const { formData, onChange, errorSchema } = this.props;
|
|
355
|
+
const arrayData = Array.isArray(formData) ? formData : [];
|
|
356
|
+
const newFormData = arrayData.map((item, i) => {
|
|
357
|
+
const jsonValue = typeof value === "undefined" ? null : value;
|
|
358
|
+
return index === i ? jsonValue : item;
|
|
359
|
+
});
|
|
360
|
+
onChange(
|
|
361
|
+
newFormData,
|
|
362
|
+
errorSchema && errorSchema && {
|
|
363
|
+
...errorSchema,
|
|
364
|
+
[index]: newErrorSchema
|
|
365
|
+
},
|
|
366
|
+
id
|
|
367
|
+
);
|
|
368
|
+
};
|
|
369
|
+
};
|
|
370
|
+
/** Callback handler used to change the value for a checkbox */
|
|
371
|
+
onSelectChange = (value) => {
|
|
372
|
+
const { onChange, idSchema } = this.props;
|
|
373
|
+
onChange(value, void 0, idSchema && idSchema.$id);
|
|
374
|
+
};
|
|
375
375
|
/** Renders the `ArrayField` depending on the specific needs of the schema and uischema elements
|
|
376
376
|
*/
|
|
377
377
|
render() {
|
|
@@ -948,30 +948,6 @@ var AnyOfField = class extends Component2 {
|
|
|
948
948
|
*/
|
|
949
949
|
constructor(props) {
|
|
950
950
|
super(props);
|
|
951
|
-
/** Callback handler to remember what the currently selected option is. In addition to that the `formData` is updated
|
|
952
|
-
* to remove properties that are not part of the newly selected option schema, and then the updated data is passed to
|
|
953
|
-
* the `onChange` handler.
|
|
954
|
-
*
|
|
955
|
-
* @param option - The new option value being selected
|
|
956
|
-
*/
|
|
957
|
-
this.onOptionChange = (option) => {
|
|
958
|
-
const { selectedOption, retrievedOptions } = this.state;
|
|
959
|
-
const { formData, onChange, registry } = this.props;
|
|
960
|
-
const { schemaUtils } = registry;
|
|
961
|
-
const intOption = option !== void 0 ? parseInt(option, 10) : -1;
|
|
962
|
-
if (intOption === selectedOption) {
|
|
963
|
-
return;
|
|
964
|
-
}
|
|
965
|
-
const newOption = intOption >= 0 ? retrievedOptions[intOption] : void 0;
|
|
966
|
-
const oldOption = selectedOption >= 0 ? retrievedOptions[selectedOption] : void 0;
|
|
967
|
-
let newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
|
|
968
|
-
if (newOption) {
|
|
969
|
-
newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, "excludeObjectChildren");
|
|
970
|
-
}
|
|
971
|
-
this.setState({ selectedOption: intOption }, () => {
|
|
972
|
-
onChange(newFormData, void 0, this.getFieldId());
|
|
973
|
-
});
|
|
974
|
-
};
|
|
975
951
|
const {
|
|
976
952
|
formData,
|
|
977
953
|
options,
|
|
@@ -1026,6 +1002,30 @@ var AnyOfField = class extends Component2 {
|
|
|
1026
1002
|
const option = schemaUtils.getClosestMatchingOption(formData, options, selectedOption, discriminator);
|
|
1027
1003
|
return option;
|
|
1028
1004
|
}
|
|
1005
|
+
/** Callback handler to remember what the currently selected option is. In addition to that the `formData` is updated
|
|
1006
|
+
* to remove properties that are not part of the newly selected option schema, and then the updated data is passed to
|
|
1007
|
+
* the `onChange` handler.
|
|
1008
|
+
*
|
|
1009
|
+
* @param option - The new option value being selected
|
|
1010
|
+
*/
|
|
1011
|
+
onOptionChange = (option) => {
|
|
1012
|
+
const { selectedOption, retrievedOptions } = this.state;
|
|
1013
|
+
const { formData, onChange, registry } = this.props;
|
|
1014
|
+
const { schemaUtils } = registry;
|
|
1015
|
+
const intOption = option !== void 0 ? parseInt(option, 10) : -1;
|
|
1016
|
+
if (intOption === selectedOption) {
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
const newOption = intOption >= 0 ? retrievedOptions[intOption] : void 0;
|
|
1020
|
+
const oldOption = selectedOption >= 0 ? retrievedOptions[selectedOption] : void 0;
|
|
1021
|
+
let newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
|
|
1022
|
+
if (newOption) {
|
|
1023
|
+
newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, "excludeObjectChildren");
|
|
1024
|
+
}
|
|
1025
|
+
this.setState({ selectedOption: intOption }, () => {
|
|
1026
|
+
onChange(newFormData, void 0, this.getFieldId());
|
|
1027
|
+
});
|
|
1028
|
+
};
|
|
1029
1029
|
getFieldId() {
|
|
1030
1030
|
const { idSchema, schema } = this.props;
|
|
1031
1031
|
return `${idSchema.$id}${schema.oneOf ? "__oneof_select" : "__anyof_select"}`;
|
|
@@ -1178,138 +1178,11 @@ import set2 from "lodash/set";
|
|
|
1178
1178
|
import unset from "lodash/unset";
|
|
1179
1179
|
import { jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
1180
1180
|
var ObjectField = class extends Component3 {
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
additionalProperties: {}
|
|
1187
|
-
};
|
|
1188
|
-
/** Returns the `onPropertyChange` handler for the `name` field. Handles the special case where a user is attempting
|
|
1189
|
-
* to clear the data for a field added as an additional property. Calls the `onChange()` handler with the updated
|
|
1190
|
-
* formData.
|
|
1191
|
-
*
|
|
1192
|
-
* @param name - The name of the property
|
|
1193
|
-
* @param addedByAdditionalProperties - Flag indicating whether this property is an additional property
|
|
1194
|
-
* @returns - The onPropertyChange callback for the `name` property
|
|
1195
|
-
*/
|
|
1196
|
-
this.onPropertyChange = (name, addedByAdditionalProperties = false) => {
|
|
1197
|
-
return (value, newErrorSchema, id) => {
|
|
1198
|
-
const { formData, onChange, errorSchema } = this.props;
|
|
1199
|
-
if (value === void 0 && addedByAdditionalProperties) {
|
|
1200
|
-
value = "";
|
|
1201
|
-
}
|
|
1202
|
-
const newFormData = { ...formData, [name]: value };
|
|
1203
|
-
onChange(
|
|
1204
|
-
newFormData,
|
|
1205
|
-
errorSchema && errorSchema && {
|
|
1206
|
-
...errorSchema,
|
|
1207
|
-
[name]: newErrorSchema
|
|
1208
|
-
},
|
|
1209
|
-
id
|
|
1210
|
-
);
|
|
1211
|
-
};
|
|
1212
|
-
};
|
|
1213
|
-
/** Returns a callback to handle the onDropPropertyClick event for the given `key` which removes the old `key` data
|
|
1214
|
-
* and calls the `onChange` callback with it
|
|
1215
|
-
*
|
|
1216
|
-
* @param key - The key for which the drop callback is desired
|
|
1217
|
-
* @returns - The drop property click callback
|
|
1218
|
-
*/
|
|
1219
|
-
this.onDropPropertyClick = (key) => {
|
|
1220
|
-
return (event) => {
|
|
1221
|
-
event.preventDefault();
|
|
1222
|
-
const { onChange, formData } = this.props;
|
|
1223
|
-
const copiedFormData = { ...formData };
|
|
1224
|
-
unset(copiedFormData, key);
|
|
1225
|
-
onChange(copiedFormData);
|
|
1226
|
-
};
|
|
1227
|
-
};
|
|
1228
|
-
/** Computes the next available key name from the `preferredKey`, indexing through the already existing keys until one
|
|
1229
|
-
* that is already not assigned is found.
|
|
1230
|
-
*
|
|
1231
|
-
* @param preferredKey - The preferred name of a new key
|
|
1232
|
-
* @param [formData] - The form data in which to check if the desired key already exists
|
|
1233
|
-
* @returns - The name of the next available key from `preferredKey`
|
|
1234
|
-
*/
|
|
1235
|
-
this.getAvailableKey = (preferredKey, formData) => {
|
|
1236
|
-
const { uiSchema, registry } = this.props;
|
|
1237
|
-
const { duplicateKeySuffixSeparator = "-" } = getUiOptions4(uiSchema, registry.globalUiOptions);
|
|
1238
|
-
let index = 0;
|
|
1239
|
-
let newKey = preferredKey;
|
|
1240
|
-
while (has(formData, newKey)) {
|
|
1241
|
-
newKey = `${preferredKey}${duplicateKeySuffixSeparator}${++index}`;
|
|
1242
|
-
}
|
|
1243
|
-
return newKey;
|
|
1244
|
-
};
|
|
1245
|
-
/** Returns a callback function that deals with the rename of a key for an additional property for a schema. That
|
|
1246
|
-
* callback will attempt to rename the key and move the existing data to that key, calling `onChange` when it does.
|
|
1247
|
-
*
|
|
1248
|
-
* @param oldValue - The old value of a field
|
|
1249
|
-
* @returns - The key change callback function
|
|
1250
|
-
*/
|
|
1251
|
-
this.onKeyChange = (oldValue) => {
|
|
1252
|
-
return (value, newErrorSchema) => {
|
|
1253
|
-
if (oldValue === value) {
|
|
1254
|
-
return;
|
|
1255
|
-
}
|
|
1256
|
-
const { formData, onChange, errorSchema } = this.props;
|
|
1257
|
-
value = this.getAvailableKey(value, formData);
|
|
1258
|
-
const newFormData = {
|
|
1259
|
-
...formData
|
|
1260
|
-
};
|
|
1261
|
-
const newKeys = { [oldValue]: value };
|
|
1262
|
-
const keyValues = Object.keys(newFormData).map((key) => {
|
|
1263
|
-
const newKey = newKeys[key] || key;
|
|
1264
|
-
return { [newKey]: newFormData[key] };
|
|
1265
|
-
});
|
|
1266
|
-
const renamedObj = Object.assign({}, ...keyValues);
|
|
1267
|
-
this.setState({ wasPropertyKeyModified: true });
|
|
1268
|
-
onChange(
|
|
1269
|
-
renamedObj,
|
|
1270
|
-
errorSchema && errorSchema && {
|
|
1271
|
-
...errorSchema,
|
|
1272
|
-
[value]: newErrorSchema
|
|
1273
|
-
}
|
|
1274
|
-
);
|
|
1275
|
-
};
|
|
1276
|
-
};
|
|
1277
|
-
/** Handles the adding of a new additional property on the given `schema`. Calls the `onChange` callback once the new
|
|
1278
|
-
* default data for that field has been added to the formData.
|
|
1279
|
-
*
|
|
1280
|
-
* @param schema - The schema element to which the new property is being added
|
|
1281
|
-
*/
|
|
1282
|
-
this.handleAddClick = (schema) => () => {
|
|
1283
|
-
if (!schema.additionalProperties) {
|
|
1284
|
-
return;
|
|
1285
|
-
}
|
|
1286
|
-
const { formData, onChange, registry } = this.props;
|
|
1287
|
-
const newFormData = { ...formData };
|
|
1288
|
-
let type = void 0;
|
|
1289
|
-
let constValue = void 0;
|
|
1290
|
-
let defaultValue = void 0;
|
|
1291
|
-
if (isObject3(schema.additionalProperties)) {
|
|
1292
|
-
type = schema.additionalProperties.type;
|
|
1293
|
-
constValue = schema.additionalProperties.const;
|
|
1294
|
-
defaultValue = schema.additionalProperties.default;
|
|
1295
|
-
let apSchema = schema.additionalProperties;
|
|
1296
|
-
if (REF_KEY in apSchema) {
|
|
1297
|
-
const { schemaUtils } = registry;
|
|
1298
|
-
apSchema = schemaUtils.retrieveSchema({ $ref: apSchema[REF_KEY] }, formData);
|
|
1299
|
-
type = apSchema.type;
|
|
1300
|
-
constValue = apSchema.const;
|
|
1301
|
-
defaultValue = apSchema.default;
|
|
1302
|
-
}
|
|
1303
|
-
if (!type && (ANY_OF_KEY2 in apSchema || ONE_OF_KEY2 in apSchema)) {
|
|
1304
|
-
type = "object";
|
|
1305
|
-
}
|
|
1306
|
-
}
|
|
1307
|
-
const newKey = this.getAvailableKey("newKey", newFormData);
|
|
1308
|
-
const newValue = constValue ?? defaultValue ?? this.getDefaultValue(type);
|
|
1309
|
-
set2(newFormData, newKey, newValue);
|
|
1310
|
-
onChange(newFormData);
|
|
1311
|
-
};
|
|
1312
|
-
}
|
|
1181
|
+
/** Set up the initial state */
|
|
1182
|
+
state = {
|
|
1183
|
+
wasPropertyKeyModified: false,
|
|
1184
|
+
additionalProperties: {}
|
|
1185
|
+
};
|
|
1313
1186
|
/** Returns a flag indicating whether the `name` field is required in the object schema
|
|
1314
1187
|
*
|
|
1315
1188
|
* @param name - The name of the field to check for required-ness
|
|
@@ -1319,6 +1192,95 @@ var ObjectField = class extends Component3 {
|
|
|
1319
1192
|
const { schema } = this.props;
|
|
1320
1193
|
return Array.isArray(schema.required) && schema.required.indexOf(name) !== -1;
|
|
1321
1194
|
}
|
|
1195
|
+
/** Returns the `onPropertyChange` handler for the `name` field. Handles the special case where a user is attempting
|
|
1196
|
+
* to clear the data for a field added as an additional property. Calls the `onChange()` handler with the updated
|
|
1197
|
+
* formData.
|
|
1198
|
+
*
|
|
1199
|
+
* @param name - The name of the property
|
|
1200
|
+
* @param addedByAdditionalProperties - Flag indicating whether this property is an additional property
|
|
1201
|
+
* @returns - The onPropertyChange callback for the `name` property
|
|
1202
|
+
*/
|
|
1203
|
+
onPropertyChange = (name, addedByAdditionalProperties = false) => {
|
|
1204
|
+
return (value, newErrorSchema, id) => {
|
|
1205
|
+
const { formData, onChange, errorSchema } = this.props;
|
|
1206
|
+
if (value === void 0 && addedByAdditionalProperties) {
|
|
1207
|
+
value = "";
|
|
1208
|
+
}
|
|
1209
|
+
const newFormData = { ...formData, [name]: value };
|
|
1210
|
+
onChange(
|
|
1211
|
+
newFormData,
|
|
1212
|
+
errorSchema && errorSchema && {
|
|
1213
|
+
...errorSchema,
|
|
1214
|
+
[name]: newErrorSchema
|
|
1215
|
+
},
|
|
1216
|
+
id
|
|
1217
|
+
);
|
|
1218
|
+
};
|
|
1219
|
+
};
|
|
1220
|
+
/** Returns a callback to handle the onDropPropertyClick event for the given `key` which removes the old `key` data
|
|
1221
|
+
* and calls the `onChange` callback with it
|
|
1222
|
+
*
|
|
1223
|
+
* @param key - The key for which the drop callback is desired
|
|
1224
|
+
* @returns - The drop property click callback
|
|
1225
|
+
*/
|
|
1226
|
+
onDropPropertyClick = (key) => {
|
|
1227
|
+
return (event) => {
|
|
1228
|
+
event.preventDefault();
|
|
1229
|
+
const { onChange, formData } = this.props;
|
|
1230
|
+
const copiedFormData = { ...formData };
|
|
1231
|
+
unset(copiedFormData, key);
|
|
1232
|
+
onChange(copiedFormData);
|
|
1233
|
+
};
|
|
1234
|
+
};
|
|
1235
|
+
/** Computes the next available key name from the `preferredKey`, indexing through the already existing keys until one
|
|
1236
|
+
* that is already not assigned is found.
|
|
1237
|
+
*
|
|
1238
|
+
* @param preferredKey - The preferred name of a new key
|
|
1239
|
+
* @param [formData] - The form data in which to check if the desired key already exists
|
|
1240
|
+
* @returns - The name of the next available key from `preferredKey`
|
|
1241
|
+
*/
|
|
1242
|
+
getAvailableKey = (preferredKey, formData) => {
|
|
1243
|
+
const { uiSchema, registry } = this.props;
|
|
1244
|
+
const { duplicateKeySuffixSeparator = "-" } = getUiOptions4(uiSchema, registry.globalUiOptions);
|
|
1245
|
+
let index = 0;
|
|
1246
|
+
let newKey = preferredKey;
|
|
1247
|
+
while (has(formData, newKey)) {
|
|
1248
|
+
newKey = `${preferredKey}${duplicateKeySuffixSeparator}${++index}`;
|
|
1249
|
+
}
|
|
1250
|
+
return newKey;
|
|
1251
|
+
};
|
|
1252
|
+
/** Returns a callback function that deals with the rename of a key for an additional property for a schema. That
|
|
1253
|
+
* callback will attempt to rename the key and move the existing data to that key, calling `onChange` when it does.
|
|
1254
|
+
*
|
|
1255
|
+
* @param oldValue - The old value of a field
|
|
1256
|
+
* @returns - The key change callback function
|
|
1257
|
+
*/
|
|
1258
|
+
onKeyChange = (oldValue) => {
|
|
1259
|
+
return (value, newErrorSchema) => {
|
|
1260
|
+
if (oldValue === value) {
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
const { formData, onChange, errorSchema } = this.props;
|
|
1264
|
+
value = this.getAvailableKey(value, formData);
|
|
1265
|
+
const newFormData = {
|
|
1266
|
+
...formData
|
|
1267
|
+
};
|
|
1268
|
+
const newKeys = { [oldValue]: value };
|
|
1269
|
+
const keyValues = Object.keys(newFormData).map((key) => {
|
|
1270
|
+
const newKey = newKeys[key] || key;
|
|
1271
|
+
return { [newKey]: newFormData[key] };
|
|
1272
|
+
});
|
|
1273
|
+
const renamedObj = Object.assign({}, ...keyValues);
|
|
1274
|
+
this.setState({ wasPropertyKeyModified: true });
|
|
1275
|
+
onChange(
|
|
1276
|
+
renamedObj,
|
|
1277
|
+
errorSchema && errorSchema && {
|
|
1278
|
+
...errorSchema,
|
|
1279
|
+
[value]: newErrorSchema
|
|
1280
|
+
}
|
|
1281
|
+
);
|
|
1282
|
+
};
|
|
1283
|
+
};
|
|
1322
1284
|
/** Returns a default value to be used for a new additional schema property of the given `type`
|
|
1323
1285
|
*
|
|
1324
1286
|
* @param type - The type of the new additional schema property
|
|
@@ -1343,6 +1305,41 @@ var ObjectField = class extends Component3 {
|
|
|
1343
1305
|
return translateString(TranslatableString4.NewStringDefault);
|
|
1344
1306
|
}
|
|
1345
1307
|
}
|
|
1308
|
+
/** Handles the adding of a new additional property on the given `schema`. Calls the `onChange` callback once the new
|
|
1309
|
+
* default data for that field has been added to the formData.
|
|
1310
|
+
*
|
|
1311
|
+
* @param schema - The schema element to which the new property is being added
|
|
1312
|
+
*/
|
|
1313
|
+
handleAddClick = (schema) => () => {
|
|
1314
|
+
if (!schema.additionalProperties) {
|
|
1315
|
+
return;
|
|
1316
|
+
}
|
|
1317
|
+
const { formData, onChange, registry } = this.props;
|
|
1318
|
+
const newFormData = { ...formData };
|
|
1319
|
+
let type = void 0;
|
|
1320
|
+
let constValue = void 0;
|
|
1321
|
+
let defaultValue = void 0;
|
|
1322
|
+
if (isObject3(schema.additionalProperties)) {
|
|
1323
|
+
type = schema.additionalProperties.type;
|
|
1324
|
+
constValue = schema.additionalProperties.const;
|
|
1325
|
+
defaultValue = schema.additionalProperties.default;
|
|
1326
|
+
let apSchema = schema.additionalProperties;
|
|
1327
|
+
if (REF_KEY in apSchema) {
|
|
1328
|
+
const { schemaUtils } = registry;
|
|
1329
|
+
apSchema = schemaUtils.retrieveSchema({ $ref: apSchema[REF_KEY] }, formData);
|
|
1330
|
+
type = apSchema.type;
|
|
1331
|
+
constValue = apSchema.const;
|
|
1332
|
+
defaultValue = apSchema.default;
|
|
1333
|
+
}
|
|
1334
|
+
if (!type && (ANY_OF_KEY2 in apSchema || ONE_OF_KEY2 in apSchema)) {
|
|
1335
|
+
type = "object";
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
const newKey = this.getAvailableKey("newKey", newFormData);
|
|
1339
|
+
const newValue = constValue ?? defaultValue ?? this.getDefaultValue(type);
|
|
1340
|
+
set2(newFormData, newKey, newValue);
|
|
1341
|
+
onChange(newFormData);
|
|
1342
|
+
};
|
|
1346
1343
|
/** Renders the `ObjectField` from the given props
|
|
1347
1344
|
*/
|
|
1348
1345
|
render() {
|
|
@@ -3342,6 +3339,10 @@ function getDefaultRegistry() {
|
|
|
3342
3339
|
// src/components/Form.tsx
|
|
3343
3340
|
import { jsx as jsx45, jsxs as jsxs21 } from "react/jsx-runtime";
|
|
3344
3341
|
var Form = class extends Component5 {
|
|
3342
|
+
/** The ref used to hold the `form` element, this needs to be `any` because `tagName` or `_internalFormWrapper` can
|
|
3343
|
+
* provide any possible type here
|
|
3344
|
+
*/
|
|
3345
|
+
formElement;
|
|
3345
3346
|
/** Constructs the `Form` from the `props`. Will setup the initial state from the props. It will also call the
|
|
3346
3347
|
* `onChange` handler if the initially provided `formData` is modified to add missing default values as part of the
|
|
3347
3348
|
* state construction.
|
|
@@ -3350,268 +3351,6 @@ var Form = class extends Component5 {
|
|
|
3350
3351
|
*/
|
|
3351
3352
|
constructor(props) {
|
|
3352
3353
|
super(props);
|
|
3353
|
-
/** Returns the `formData` with only the elements specified in the `fields` list
|
|
3354
|
-
*
|
|
3355
|
-
* @param formData - The data for the `Form`
|
|
3356
|
-
* @param fields - The fields to keep while filtering
|
|
3357
|
-
*/
|
|
3358
|
-
this.getUsedFormData = (formData, fields2) => {
|
|
3359
|
-
if (fields2.length === 0 && typeof formData !== "object") {
|
|
3360
|
-
return formData;
|
|
3361
|
-
}
|
|
3362
|
-
const data = _pick(formData, fields2);
|
|
3363
|
-
if (Array.isArray(formData)) {
|
|
3364
|
-
return Object.keys(data).map((key) => data[key]);
|
|
3365
|
-
}
|
|
3366
|
-
return data;
|
|
3367
|
-
};
|
|
3368
|
-
/** Returns the list of field names from inspecting the `pathSchema` as well as using the `formData`
|
|
3369
|
-
*
|
|
3370
|
-
* @param pathSchema - The `PathSchema` object for the form
|
|
3371
|
-
* @param [formData] - The form data to use while checking for empty objects/arrays
|
|
3372
|
-
*/
|
|
3373
|
-
this.getFieldNames = (pathSchema, formData) => {
|
|
3374
|
-
const getAllPaths = (_obj, acc = [], paths = [[]]) => {
|
|
3375
|
-
Object.keys(_obj).forEach((key) => {
|
|
3376
|
-
if (typeof _obj[key] === "object") {
|
|
3377
|
-
const newPaths = paths.map((path) => [...path, key]);
|
|
3378
|
-
if (_obj[key][RJSF_ADDITIONAL_PROPERTIES_FLAG] && _obj[key][NAME_KEY] !== "") {
|
|
3379
|
-
acc.push(_obj[key][NAME_KEY]);
|
|
3380
|
-
} else {
|
|
3381
|
-
getAllPaths(_obj[key], acc, newPaths);
|
|
3382
|
-
}
|
|
3383
|
-
} else if (key === NAME_KEY && _obj[key] !== "") {
|
|
3384
|
-
paths.forEach((path) => {
|
|
3385
|
-
const formValue = _get(formData, path);
|
|
3386
|
-
if (typeof formValue !== "object" || _isEmpty(formValue) || Array.isArray(formValue) && formValue.every((val) => typeof val !== "object")) {
|
|
3387
|
-
acc.push(path);
|
|
3388
|
-
}
|
|
3389
|
-
});
|
|
3390
|
-
}
|
|
3391
|
-
});
|
|
3392
|
-
return acc;
|
|
3393
|
-
};
|
|
3394
|
-
return getAllPaths(pathSchema);
|
|
3395
|
-
};
|
|
3396
|
-
/** Returns the `formData` after filtering to remove any extra data not in a form field
|
|
3397
|
-
*
|
|
3398
|
-
* @param formData - The data for the `Form`
|
|
3399
|
-
* @returns The `formData` after omitting extra data
|
|
3400
|
-
*/
|
|
3401
|
-
this.omitExtraData = (formData) => {
|
|
3402
|
-
const { schema, schemaUtils } = this.state;
|
|
3403
|
-
const retrievedSchema = schemaUtils.retrieveSchema(schema, formData);
|
|
3404
|
-
const pathSchema = schemaUtils.toPathSchema(retrievedSchema, "", formData);
|
|
3405
|
-
const fieldNames = this.getFieldNames(pathSchema, formData);
|
|
3406
|
-
const newFormData = this.getUsedFormData(formData, fieldNames);
|
|
3407
|
-
return newFormData;
|
|
3408
|
-
};
|
|
3409
|
-
/** Function to handle changes made to a field in the `Form`. This handler receives an entirely new copy of the
|
|
3410
|
-
* `formData` along with a new `ErrorSchema`. It will first update the `formData` with any missing default fields and
|
|
3411
|
-
* then, if `omitExtraData` and `liveOmit` are turned on, the `formData` will be filtered to remove any extra data not
|
|
3412
|
-
* in a form field. Then, the resulting formData will be validated if required. The state will be updated with the new
|
|
3413
|
-
* updated (potentially filtered) `formData`, any errors that resulted from validation. Finally the `onChange`
|
|
3414
|
-
* callback will be called if specified with the updated state.
|
|
3415
|
-
*
|
|
3416
|
-
* @param formData - The new form data from a change to a field
|
|
3417
|
-
* @param newErrorSchema - The new `ErrorSchema` based on the field change
|
|
3418
|
-
* @param id - The id of the field that caused the change
|
|
3419
|
-
*/
|
|
3420
|
-
this.onChange = (formData, newErrorSchema, id) => {
|
|
3421
|
-
const { extraErrors, omitExtraData, liveOmit, noValidate, liveValidate, onChange } = this.props;
|
|
3422
|
-
const { schemaUtils, schema } = this.state;
|
|
3423
|
-
let retrievedSchema = this.state.retrievedSchema;
|
|
3424
|
-
if (isObject5(formData) || Array.isArray(formData)) {
|
|
3425
|
-
const newState = this.getStateFromProps(this.props, formData);
|
|
3426
|
-
formData = newState.formData;
|
|
3427
|
-
retrievedSchema = newState.retrievedSchema;
|
|
3428
|
-
}
|
|
3429
|
-
const mustValidate = !noValidate && liveValidate;
|
|
3430
|
-
let state = { formData, schema };
|
|
3431
|
-
let newFormData = formData;
|
|
3432
|
-
if (omitExtraData === true && liveOmit === true) {
|
|
3433
|
-
newFormData = this.omitExtraData(formData);
|
|
3434
|
-
state = {
|
|
3435
|
-
formData: newFormData
|
|
3436
|
-
};
|
|
3437
|
-
}
|
|
3438
|
-
if (mustValidate) {
|
|
3439
|
-
const schemaValidation = this.validate(newFormData, schema, schemaUtils, retrievedSchema);
|
|
3440
|
-
let errors = schemaValidation.errors;
|
|
3441
|
-
let errorSchema = schemaValidation.errorSchema;
|
|
3442
|
-
const schemaValidationErrors = errors;
|
|
3443
|
-
const schemaValidationErrorSchema = errorSchema;
|
|
3444
|
-
if (extraErrors) {
|
|
3445
|
-
const merged = validationDataMerge(schemaValidation, extraErrors);
|
|
3446
|
-
errorSchema = merged.errorSchema;
|
|
3447
|
-
errors = merged.errors;
|
|
3448
|
-
}
|
|
3449
|
-
if (newErrorSchema) {
|
|
3450
|
-
const filteredErrors = this.filterErrorsBasedOnSchema(newErrorSchema, retrievedSchema, newFormData);
|
|
3451
|
-
errorSchema = mergeObjects2(errorSchema, filteredErrors, "preventDuplicates");
|
|
3452
|
-
}
|
|
3453
|
-
state = {
|
|
3454
|
-
formData: newFormData,
|
|
3455
|
-
errors,
|
|
3456
|
-
errorSchema,
|
|
3457
|
-
schemaValidationErrors,
|
|
3458
|
-
schemaValidationErrorSchema
|
|
3459
|
-
};
|
|
3460
|
-
} else if (!noValidate && newErrorSchema) {
|
|
3461
|
-
const errorSchema = extraErrors ? mergeObjects2(newErrorSchema, extraErrors, "preventDuplicates") : newErrorSchema;
|
|
3462
|
-
state = {
|
|
3463
|
-
formData: newFormData,
|
|
3464
|
-
errorSchema,
|
|
3465
|
-
errors: toErrorList(errorSchema)
|
|
3466
|
-
};
|
|
3467
|
-
}
|
|
3468
|
-
this.setState(state, () => onChange && onChange({ ...this.state, ...state }, id));
|
|
3469
|
-
};
|
|
3470
|
-
/**
|
|
3471
|
-
* Callback function to handle reset form data.
|
|
3472
|
-
* - Reset all fields with default values.
|
|
3473
|
-
* - Reset validations and errors
|
|
3474
|
-
*
|
|
3475
|
-
*/
|
|
3476
|
-
this.reset = () => {
|
|
3477
|
-
const { onChange } = this.props;
|
|
3478
|
-
const newState = this.getStateFromProps(this.props, void 0);
|
|
3479
|
-
const newFormData = newState.formData;
|
|
3480
|
-
const state = {
|
|
3481
|
-
formData: newFormData,
|
|
3482
|
-
errorSchema: {},
|
|
3483
|
-
errors: [],
|
|
3484
|
-
schemaValidationErrors: [],
|
|
3485
|
-
schemaValidationErrorSchema: {}
|
|
3486
|
-
};
|
|
3487
|
-
this.setState(state, () => onChange && onChange({ ...this.state, ...state }));
|
|
3488
|
-
};
|
|
3489
|
-
/** Callback function to handle when a field on the form is blurred. Calls the `onBlur` callback for the `Form` if it
|
|
3490
|
-
* was provided.
|
|
3491
|
-
*
|
|
3492
|
-
* @param id - The unique `id` of the field that was blurred
|
|
3493
|
-
* @param data - The data associated with the field that was blurred
|
|
3494
|
-
*/
|
|
3495
|
-
this.onBlur = (id, data) => {
|
|
3496
|
-
const { onBlur } = this.props;
|
|
3497
|
-
if (onBlur) {
|
|
3498
|
-
onBlur(id, data);
|
|
3499
|
-
}
|
|
3500
|
-
};
|
|
3501
|
-
/** Callback function to handle when a field on the form is focused. Calls the `onFocus` callback for the `Form` if it
|
|
3502
|
-
* was provided.
|
|
3503
|
-
*
|
|
3504
|
-
* @param id - The unique `id` of the field that was focused
|
|
3505
|
-
* @param data - The data associated with the field that was focused
|
|
3506
|
-
*/
|
|
3507
|
-
this.onFocus = (id, data) => {
|
|
3508
|
-
const { onFocus } = this.props;
|
|
3509
|
-
if (onFocus) {
|
|
3510
|
-
onFocus(id, data);
|
|
3511
|
-
}
|
|
3512
|
-
};
|
|
3513
|
-
/** Callback function to handle when the form is submitted. First, it prevents the default event behavior. Nothing
|
|
3514
|
-
* happens if the target and currentTarget of the event are not the same. It will omit any extra data in the
|
|
3515
|
-
* `formData` in the state if `omitExtraData` is true. It will validate the resulting `formData`, reporting errors
|
|
3516
|
-
* via the `onError()` callback unless validation is disabled. Finally, it will add in any `extraErrors` and then call
|
|
3517
|
-
* back the `onSubmit` callback if it was provided.
|
|
3518
|
-
*
|
|
3519
|
-
* @param event - The submit HTML form event
|
|
3520
|
-
*/
|
|
3521
|
-
this.onSubmit = (event) => {
|
|
3522
|
-
event.preventDefault();
|
|
3523
|
-
if (event.target !== event.currentTarget) {
|
|
3524
|
-
return;
|
|
3525
|
-
}
|
|
3526
|
-
event.persist();
|
|
3527
|
-
const { omitExtraData, extraErrors, noValidate, onSubmit } = this.props;
|
|
3528
|
-
let { formData: newFormData } = this.state;
|
|
3529
|
-
if (omitExtraData === true) {
|
|
3530
|
-
newFormData = this.omitExtraData(newFormData);
|
|
3531
|
-
}
|
|
3532
|
-
if (noValidate || this.validateFormWithFormData(newFormData)) {
|
|
3533
|
-
const errorSchema = extraErrors || {};
|
|
3534
|
-
const errors = extraErrors ? toErrorList(extraErrors) : [];
|
|
3535
|
-
this.setState(
|
|
3536
|
-
{
|
|
3537
|
-
formData: newFormData,
|
|
3538
|
-
errors,
|
|
3539
|
-
errorSchema,
|
|
3540
|
-
schemaValidationErrors: [],
|
|
3541
|
-
schemaValidationErrorSchema: {}
|
|
3542
|
-
},
|
|
3543
|
-
() => {
|
|
3544
|
-
if (onSubmit) {
|
|
3545
|
-
onSubmit({ ...this.state, formData: newFormData, status: "submitted" }, event);
|
|
3546
|
-
}
|
|
3547
|
-
}
|
|
3548
|
-
);
|
|
3549
|
-
}
|
|
3550
|
-
};
|
|
3551
|
-
/** Provides a function that can be used to programmatically submit the `Form` */
|
|
3552
|
-
this.submit = () => {
|
|
3553
|
-
if (this.formElement.current) {
|
|
3554
|
-
const submitCustomEvent = new CustomEvent("submit", {
|
|
3555
|
-
cancelable: true
|
|
3556
|
-
});
|
|
3557
|
-
submitCustomEvent.preventDefault();
|
|
3558
|
-
this.formElement.current.dispatchEvent(submitCustomEvent);
|
|
3559
|
-
this.formElement.current.requestSubmit();
|
|
3560
|
-
}
|
|
3561
|
-
};
|
|
3562
|
-
/** Validates the form using the given `formData`. For use on form submission or on programmatic validation.
|
|
3563
|
-
* If `onError` is provided, then it will be called with the list of errors.
|
|
3564
|
-
*
|
|
3565
|
-
* @param formData - The form data to validate
|
|
3566
|
-
* @returns - True if the form is valid, false otherwise.
|
|
3567
|
-
*/
|
|
3568
|
-
this.validateFormWithFormData = (formData) => {
|
|
3569
|
-
const { extraErrors, extraErrorsBlockSubmit, focusOnFirstError, onError } = this.props;
|
|
3570
|
-
const { errors: prevErrors } = this.state;
|
|
3571
|
-
const schemaValidation = this.validate(formData);
|
|
3572
|
-
let errors = schemaValidation.errors;
|
|
3573
|
-
let errorSchema = schemaValidation.errorSchema;
|
|
3574
|
-
const schemaValidationErrors = errors;
|
|
3575
|
-
const schemaValidationErrorSchema = errorSchema;
|
|
3576
|
-
const hasError = errors.length > 0 || extraErrors && extraErrorsBlockSubmit;
|
|
3577
|
-
if (hasError) {
|
|
3578
|
-
if (extraErrors) {
|
|
3579
|
-
const merged = validationDataMerge(schemaValidation, extraErrors);
|
|
3580
|
-
errorSchema = merged.errorSchema;
|
|
3581
|
-
errors = merged.errors;
|
|
3582
|
-
}
|
|
3583
|
-
if (focusOnFirstError) {
|
|
3584
|
-
if (typeof focusOnFirstError === "function") {
|
|
3585
|
-
focusOnFirstError(errors[0]);
|
|
3586
|
-
} else {
|
|
3587
|
-
this.focusOnError(errors[0]);
|
|
3588
|
-
}
|
|
3589
|
-
}
|
|
3590
|
-
this.setState(
|
|
3591
|
-
{
|
|
3592
|
-
errors,
|
|
3593
|
-
errorSchema,
|
|
3594
|
-
schemaValidationErrors,
|
|
3595
|
-
schemaValidationErrorSchema
|
|
3596
|
-
},
|
|
3597
|
-
() => {
|
|
3598
|
-
if (onError) {
|
|
3599
|
-
onError(errors);
|
|
3600
|
-
} else {
|
|
3601
|
-
console.error("Form validation failed", errors);
|
|
3602
|
-
}
|
|
3603
|
-
}
|
|
3604
|
-
);
|
|
3605
|
-
} else if (prevErrors.length > 0) {
|
|
3606
|
-
this.setState({
|
|
3607
|
-
errors: [],
|
|
3608
|
-
errorSchema: {},
|
|
3609
|
-
schemaValidationErrors: [],
|
|
3610
|
-
schemaValidationErrorSchema: {}
|
|
3611
|
-
});
|
|
3612
|
-
}
|
|
3613
|
-
return !hasError;
|
|
3614
|
-
};
|
|
3615
3354
|
if (!props.validator) {
|
|
3616
3355
|
throw new Error("A validator is required for Form functionality to work");
|
|
3617
3356
|
}
|
|
@@ -3855,6 +3594,62 @@ var Form = class extends Component5 {
|
|
|
3855
3594
|
}
|
|
3856
3595
|
return null;
|
|
3857
3596
|
}
|
|
3597
|
+
/** Returns the `formData` with only the elements specified in the `fields` list
|
|
3598
|
+
*
|
|
3599
|
+
* @param formData - The data for the `Form`
|
|
3600
|
+
* @param fields - The fields to keep while filtering
|
|
3601
|
+
*/
|
|
3602
|
+
getUsedFormData = (formData, fields2) => {
|
|
3603
|
+
if (fields2.length === 0 && typeof formData !== "object") {
|
|
3604
|
+
return formData;
|
|
3605
|
+
}
|
|
3606
|
+
const data = _pick(formData, fields2);
|
|
3607
|
+
if (Array.isArray(formData)) {
|
|
3608
|
+
return Object.keys(data).map((key) => data[key]);
|
|
3609
|
+
}
|
|
3610
|
+
return data;
|
|
3611
|
+
};
|
|
3612
|
+
/** Returns the list of field names from inspecting the `pathSchema` as well as using the `formData`
|
|
3613
|
+
*
|
|
3614
|
+
* @param pathSchema - The `PathSchema` object for the form
|
|
3615
|
+
* @param [formData] - The form data to use while checking for empty objects/arrays
|
|
3616
|
+
*/
|
|
3617
|
+
getFieldNames = (pathSchema, formData) => {
|
|
3618
|
+
const getAllPaths = (_obj, acc = [], paths = [[]]) => {
|
|
3619
|
+
Object.keys(_obj).forEach((key) => {
|
|
3620
|
+
if (typeof _obj[key] === "object") {
|
|
3621
|
+
const newPaths = paths.map((path) => [...path, key]);
|
|
3622
|
+
if (_obj[key][RJSF_ADDITIONAL_PROPERTIES_FLAG] && _obj[key][NAME_KEY] !== "") {
|
|
3623
|
+
acc.push(_obj[key][NAME_KEY]);
|
|
3624
|
+
} else {
|
|
3625
|
+
getAllPaths(_obj[key], acc, newPaths);
|
|
3626
|
+
}
|
|
3627
|
+
} else if (key === NAME_KEY && _obj[key] !== "") {
|
|
3628
|
+
paths.forEach((path) => {
|
|
3629
|
+
const formValue = _get(formData, path);
|
|
3630
|
+
if (typeof formValue !== "object" || _isEmpty(formValue) || Array.isArray(formValue) && formValue.every((val) => typeof val !== "object")) {
|
|
3631
|
+
acc.push(path);
|
|
3632
|
+
}
|
|
3633
|
+
});
|
|
3634
|
+
}
|
|
3635
|
+
});
|
|
3636
|
+
return acc;
|
|
3637
|
+
};
|
|
3638
|
+
return getAllPaths(pathSchema);
|
|
3639
|
+
};
|
|
3640
|
+
/** Returns the `formData` after filtering to remove any extra data not in a form field
|
|
3641
|
+
*
|
|
3642
|
+
* @param formData - The data for the `Form`
|
|
3643
|
+
* @returns The `formData` after omitting extra data
|
|
3644
|
+
*/
|
|
3645
|
+
omitExtraData = (formData) => {
|
|
3646
|
+
const { schema, schemaUtils } = this.state;
|
|
3647
|
+
const retrievedSchema = schemaUtils.retrieveSchema(schema, formData);
|
|
3648
|
+
const pathSchema = schemaUtils.toPathSchema(retrievedSchema, "", formData);
|
|
3649
|
+
const fieldNames = this.getFieldNames(pathSchema, formData);
|
|
3650
|
+
const newFormData = this.getUsedFormData(formData, fieldNames);
|
|
3651
|
+
return newFormData;
|
|
3652
|
+
};
|
|
3858
3653
|
// Filtering errors based on your retrieved schema to only show errors for properties in the selected branch.
|
|
3859
3654
|
filterErrorsBasedOnSchema(schemaErrors, resolvedSchema, formData) {
|
|
3860
3655
|
const { retrievedSchema, schemaUtils } = this.state;
|
|
@@ -3889,6 +3684,67 @@ var Form = class extends Component5 {
|
|
|
3889
3684
|
};
|
|
3890
3685
|
return filterNilOrEmptyErrors(filteredErrors, prevCustomValidateErrors);
|
|
3891
3686
|
}
|
|
3687
|
+
/** Function to handle changes made to a field in the `Form`. This handler receives an entirely new copy of the
|
|
3688
|
+
* `formData` along with a new `ErrorSchema`. It will first update the `formData` with any missing default fields and
|
|
3689
|
+
* then, if `omitExtraData` and `liveOmit` are turned on, the `formData` will be filtered to remove any extra data not
|
|
3690
|
+
* in a form field. Then, the resulting formData will be validated if required. The state will be updated with the new
|
|
3691
|
+
* updated (potentially filtered) `formData`, any errors that resulted from validation. Finally the `onChange`
|
|
3692
|
+
* callback will be called if specified with the updated state.
|
|
3693
|
+
*
|
|
3694
|
+
* @param formData - The new form data from a change to a field
|
|
3695
|
+
* @param newErrorSchema - The new `ErrorSchema` based on the field change
|
|
3696
|
+
* @param id - The id of the field that caused the change
|
|
3697
|
+
*/
|
|
3698
|
+
onChange = (formData, newErrorSchema, id) => {
|
|
3699
|
+
const { extraErrors, omitExtraData, liveOmit, noValidate, liveValidate, onChange } = this.props;
|
|
3700
|
+
const { schemaUtils, schema } = this.state;
|
|
3701
|
+
let retrievedSchema = this.state.retrievedSchema;
|
|
3702
|
+
if (isObject5(formData) || Array.isArray(formData)) {
|
|
3703
|
+
const newState = this.getStateFromProps(this.props, formData);
|
|
3704
|
+
formData = newState.formData;
|
|
3705
|
+
retrievedSchema = newState.retrievedSchema;
|
|
3706
|
+
}
|
|
3707
|
+
const mustValidate = !noValidate && liveValidate;
|
|
3708
|
+
let state = { formData, schema };
|
|
3709
|
+
let newFormData = formData;
|
|
3710
|
+
if (omitExtraData === true && liveOmit === true) {
|
|
3711
|
+
newFormData = this.omitExtraData(formData);
|
|
3712
|
+
state = {
|
|
3713
|
+
formData: newFormData
|
|
3714
|
+
};
|
|
3715
|
+
}
|
|
3716
|
+
if (mustValidate) {
|
|
3717
|
+
const schemaValidation = this.validate(newFormData, schema, schemaUtils, retrievedSchema);
|
|
3718
|
+
let errors = schemaValidation.errors;
|
|
3719
|
+
let errorSchema = schemaValidation.errorSchema;
|
|
3720
|
+
const schemaValidationErrors = errors;
|
|
3721
|
+
const schemaValidationErrorSchema = errorSchema;
|
|
3722
|
+
if (extraErrors) {
|
|
3723
|
+
const merged = validationDataMerge(schemaValidation, extraErrors);
|
|
3724
|
+
errorSchema = merged.errorSchema;
|
|
3725
|
+
errors = merged.errors;
|
|
3726
|
+
}
|
|
3727
|
+
if (newErrorSchema) {
|
|
3728
|
+
const filteredErrors = this.filterErrorsBasedOnSchema(newErrorSchema, retrievedSchema, newFormData);
|
|
3729
|
+
errorSchema = mergeObjects2(errorSchema, filteredErrors, "preventDuplicates");
|
|
3730
|
+
}
|
|
3731
|
+
state = {
|
|
3732
|
+
formData: newFormData,
|
|
3733
|
+
errors,
|
|
3734
|
+
errorSchema,
|
|
3735
|
+
schemaValidationErrors,
|
|
3736
|
+
schemaValidationErrorSchema
|
|
3737
|
+
};
|
|
3738
|
+
} else if (!noValidate && newErrorSchema) {
|
|
3739
|
+
const errorSchema = extraErrors ? mergeObjects2(newErrorSchema, extraErrors, "preventDuplicates") : newErrorSchema;
|
|
3740
|
+
state = {
|
|
3741
|
+
formData: newFormData,
|
|
3742
|
+
errorSchema,
|
|
3743
|
+
errors: toErrorList(errorSchema)
|
|
3744
|
+
};
|
|
3745
|
+
}
|
|
3746
|
+
this.setState(state, () => onChange && onChange({ ...this.state, ...state }, id));
|
|
3747
|
+
};
|
|
3892
3748
|
/**
|
|
3893
3749
|
* If the retrievedSchema has changed the new retrievedSchema is returned.
|
|
3894
3750
|
* Otherwise, the old retrievedSchema is returned to persist reference.
|
|
@@ -3902,6 +3758,87 @@ var Form = class extends Component5 {
|
|
|
3902
3758
|
const isTheSame = deepEquals3(retrievedSchema, this.state?.retrievedSchema);
|
|
3903
3759
|
return isTheSame ? this.state.retrievedSchema : retrievedSchema;
|
|
3904
3760
|
}
|
|
3761
|
+
/**
|
|
3762
|
+
* Callback function to handle reset form data.
|
|
3763
|
+
* - Reset all fields with default values.
|
|
3764
|
+
* - Reset validations and errors
|
|
3765
|
+
*
|
|
3766
|
+
*/
|
|
3767
|
+
reset = () => {
|
|
3768
|
+
const { onChange } = this.props;
|
|
3769
|
+
const newState = this.getStateFromProps(this.props, void 0);
|
|
3770
|
+
const newFormData = newState.formData;
|
|
3771
|
+
const state = {
|
|
3772
|
+
formData: newFormData,
|
|
3773
|
+
errorSchema: {},
|
|
3774
|
+
errors: [],
|
|
3775
|
+
schemaValidationErrors: [],
|
|
3776
|
+
schemaValidationErrorSchema: {}
|
|
3777
|
+
};
|
|
3778
|
+
this.setState(state, () => onChange && onChange({ ...this.state, ...state }));
|
|
3779
|
+
};
|
|
3780
|
+
/** Callback function to handle when a field on the form is blurred. Calls the `onBlur` callback for the `Form` if it
|
|
3781
|
+
* was provided.
|
|
3782
|
+
*
|
|
3783
|
+
* @param id - The unique `id` of the field that was blurred
|
|
3784
|
+
* @param data - The data associated with the field that was blurred
|
|
3785
|
+
*/
|
|
3786
|
+
onBlur = (id, data) => {
|
|
3787
|
+
const { onBlur } = this.props;
|
|
3788
|
+
if (onBlur) {
|
|
3789
|
+
onBlur(id, data);
|
|
3790
|
+
}
|
|
3791
|
+
};
|
|
3792
|
+
/** Callback function to handle when a field on the form is focused. Calls the `onFocus` callback for the `Form` if it
|
|
3793
|
+
* was provided.
|
|
3794
|
+
*
|
|
3795
|
+
* @param id - The unique `id` of the field that was focused
|
|
3796
|
+
* @param data - The data associated with the field that was focused
|
|
3797
|
+
*/
|
|
3798
|
+
onFocus = (id, data) => {
|
|
3799
|
+
const { onFocus } = this.props;
|
|
3800
|
+
if (onFocus) {
|
|
3801
|
+
onFocus(id, data);
|
|
3802
|
+
}
|
|
3803
|
+
};
|
|
3804
|
+
/** Callback function to handle when the form is submitted. First, it prevents the default event behavior. Nothing
|
|
3805
|
+
* happens if the target and currentTarget of the event are not the same. It will omit any extra data in the
|
|
3806
|
+
* `formData` in the state if `omitExtraData` is true. It will validate the resulting `formData`, reporting errors
|
|
3807
|
+
* via the `onError()` callback unless validation is disabled. Finally, it will add in any `extraErrors` and then call
|
|
3808
|
+
* back the `onSubmit` callback if it was provided.
|
|
3809
|
+
*
|
|
3810
|
+
* @param event - The submit HTML form event
|
|
3811
|
+
*/
|
|
3812
|
+
onSubmit = (event) => {
|
|
3813
|
+
event.preventDefault();
|
|
3814
|
+
if (event.target !== event.currentTarget) {
|
|
3815
|
+
return;
|
|
3816
|
+
}
|
|
3817
|
+
event.persist();
|
|
3818
|
+
const { omitExtraData, extraErrors, noValidate, onSubmit } = this.props;
|
|
3819
|
+
let { formData: newFormData } = this.state;
|
|
3820
|
+
if (omitExtraData === true) {
|
|
3821
|
+
newFormData = this.omitExtraData(newFormData);
|
|
3822
|
+
}
|
|
3823
|
+
if (noValidate || this.validateFormWithFormData(newFormData)) {
|
|
3824
|
+
const errorSchema = extraErrors || {};
|
|
3825
|
+
const errors = extraErrors ? toErrorList(extraErrors) : [];
|
|
3826
|
+
this.setState(
|
|
3827
|
+
{
|
|
3828
|
+
formData: newFormData,
|
|
3829
|
+
errors,
|
|
3830
|
+
errorSchema,
|
|
3831
|
+
schemaValidationErrors: [],
|
|
3832
|
+
schemaValidationErrorSchema: {}
|
|
3833
|
+
},
|
|
3834
|
+
() => {
|
|
3835
|
+
if (onSubmit) {
|
|
3836
|
+
onSubmit({ ...this.state, formData: newFormData, status: "submitted" }, event);
|
|
3837
|
+
}
|
|
3838
|
+
}
|
|
3839
|
+
);
|
|
3840
|
+
}
|
|
3841
|
+
};
|
|
3905
3842
|
/** Returns the registry for the form */
|
|
3906
3843
|
getRegistry() {
|
|
3907
3844
|
const { translateString: customTranslateString, uiSchema = {} } = this.props;
|
|
@@ -3925,6 +3862,17 @@ var Form = class extends Component5 {
|
|
|
3925
3862
|
globalUiOptions: uiSchema[UI_GLOBAL_OPTIONS_KEY]
|
|
3926
3863
|
};
|
|
3927
3864
|
}
|
|
3865
|
+
/** Provides a function that can be used to programmatically submit the `Form` */
|
|
3866
|
+
submit = () => {
|
|
3867
|
+
if (this.formElement.current) {
|
|
3868
|
+
const submitCustomEvent = new CustomEvent("submit", {
|
|
3869
|
+
cancelable: true
|
|
3870
|
+
});
|
|
3871
|
+
submitCustomEvent.preventDefault();
|
|
3872
|
+
this.formElement.current.dispatchEvent(submitCustomEvent);
|
|
3873
|
+
this.formElement.current.requestSubmit();
|
|
3874
|
+
}
|
|
3875
|
+
};
|
|
3928
3876
|
/** Attempts to focus on the field associated with the `error`. Uses the `property` field to compute path of the error
|
|
3929
3877
|
* field, then, using the `idPrefix` and `idSeparator` converts that path into an id. Then the input element with that
|
|
3930
3878
|
* id is attempted to be found using the `formElement` ref. If it is located, then it is focused.
|
|
@@ -3952,6 +3900,59 @@ var Form = class extends Component5 {
|
|
|
3952
3900
|
field.focus();
|
|
3953
3901
|
}
|
|
3954
3902
|
}
|
|
3903
|
+
/** Validates the form using the given `formData`. For use on form submission or on programmatic validation.
|
|
3904
|
+
* If `onError` is provided, then it will be called with the list of errors.
|
|
3905
|
+
*
|
|
3906
|
+
* @param formData - The form data to validate
|
|
3907
|
+
* @returns - True if the form is valid, false otherwise.
|
|
3908
|
+
*/
|
|
3909
|
+
validateFormWithFormData = (formData) => {
|
|
3910
|
+
const { extraErrors, extraErrorsBlockSubmit, focusOnFirstError, onError } = this.props;
|
|
3911
|
+
const { errors: prevErrors } = this.state;
|
|
3912
|
+
const schemaValidation = this.validate(formData);
|
|
3913
|
+
let errors = schemaValidation.errors;
|
|
3914
|
+
let errorSchema = schemaValidation.errorSchema;
|
|
3915
|
+
const schemaValidationErrors = errors;
|
|
3916
|
+
const schemaValidationErrorSchema = errorSchema;
|
|
3917
|
+
const hasError = errors.length > 0 || extraErrors && extraErrorsBlockSubmit;
|
|
3918
|
+
if (hasError) {
|
|
3919
|
+
if (extraErrors) {
|
|
3920
|
+
const merged = validationDataMerge(schemaValidation, extraErrors);
|
|
3921
|
+
errorSchema = merged.errorSchema;
|
|
3922
|
+
errors = merged.errors;
|
|
3923
|
+
}
|
|
3924
|
+
if (focusOnFirstError) {
|
|
3925
|
+
if (typeof focusOnFirstError === "function") {
|
|
3926
|
+
focusOnFirstError(errors[0]);
|
|
3927
|
+
} else {
|
|
3928
|
+
this.focusOnError(errors[0]);
|
|
3929
|
+
}
|
|
3930
|
+
}
|
|
3931
|
+
this.setState(
|
|
3932
|
+
{
|
|
3933
|
+
errors,
|
|
3934
|
+
errorSchema,
|
|
3935
|
+
schemaValidationErrors,
|
|
3936
|
+
schemaValidationErrorSchema
|
|
3937
|
+
},
|
|
3938
|
+
() => {
|
|
3939
|
+
if (onError) {
|
|
3940
|
+
onError(errors);
|
|
3941
|
+
} else {
|
|
3942
|
+
console.error("Form validation failed", errors);
|
|
3943
|
+
}
|
|
3944
|
+
}
|
|
3945
|
+
);
|
|
3946
|
+
} else if (prevErrors.length > 0) {
|
|
3947
|
+
this.setState({
|
|
3948
|
+
errors: [],
|
|
3949
|
+
errorSchema: {},
|
|
3950
|
+
schemaValidationErrors: [],
|
|
3951
|
+
schemaValidationErrorSchema: {}
|
|
3952
|
+
});
|
|
3953
|
+
}
|
|
3954
|
+
return !hasError;
|
|
3955
|
+
};
|
|
3955
3956
|
/** Programmatically validate the form. If `omitExtraData` is true, the `formData` will first be filtered to remove
|
|
3956
3957
|
* any extra data not in a form field. If `onError` is provided, then it will be called with the list of errors the
|
|
3957
3958
|
* same way as would happen on form submission.
|