@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,853 @@
|
|
|
1
|
+
import { Component, ElementType, FormEvent, ReactNode, Ref, RefObject, createRef } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
createSchemaUtils,
|
|
4
|
+
CustomValidator,
|
|
5
|
+
deepEquals,
|
|
6
|
+
ErrorSchema,
|
|
7
|
+
ErrorTransformer,
|
|
8
|
+
FormContextType,
|
|
9
|
+
GenericObjectType,
|
|
10
|
+
getTemplate,
|
|
11
|
+
getUiOptions,
|
|
12
|
+
IdSchema,
|
|
13
|
+
isObject,
|
|
14
|
+
mergeObjects,
|
|
15
|
+
NAME_KEY,
|
|
16
|
+
PathSchema,
|
|
17
|
+
StrictRJSFSchema,
|
|
18
|
+
Registry,
|
|
19
|
+
RegistryFieldsType,
|
|
20
|
+
RegistryWidgetsType,
|
|
21
|
+
RJSFSchema,
|
|
22
|
+
RJSFValidationError,
|
|
23
|
+
RJSF_ADDITONAL_PROPERTIES_FLAG,
|
|
24
|
+
SchemaUtilsType,
|
|
25
|
+
shouldRender,
|
|
26
|
+
SUBMIT_BTN_OPTIONS_KEY,
|
|
27
|
+
TemplatesType,
|
|
28
|
+
toErrorList,
|
|
29
|
+
UiSchema,
|
|
30
|
+
UI_GLOBAL_OPTIONS_KEY,
|
|
31
|
+
UI_OPTIONS_KEY,
|
|
32
|
+
ValidationData,
|
|
33
|
+
validationDataMerge,
|
|
34
|
+
ValidatorType,
|
|
35
|
+
Experimental_DefaultFormStateBehavior,
|
|
36
|
+
} from '@rjsf/utils';
|
|
37
|
+
import _get from 'lodash/get';
|
|
38
|
+
import _isEmpty from 'lodash/isEmpty';
|
|
39
|
+
import _pick from 'lodash/pick';
|
|
40
|
+
import _toPath from 'lodash/toPath';
|
|
41
|
+
|
|
42
|
+
import getDefaultRegistry from '../getDefaultRegistry';
|
|
43
|
+
|
|
44
|
+
/** The properties that are passed to the `Form` */
|
|
45
|
+
export interface FormProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> {
|
|
46
|
+
/** The JSON schema object for the form */
|
|
47
|
+
schema: S;
|
|
48
|
+
/** An implementation of the `ValidatorType` interface that is needed for form validation to work */
|
|
49
|
+
validator: ValidatorType<T, S, F>;
|
|
50
|
+
/** The optional children for the form, if provided, it will replace the default `SubmitButton` */
|
|
51
|
+
children?: ReactNode;
|
|
52
|
+
/** The uiSchema for the form */
|
|
53
|
+
uiSchema?: UiSchema<T, S, F>;
|
|
54
|
+
/** The data for the form, used to prefill a form with existing data */
|
|
55
|
+
formData?: T;
|
|
56
|
+
// Form presentation and behavior modifiers
|
|
57
|
+
/** You can provide a `formContext` object to the form, which is passed down to all fields and widgets. Useful for
|
|
58
|
+
* implementing context aware fields and widgets.
|
|
59
|
+
*
|
|
60
|
+
* NOTE: Setting `{readonlyAsDisabled: false}` on the formContext will make the antd theme treat readOnly fields as
|
|
61
|
+
* disabled.
|
|
62
|
+
*/
|
|
63
|
+
formContext?: F;
|
|
64
|
+
/** To avoid collisions with existing ids in the DOM, it is possible to change the prefix used for ids;
|
|
65
|
+
* Default is `root`
|
|
66
|
+
*/
|
|
67
|
+
idPrefix?: string;
|
|
68
|
+
/** To avoid using a path separator that is present in field names, it is possible to change the separator used for
|
|
69
|
+
* ids (Default is `_`)
|
|
70
|
+
*/
|
|
71
|
+
idSeparator?: string;
|
|
72
|
+
/** It's possible to disable the whole form by setting the `disabled` prop. The `disabled` prop is then forwarded down
|
|
73
|
+
* to each field of the form. If you just want to disable some fields, see the `ui:disabled` parameter in `uiSchema`
|
|
74
|
+
*/
|
|
75
|
+
disabled?: boolean;
|
|
76
|
+
/** It's possible to make the whole form read-only by setting the `readonly` prop. The `readonly` prop is then
|
|
77
|
+
* forwarded down to each field of the form. If you just want to make some fields read-only, see the `ui:readonly`
|
|
78
|
+
* parameter in `uiSchema`
|
|
79
|
+
*/
|
|
80
|
+
readonly?: boolean;
|
|
81
|
+
// Form registry
|
|
82
|
+
/** The dictionary of registered fields in the form */
|
|
83
|
+
fields?: RegistryFieldsType<T, S, F>;
|
|
84
|
+
/** The dictionary of registered templates in the form; Partial allows a subset to be provided beyond the defaults */
|
|
85
|
+
templates?: Partial<Omit<TemplatesType<T, S, F>, 'ButtonTemplates'>> & {
|
|
86
|
+
ButtonTemplates?: Partial<TemplatesType<T, S, F>['ButtonTemplates']>;
|
|
87
|
+
};
|
|
88
|
+
/** The dictionary of registered widgets in the form */
|
|
89
|
+
widgets?: RegistryWidgetsType<T, S, F>;
|
|
90
|
+
// Callbacks
|
|
91
|
+
/** If you plan on being notified every time the form data are updated, you can pass an `onChange` handler, which will
|
|
92
|
+
* receive the same args as `onSubmit` any time a value is updated in the form. Can also return the `id` of the field
|
|
93
|
+
* that caused the change
|
|
94
|
+
*/
|
|
95
|
+
onChange?: (data: IChangeEvent<T, S, F>, id?: string) => void;
|
|
96
|
+
/** To react when submitted form data are invalid, pass an `onError` handler. It will be passed the list of
|
|
97
|
+
* encountered errors
|
|
98
|
+
*/
|
|
99
|
+
onError?: (errors: RJSFValidationError[]) => void;
|
|
100
|
+
/** You can pass a function as the `onSubmit` prop of your `Form` component to listen to when the form is submitted
|
|
101
|
+
* and its data are valid. It will be passed a result object having a `formData` attribute, which is the valid form
|
|
102
|
+
* data you're usually after. The original event will also be passed as a second parameter
|
|
103
|
+
*/
|
|
104
|
+
onSubmit?: (data: IChangeEvent<T, S, F>, event: FormEvent<any>) => void;
|
|
105
|
+
/** Sometimes you may want to trigger events or modify external state when a field has been touched, so you can pass
|
|
106
|
+
* an `onBlur` handler, which will receive the id of the input that was blurred and the field value
|
|
107
|
+
*/
|
|
108
|
+
onBlur?: (id: string, data: any) => void;
|
|
109
|
+
/** Sometimes you may want to trigger events or modify external state when a field has been focused, so you can pass
|
|
110
|
+
* an `onFocus` handler, which will receive the id of the input that is focused and the field value
|
|
111
|
+
*/
|
|
112
|
+
onFocus?: (id: string, data: any) => void;
|
|
113
|
+
// <form /> HTML attributes
|
|
114
|
+
/** The value of this prop will be passed to the `accept-charset` HTML attribute on the form */
|
|
115
|
+
acceptcharset?: string;
|
|
116
|
+
/** The value of this prop will be passed to the `action` HTML attribute on the form
|
|
117
|
+
*
|
|
118
|
+
* NOTE: this just renders the `action` attribute in the HTML markup. There is no real network request being sent to
|
|
119
|
+
* this `action` on submit. Instead, react-jsonschema-form catches the submit event with `event.preventDefault()`
|
|
120
|
+
* and then calls the `onSubmit` function, where you could send a request programmatically with `fetch` or similar.
|
|
121
|
+
*/
|
|
122
|
+
action?: string;
|
|
123
|
+
/** The value of this prop will be passed to the `autocomplete` HTML attribute on the form */
|
|
124
|
+
autoComplete?: string;
|
|
125
|
+
/** The value of this prop will be passed to the `class` HTML attribute on the form */
|
|
126
|
+
className?: string;
|
|
127
|
+
/** The value of this prop will be passed to the `enctype` HTML attribute on the form */
|
|
128
|
+
enctype?: string;
|
|
129
|
+
/** The value of this prop will be passed to the `id` HTML attribute on the form */
|
|
130
|
+
id?: string;
|
|
131
|
+
/** The value of this prop will be passed to the `name` HTML attribute on the form */
|
|
132
|
+
name?: string;
|
|
133
|
+
/** The value of this prop will be passed to the `method` HTML attribute on the form */
|
|
134
|
+
method?: string;
|
|
135
|
+
/** It's possible to change the default `form` tag name to a different HTML tag, which can be helpful if you are
|
|
136
|
+
* nesting forms. However, native browser form behaviour, such as submitting when the `Enter` key is pressed, may no
|
|
137
|
+
* longer work
|
|
138
|
+
*/
|
|
139
|
+
tagName?: ElementType;
|
|
140
|
+
/** The value of this prop will be passed to the `target` HTML attribute on the form */
|
|
141
|
+
target?: string;
|
|
142
|
+
// Errors and validation
|
|
143
|
+
/** Formerly the `validate` prop; Takes a function that specifies custom validation rules for the form */
|
|
144
|
+
customValidate?: CustomValidator<T, S, F>;
|
|
145
|
+
/** This prop allows passing in custom errors that are augmented with the existing JSON Schema errors on the form; it
|
|
146
|
+
* can be used to implement asynchronous validation. By default, these are non-blocking errors, meaning that you can
|
|
147
|
+
* still submit the form when these are the only errors displayed to the user.
|
|
148
|
+
*/
|
|
149
|
+
extraErrors?: ErrorSchema<T>;
|
|
150
|
+
/** If set to true, causes the `extraErrors` to become blocking when the form is submitted */
|
|
151
|
+
extraErrorsBlockSubmit?: boolean;
|
|
152
|
+
/** If set to true, turns off HTML5 validation on the form; Set to `false` by default */
|
|
153
|
+
noHtml5Validate?: boolean;
|
|
154
|
+
/** If set to true, turns off all validation. Set to `false` by default
|
|
155
|
+
*
|
|
156
|
+
* @deprecated - In a future release, this switch may be replaced by making `validator` prop optional
|
|
157
|
+
*/
|
|
158
|
+
noValidate?: boolean;
|
|
159
|
+
/** If set to true, the form will perform validation and show any validation errors whenever the form data is changed,
|
|
160
|
+
* rather than just on submit
|
|
161
|
+
*/
|
|
162
|
+
liveValidate?: boolean;
|
|
163
|
+
/** If `omitExtraData` and `liveOmit` are both set to true, then extra form data values that are not in any form field
|
|
164
|
+
* will be removed whenever `onChange` is called. Set to `false` by default
|
|
165
|
+
*/
|
|
166
|
+
liveOmit?: boolean;
|
|
167
|
+
/** If set to true, then extra form data values that are not in any form field will be removed whenever `onSubmit` is
|
|
168
|
+
* called. Set to `false` by default.
|
|
169
|
+
*/
|
|
170
|
+
omitExtraData?: boolean;
|
|
171
|
+
/** When this prop is set to `top` or 'bottom', a list of errors (or the custom error list defined in the `ErrorList`) will also
|
|
172
|
+
* show. When set to false, only inline input validation errors will be shown. Set to `top` by default
|
|
173
|
+
*/
|
|
174
|
+
showErrorList?: false | 'top' | 'bottom';
|
|
175
|
+
/** A function can be passed to this prop in order to make modifications to the default errors resulting from JSON
|
|
176
|
+
* Schema validation
|
|
177
|
+
*/
|
|
178
|
+
transformErrors?: ErrorTransformer<T, S, F>;
|
|
179
|
+
/** If set to true, then the first field with an error will receive the focus when the form is submitted with errors
|
|
180
|
+
*/
|
|
181
|
+
focusOnFirstError?: boolean | ((error: RJSFValidationError) => void);
|
|
182
|
+
/** Optional string translation function, if provided, allows users to change the translation of the RJSF internal
|
|
183
|
+
* strings. Some strings contain replaceable parameter values as indicated by `%1`, `%2`, etc. The number after the
|
|
184
|
+
* `%` indicates the order of the parameter. The ordering of parameters is important because some languages may choose
|
|
185
|
+
* to put the second parameter before the first in its translation.
|
|
186
|
+
*/
|
|
187
|
+
translateString?: Registry['translateString'];
|
|
188
|
+
/** Optional configuration object with flags, if provided, allows users to override default form state behavior
|
|
189
|
+
* Currently only affecting minItems on array fields and handling of setting defaults based on the value of
|
|
190
|
+
* `emptyObjectFields`
|
|
191
|
+
*/
|
|
192
|
+
experimental_defaultFormStateBehavior?: Experimental_DefaultFormStateBehavior;
|
|
193
|
+
// Private
|
|
194
|
+
/**
|
|
195
|
+
* _internalFormWrapper is currently used by the semantic-ui theme to provide a custom wrapper around `<Form />`
|
|
196
|
+
* that supports the proper rendering of those themes. To use this prop, one must pass a component that takes two
|
|
197
|
+
* props: `children` and `as`. That component, at minimum, should render the `children` inside of a <form /> tag
|
|
198
|
+
* unless `as` is provided, in which case, use the `as` prop in place of `<form />`.
|
|
199
|
+
* i.e.:
|
|
200
|
+
* ```
|
|
201
|
+
* export default function InternalForm({ children, as }) {
|
|
202
|
+
* const FormTag = as || 'form';
|
|
203
|
+
* return <FormTag>{children}</FormTag>;
|
|
204
|
+
* }
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* Use at your own risk as this prop is private and may change at any time without notice.
|
|
208
|
+
*/
|
|
209
|
+
_internalFormWrapper?: ElementType;
|
|
210
|
+
/** Support receiving a React ref to the Form
|
|
211
|
+
*/
|
|
212
|
+
ref?: Ref<Form<T, S, F>>;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The data that is contained within the state for the `Form` */
|
|
216
|
+
export interface FormState<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> {
|
|
217
|
+
/** The JSON schema object for the form */
|
|
218
|
+
schema: S;
|
|
219
|
+
/** The uiSchema for the form */
|
|
220
|
+
uiSchema: UiSchema<T, S, F>;
|
|
221
|
+
/** The `IdSchema` for the form, computed from the `schema`, the `rootFieldId`, the `formData` and the `idPrefix` and
|
|
222
|
+
* `idSeparator` props.
|
|
223
|
+
*/
|
|
224
|
+
idSchema: IdSchema<T>;
|
|
225
|
+
/** The schemaUtils implementation used by the `Form`, created from the `validator` and the `schema` */
|
|
226
|
+
schemaUtils: SchemaUtilsType<T, S, F>;
|
|
227
|
+
/** The current data for the form, computed from the `formData` prop and the changes made by the user */
|
|
228
|
+
formData?: T;
|
|
229
|
+
/** Flag indicating whether the form is in edit mode, true when `formData` is passed to the form, otherwise false */
|
|
230
|
+
edit: boolean;
|
|
231
|
+
/** The current list of errors for the form, includes `extraErrors` */
|
|
232
|
+
errors: RJSFValidationError[];
|
|
233
|
+
/** The current errors, in `ErrorSchema` format, for the form, includes `extraErrors` */
|
|
234
|
+
errorSchema: ErrorSchema<T>;
|
|
235
|
+
/** The current list of errors for the form directly from schema validation, does NOT include `extraErrors` */
|
|
236
|
+
schemaValidationErrors: RJSFValidationError[];
|
|
237
|
+
/** The current errors, in `ErrorSchema` format, for the form directly from schema validation, does NOT include
|
|
238
|
+
* `extraErrors`
|
|
239
|
+
*/
|
|
240
|
+
schemaValidationErrorSchema: ErrorSchema<T>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** The event data passed when changes have been made to the form, includes everything from the `FormState` except
|
|
244
|
+
* the schema validation errors. An additional `status` is added when returned from `onSubmit`
|
|
245
|
+
*/
|
|
246
|
+
export interface IChangeEvent<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>
|
|
247
|
+
extends Omit<FormState<T, S, F>, 'schemaValidationErrors' | 'schemaValidationErrorSchema'> {
|
|
248
|
+
/** The status of the form when submitted */
|
|
249
|
+
status?: 'submitted';
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** The `Form` component renders the outer form and all the fields defined in the `schema` */
|
|
253
|
+
export default class Form<
|
|
254
|
+
T = any,
|
|
255
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
256
|
+
F extends FormContextType = any
|
|
257
|
+
> extends Component<FormProps<T, S, F>, FormState<T, S, F>> {
|
|
258
|
+
/** The ref used to hold the `form` element, this needs to be `any` because `tagName` or `_internalFormWrapper` can
|
|
259
|
+
* provide any possible type here
|
|
260
|
+
*/
|
|
261
|
+
formElement: RefObject<any>;
|
|
262
|
+
|
|
263
|
+
/** Constructs the `Form` from the `props`. Will setup the initial state from the props. It will also call the
|
|
264
|
+
* `onChange` handler if the initially provided `formData` is modified to add missing default values as part of the
|
|
265
|
+
* state construction.
|
|
266
|
+
*
|
|
267
|
+
* @param props - The initial props for the `Form`
|
|
268
|
+
*/
|
|
269
|
+
constructor(props: FormProps<T, S, F>) {
|
|
270
|
+
super(props);
|
|
271
|
+
|
|
272
|
+
if (!props.validator) {
|
|
273
|
+
throw new Error('A validator is required for Form functionality to work');
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
this.state = this.getStateFromProps(props, props.formData);
|
|
277
|
+
if (this.props.onChange && !deepEquals(this.state.formData, this.props.formData)) {
|
|
278
|
+
this.props.onChange(this.state);
|
|
279
|
+
}
|
|
280
|
+
this.formElement = createRef();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** React lifecycle method that gets called before new props are provided, updates the state based on new props. It
|
|
284
|
+
* will also call the`onChange` handler if the `formData` is modified to add missing default values as part of the
|
|
285
|
+
* state construction.
|
|
286
|
+
*
|
|
287
|
+
* @param nextProps - The new set of props about to be applied to the `Form`
|
|
288
|
+
*/
|
|
289
|
+
UNSAFE_componentWillReceiveProps(nextProps: FormProps<T, S, F>) {
|
|
290
|
+
const nextState = this.getStateFromProps(nextProps, nextProps.formData);
|
|
291
|
+
if (
|
|
292
|
+
!deepEquals(nextState.formData, nextProps.formData) &&
|
|
293
|
+
!deepEquals(nextState.formData, this.state.formData) &&
|
|
294
|
+
nextProps.onChange
|
|
295
|
+
) {
|
|
296
|
+
nextProps.onChange(nextState);
|
|
297
|
+
}
|
|
298
|
+
this.setState(nextState);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Extracts the updated state from the given `props` and `inputFormData`. As part of this process, the
|
|
302
|
+
* `inputFormData` is first processed to add any missing required defaults. After that, the data is run through the
|
|
303
|
+
* validation process IF required by the `props`.
|
|
304
|
+
*
|
|
305
|
+
* @param props - The props passed to the `Form`
|
|
306
|
+
* @param inputFormData - The new or current data for the `Form`
|
|
307
|
+
* @returns - The new state for the `Form`
|
|
308
|
+
*/
|
|
309
|
+
getStateFromProps(props: FormProps<T, S, F>, inputFormData?: T): FormState<T, S, F> {
|
|
310
|
+
const state: FormState<T, S, F> = this.state || {};
|
|
311
|
+
const schema = 'schema' in props ? props.schema : this.props.schema;
|
|
312
|
+
const uiSchema: UiSchema<T, S, F> = ('uiSchema' in props ? props.uiSchema! : this.props.uiSchema!) || {};
|
|
313
|
+
const edit = typeof inputFormData !== 'undefined';
|
|
314
|
+
const liveValidate = 'liveValidate' in props ? props.liveValidate : this.props.liveValidate;
|
|
315
|
+
const mustValidate = edit && !props.noValidate && liveValidate;
|
|
316
|
+
const rootSchema = schema;
|
|
317
|
+
const experimental_defaultFormStateBehavior =
|
|
318
|
+
'experimental_defaultFormStateBehavior' in props
|
|
319
|
+
? props.experimental_defaultFormStateBehavior
|
|
320
|
+
: this.props.experimental_defaultFormStateBehavior;
|
|
321
|
+
let schemaUtils: SchemaUtilsType<T, S, F> = state.schemaUtils;
|
|
322
|
+
if (
|
|
323
|
+
!schemaUtils ||
|
|
324
|
+
schemaUtils.doesSchemaUtilsDiffer(props.validator, rootSchema, experimental_defaultFormStateBehavior)
|
|
325
|
+
) {
|
|
326
|
+
schemaUtils = createSchemaUtils<T, S, F>(props.validator, rootSchema, experimental_defaultFormStateBehavior);
|
|
327
|
+
}
|
|
328
|
+
const formData: T = schemaUtils.getDefaultFormState(schema, inputFormData) as T;
|
|
329
|
+
const retrievedSchema = schemaUtils.retrieveSchema(schema, formData);
|
|
330
|
+
|
|
331
|
+
const getCurrentErrors = (): ValidationData<T> => {
|
|
332
|
+
if (props.noValidate) {
|
|
333
|
+
return { errors: [], errorSchema: {} };
|
|
334
|
+
} else if (!props.liveValidate) {
|
|
335
|
+
return {
|
|
336
|
+
errors: state.schemaValidationErrors || [],
|
|
337
|
+
errorSchema: state.schemaValidationErrorSchema || {},
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
return {
|
|
341
|
+
errors: state.errors || [],
|
|
342
|
+
errorSchema: state.errorSchema || {},
|
|
343
|
+
};
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
let errors: RJSFValidationError[];
|
|
347
|
+
let errorSchema: ErrorSchema<T> | undefined;
|
|
348
|
+
let schemaValidationErrors: RJSFValidationError[] = state.schemaValidationErrors;
|
|
349
|
+
let schemaValidationErrorSchema: ErrorSchema<T> = state.schemaValidationErrorSchema;
|
|
350
|
+
if (mustValidate) {
|
|
351
|
+
const schemaValidation = this.validate(formData, schema, schemaUtils);
|
|
352
|
+
errors = schemaValidation.errors;
|
|
353
|
+
errorSchema = schemaValidation.errorSchema;
|
|
354
|
+
schemaValidationErrors = errors;
|
|
355
|
+
schemaValidationErrorSchema = errorSchema;
|
|
356
|
+
} else {
|
|
357
|
+
const currentErrors = getCurrentErrors();
|
|
358
|
+
errors = currentErrors.errors;
|
|
359
|
+
errorSchema = currentErrors.errorSchema;
|
|
360
|
+
}
|
|
361
|
+
if (props.extraErrors) {
|
|
362
|
+
const merged = validationDataMerge({ errorSchema, errors }, props.extraErrors);
|
|
363
|
+
errorSchema = merged.errorSchema;
|
|
364
|
+
errors = merged.errors;
|
|
365
|
+
}
|
|
366
|
+
const idSchema = schemaUtils.toIdSchema(
|
|
367
|
+
retrievedSchema,
|
|
368
|
+
uiSchema['ui:rootFieldId'],
|
|
369
|
+
formData,
|
|
370
|
+
props.idPrefix,
|
|
371
|
+
props.idSeparator
|
|
372
|
+
);
|
|
373
|
+
const nextState: FormState<T, S, F> = {
|
|
374
|
+
schemaUtils,
|
|
375
|
+
schema,
|
|
376
|
+
uiSchema,
|
|
377
|
+
idSchema,
|
|
378
|
+
formData,
|
|
379
|
+
edit,
|
|
380
|
+
errors,
|
|
381
|
+
errorSchema,
|
|
382
|
+
schemaValidationErrors,
|
|
383
|
+
schemaValidationErrorSchema,
|
|
384
|
+
};
|
|
385
|
+
return nextState;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** React lifecycle method that is used to determine whether component should be updated.
|
|
389
|
+
*
|
|
390
|
+
* @param nextProps - The next version of the props
|
|
391
|
+
* @param nextState - The next version of the state
|
|
392
|
+
* @returns - True if the component should be updated, false otherwise
|
|
393
|
+
*/
|
|
394
|
+
shouldComponentUpdate(nextProps: FormProps<T, S, F>, nextState: FormState<T, S, F>): boolean {
|
|
395
|
+
return shouldRender(this, nextProps, nextState);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/** Validates the `formData` against the `schema` using the `altSchemaUtils` (if provided otherwise it uses the
|
|
399
|
+
* `schemaUtils` in the state), returning the results.
|
|
400
|
+
*
|
|
401
|
+
* @param formData - The new form data to validate
|
|
402
|
+
* @param schema - The schema used to validate against
|
|
403
|
+
* @param altSchemaUtils - The alternate schemaUtils to use for validation
|
|
404
|
+
*/
|
|
405
|
+
validate(
|
|
406
|
+
formData: T | undefined,
|
|
407
|
+
schema = this.props.schema,
|
|
408
|
+
altSchemaUtils?: SchemaUtilsType<T, S, F>
|
|
409
|
+
): ValidationData<T> {
|
|
410
|
+
const schemaUtils = altSchemaUtils ? altSchemaUtils : this.state.schemaUtils;
|
|
411
|
+
const { customValidate, transformErrors, uiSchema } = this.props;
|
|
412
|
+
const resolvedSchema = schemaUtils.retrieveSchema(schema, formData);
|
|
413
|
+
return schemaUtils
|
|
414
|
+
.getValidator()
|
|
415
|
+
.validateFormData(formData, resolvedSchema, customValidate, transformErrors, uiSchema);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** Renders any errors contained in the `state` in using the `ErrorList`, if not disabled by `showErrorList`. */
|
|
419
|
+
renderErrors(registry: Registry<T, S, F>) {
|
|
420
|
+
const { errors, errorSchema, schema, uiSchema } = this.state;
|
|
421
|
+
const { formContext } = this.props;
|
|
422
|
+
const options = getUiOptions<T, S, F>(uiSchema);
|
|
423
|
+
const ErrorListTemplate = getTemplate<'ErrorListTemplate', T, S, F>('ErrorListTemplate', registry, options);
|
|
424
|
+
|
|
425
|
+
if (errors && errors.length) {
|
|
426
|
+
return (
|
|
427
|
+
<ErrorListTemplate
|
|
428
|
+
errors={errors}
|
|
429
|
+
errorSchema={errorSchema || {}}
|
|
430
|
+
schema={schema}
|
|
431
|
+
uiSchema={uiSchema}
|
|
432
|
+
formContext={formContext}
|
|
433
|
+
registry={registry}
|
|
434
|
+
/>
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** Returns the `formData` with only the elements specified in the `fields` list
|
|
441
|
+
*
|
|
442
|
+
* @param formData - The data for the `Form`
|
|
443
|
+
* @param fields - The fields to keep while filtering
|
|
444
|
+
*/
|
|
445
|
+
getUsedFormData = (formData: T | undefined, fields: string[][]): T | undefined => {
|
|
446
|
+
// For the case of a single input form
|
|
447
|
+
if (fields.length === 0 && typeof formData !== 'object') {
|
|
448
|
+
return formData;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// _pick has incorrect type definition, it works with string[][], because lodash/hasIn supports it
|
|
452
|
+
const data: GenericObjectType = _pick(formData, fields as unknown as string[]);
|
|
453
|
+
if (Array.isArray(formData)) {
|
|
454
|
+
return Object.keys(data).map((key: string) => data[key]) as unknown as T;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return data as T;
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
/** Returns the list of field names from inspecting the `pathSchema` as well as using the `formData`
|
|
461
|
+
*
|
|
462
|
+
* @param pathSchema - The `PathSchema` object for the form
|
|
463
|
+
* @param [formData] - The form data to use while checking for empty objects/arrays
|
|
464
|
+
*/
|
|
465
|
+
getFieldNames = (pathSchema: PathSchema<T>, formData?: T): string[][] => {
|
|
466
|
+
const getAllPaths = (_obj: GenericObjectType, acc: string[][] = [], paths: string[][] = [[]]) => {
|
|
467
|
+
Object.keys(_obj).forEach((key: string) => {
|
|
468
|
+
if (typeof _obj[key] === 'object') {
|
|
469
|
+
const newPaths = paths.map((path) => [...path, key]);
|
|
470
|
+
// If an object is marked with additionalProperties, all its keys are valid
|
|
471
|
+
if (_obj[key][RJSF_ADDITONAL_PROPERTIES_FLAG] && _obj[key][NAME_KEY] !== '') {
|
|
472
|
+
acc.push(_obj[key][NAME_KEY]);
|
|
473
|
+
} else {
|
|
474
|
+
getAllPaths(_obj[key], acc, newPaths);
|
|
475
|
+
}
|
|
476
|
+
} else if (key === NAME_KEY && _obj[key] !== '') {
|
|
477
|
+
paths.forEach((path) => {
|
|
478
|
+
const formValue = _get(formData, path);
|
|
479
|
+
// adds path to fieldNames if it points to a value
|
|
480
|
+
// or an empty object/array
|
|
481
|
+
if (typeof formValue !== 'object' || _isEmpty(formValue)) {
|
|
482
|
+
acc.push(path);
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
return acc;
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
return getAllPaths(pathSchema);
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
/** Function to handle changes made to a field in the `Form`. This handler receives an entirely new copy of the
|
|
494
|
+
* `formData` along with a new `ErrorSchema`. It will first update the `formData` with any missing default fields and
|
|
495
|
+
* then, if `omitExtraData` and `liveOmit` are turned on, the `formData` will be filterer to remove any extra data not
|
|
496
|
+
* in a form field. Then, the resulting formData will be validated if required. The state will be updated with the new
|
|
497
|
+
* updated (potentially filtered) `formData`, any errors that resulted from validation. Finally the `onChange`
|
|
498
|
+
* callback will be called if specified with the updated state.
|
|
499
|
+
*
|
|
500
|
+
* @param formData - The new form data from a change to a field
|
|
501
|
+
* @param newErrorSchema - The new `ErrorSchema` based on the field change
|
|
502
|
+
* @param id - The id of the field that caused the change
|
|
503
|
+
*/
|
|
504
|
+
onChange = (formData: T | undefined, newErrorSchema?: ErrorSchema<T>, id?: string) => {
|
|
505
|
+
const { extraErrors, omitExtraData, liveOmit, noValidate, liveValidate, onChange } = this.props;
|
|
506
|
+
const { schemaUtils, schema } = this.state;
|
|
507
|
+
if (isObject(formData) || Array.isArray(formData)) {
|
|
508
|
+
const newState = this.getStateFromProps(this.props, formData);
|
|
509
|
+
formData = newState.formData;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
const mustValidate = !noValidate && liveValidate;
|
|
513
|
+
let state: Partial<FormState<T, S, F>> = { formData, schema };
|
|
514
|
+
let newFormData = formData;
|
|
515
|
+
|
|
516
|
+
if (omitExtraData === true && liveOmit === true) {
|
|
517
|
+
const retrievedSchema = schemaUtils.retrieveSchema(schema, formData);
|
|
518
|
+
const pathSchema = schemaUtils.toPathSchema(retrievedSchema, '', formData);
|
|
519
|
+
|
|
520
|
+
const fieldNames = this.getFieldNames(pathSchema, formData);
|
|
521
|
+
|
|
522
|
+
newFormData = this.getUsedFormData(formData, fieldNames);
|
|
523
|
+
state = {
|
|
524
|
+
formData: newFormData,
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if (mustValidate) {
|
|
529
|
+
const schemaValidation = this.validate(newFormData);
|
|
530
|
+
let errors = schemaValidation.errors;
|
|
531
|
+
let errorSchema = schemaValidation.errorSchema;
|
|
532
|
+
const schemaValidationErrors = errors;
|
|
533
|
+
const schemaValidationErrorSchema = errorSchema;
|
|
534
|
+
if (extraErrors) {
|
|
535
|
+
const merged = validationDataMerge(schemaValidation, extraErrors);
|
|
536
|
+
errorSchema = merged.errorSchema;
|
|
537
|
+
errors = merged.errors;
|
|
538
|
+
}
|
|
539
|
+
state = {
|
|
540
|
+
formData: newFormData,
|
|
541
|
+
errors,
|
|
542
|
+
errorSchema,
|
|
543
|
+
schemaValidationErrors,
|
|
544
|
+
schemaValidationErrorSchema,
|
|
545
|
+
};
|
|
546
|
+
} else if (!noValidate && newErrorSchema) {
|
|
547
|
+
const errorSchema = extraErrors
|
|
548
|
+
? (mergeObjects(newErrorSchema, extraErrors, 'preventDuplicates') as ErrorSchema<T>)
|
|
549
|
+
: newErrorSchema;
|
|
550
|
+
state = {
|
|
551
|
+
formData: newFormData,
|
|
552
|
+
errorSchema: errorSchema,
|
|
553
|
+
errors: toErrorList(errorSchema),
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
this.setState(state as FormState<T, S, F>, () => onChange && onChange({ ...this.state, ...state }, id));
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Callback function to handle reset form data.
|
|
561
|
+
* - Reset all fields with default values.
|
|
562
|
+
* - Reset validations and errors
|
|
563
|
+
*
|
|
564
|
+
*/
|
|
565
|
+
reset = () => {
|
|
566
|
+
const { onChange } = this.props;
|
|
567
|
+
const newState = this.getStateFromProps(this.props, undefined);
|
|
568
|
+
const newFormData = newState.formData;
|
|
569
|
+
const state = {
|
|
570
|
+
formData: newFormData,
|
|
571
|
+
errorSchema: {},
|
|
572
|
+
errors: [] as unknown,
|
|
573
|
+
schemaValidationErrors: [] as unknown,
|
|
574
|
+
schemaValidationErrorSchema: {},
|
|
575
|
+
} as FormState<T, S, F>;
|
|
576
|
+
|
|
577
|
+
this.setState(state, () => onChange && onChange({ ...this.state, ...state }));
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
/** Callback function to handle when a field on the form is blurred. Calls the `onBlur` callback for the `Form` if it
|
|
581
|
+
* was provided.
|
|
582
|
+
*
|
|
583
|
+
* @param id - The unique `id` of the field that was blurred
|
|
584
|
+
* @param data - The data associated with the field that was blurred
|
|
585
|
+
*/
|
|
586
|
+
onBlur = (id: string, data: any) => {
|
|
587
|
+
const { onBlur } = this.props;
|
|
588
|
+
if (onBlur) {
|
|
589
|
+
onBlur(id, data);
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
/** Callback function to handle when a field on the form is focused. Calls the `onFocus` callback for the `Form` if it
|
|
594
|
+
* was provided.
|
|
595
|
+
*
|
|
596
|
+
* @param id - The unique `id` of the field that was focused
|
|
597
|
+
* @param data - The data associated with the field that was focused
|
|
598
|
+
*/
|
|
599
|
+
onFocus = (id: string, data: any) => {
|
|
600
|
+
const { onFocus } = this.props;
|
|
601
|
+
if (onFocus) {
|
|
602
|
+
onFocus(id, data);
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
/** Callback function to handle when the form is submitted. First, it prevents the default event behavior. Nothing
|
|
607
|
+
* happens if the target and currentTarget of the event are not the same. It will omit any extra data in the
|
|
608
|
+
* `formData` in the state if `omitExtraData` is true. It will validate the resulting `formData`, reporting errors
|
|
609
|
+
* via the `onError()` callback unless validation is disabled. Finally, it will add in any `extraErrors` and then call
|
|
610
|
+
* back the `onSubmit` callback if it was provided.
|
|
611
|
+
*
|
|
612
|
+
* @param event - The submit HTML form event
|
|
613
|
+
*/
|
|
614
|
+
onSubmit = (event: FormEvent<any>) => {
|
|
615
|
+
event.preventDefault();
|
|
616
|
+
if (event.target !== event.currentTarget) {
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
event.persist();
|
|
621
|
+
const { omitExtraData, extraErrors, noValidate, onSubmit } = this.props;
|
|
622
|
+
let { formData: newFormData } = this.state;
|
|
623
|
+
const { schema, schemaUtils } = this.state;
|
|
624
|
+
|
|
625
|
+
if (omitExtraData === true) {
|
|
626
|
+
const retrievedSchema = schemaUtils.retrieveSchema(schema, newFormData);
|
|
627
|
+
const pathSchema = schemaUtils.toPathSchema(retrievedSchema, '', newFormData);
|
|
628
|
+
|
|
629
|
+
const fieldNames = this.getFieldNames(pathSchema, newFormData);
|
|
630
|
+
|
|
631
|
+
newFormData = this.getUsedFormData(newFormData, fieldNames);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if (noValidate || this.validateForm()) {
|
|
635
|
+
// There are no errors generated through schema validation.
|
|
636
|
+
// Check for user provided errors and update state accordingly.
|
|
637
|
+
const errorSchema = extraErrors || {};
|
|
638
|
+
const errors = extraErrors ? toErrorList(extraErrors) : [];
|
|
639
|
+
this.setState(
|
|
640
|
+
{
|
|
641
|
+
formData: newFormData,
|
|
642
|
+
errors,
|
|
643
|
+
errorSchema,
|
|
644
|
+
schemaValidationErrors: [],
|
|
645
|
+
schemaValidationErrorSchema: {},
|
|
646
|
+
},
|
|
647
|
+
() => {
|
|
648
|
+
if (onSubmit) {
|
|
649
|
+
onSubmit({ ...this.state, formData: newFormData, status: 'submitted' }, event);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
/** Returns the registry for the form */
|
|
657
|
+
getRegistry(): Registry<T, S, F> {
|
|
658
|
+
const { translateString: customTranslateString, uiSchema = {} } = this.props;
|
|
659
|
+
const { schemaUtils } = this.state;
|
|
660
|
+
const { fields, templates, widgets, formContext, translateString } = getDefaultRegistry<T, S, F>();
|
|
661
|
+
return {
|
|
662
|
+
fields: { ...fields, ...this.props.fields },
|
|
663
|
+
templates: {
|
|
664
|
+
...templates,
|
|
665
|
+
...this.props.templates,
|
|
666
|
+
ButtonTemplates: {
|
|
667
|
+
...templates.ButtonTemplates,
|
|
668
|
+
...this.props.templates?.ButtonTemplates,
|
|
669
|
+
},
|
|
670
|
+
},
|
|
671
|
+
widgets: { ...widgets, ...this.props.widgets },
|
|
672
|
+
rootSchema: this.props.schema,
|
|
673
|
+
formContext: this.props.formContext || formContext,
|
|
674
|
+
schemaUtils,
|
|
675
|
+
translateString: customTranslateString || translateString,
|
|
676
|
+
globalUiOptions: uiSchema[UI_GLOBAL_OPTIONS_KEY],
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/** Provides a function that can be used to programmatically submit the `Form` */
|
|
681
|
+
submit() {
|
|
682
|
+
if (this.formElement.current) {
|
|
683
|
+
this.formElement.current.dispatchEvent(
|
|
684
|
+
new CustomEvent('submit', {
|
|
685
|
+
cancelable: true,
|
|
686
|
+
})
|
|
687
|
+
);
|
|
688
|
+
this.formElement.current.requestSubmit();
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/** Attempts to focus on the field associated with the `error`. Uses the `property` field to compute path of the error
|
|
693
|
+
* field, then, using the `idPrefix` and `idSeparator` converts that path into an id. Then the input element with that
|
|
694
|
+
* id is attempted to be found using the `formElement` ref. If it is located, then it is focused.
|
|
695
|
+
*
|
|
696
|
+
* @param error - The error on which to focus
|
|
697
|
+
*/
|
|
698
|
+
focusOnError(error: RJSFValidationError) {
|
|
699
|
+
const { idPrefix = 'root', idSeparator = '_' } = this.props;
|
|
700
|
+
const { property } = error;
|
|
701
|
+
const path = _toPath(property);
|
|
702
|
+
if (path[0] === '') {
|
|
703
|
+
// Most of the time the `.foo` property results in the first element being empty, so replace it with the idPrefix
|
|
704
|
+
path[0] = idPrefix;
|
|
705
|
+
} else {
|
|
706
|
+
// Otherwise insert the idPrefix into the first location using unshift
|
|
707
|
+
path.unshift(idPrefix);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
const elementId = path.join(idSeparator);
|
|
711
|
+
let field = this.formElement.current.elements[elementId];
|
|
712
|
+
if (!field) {
|
|
713
|
+
// if not an exact match, try finding an input starting with the element id (like radio buttons or checkboxes)
|
|
714
|
+
field = this.formElement.current.querySelector(`input[id^=${elementId}`);
|
|
715
|
+
}
|
|
716
|
+
if (field && field.length) {
|
|
717
|
+
// If we got a list with length > 0
|
|
718
|
+
field = field[0];
|
|
719
|
+
}
|
|
720
|
+
if (field) {
|
|
721
|
+
field.focus();
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/** Programmatically validate the form. If `onError` is provided, then it will be called with the list of errors the
|
|
726
|
+
* same way as would happen on form submission.
|
|
727
|
+
*
|
|
728
|
+
* @returns - True if the form is valid, false otherwise.
|
|
729
|
+
*/
|
|
730
|
+
validateForm() {
|
|
731
|
+
const { extraErrors, extraErrorsBlockSubmit, focusOnFirstError, onError } = this.props;
|
|
732
|
+
const { formData } = this.state;
|
|
733
|
+
const schemaValidation = this.validate(formData);
|
|
734
|
+
let errors = schemaValidation.errors;
|
|
735
|
+
let errorSchema = schemaValidation.errorSchema;
|
|
736
|
+
const schemaValidationErrors = errors;
|
|
737
|
+
const schemaValidationErrorSchema = errorSchema;
|
|
738
|
+
if (errors.length > 0 || (extraErrors && extraErrorsBlockSubmit)) {
|
|
739
|
+
if (extraErrors) {
|
|
740
|
+
const merged = validationDataMerge(schemaValidation, extraErrors);
|
|
741
|
+
errorSchema = merged.errorSchema;
|
|
742
|
+
errors = merged.errors;
|
|
743
|
+
}
|
|
744
|
+
if (focusOnFirstError) {
|
|
745
|
+
if (typeof focusOnFirstError === 'function') {
|
|
746
|
+
focusOnFirstError(errors[0]);
|
|
747
|
+
} else {
|
|
748
|
+
this.focusOnError(errors[0]);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
this.setState(
|
|
752
|
+
{
|
|
753
|
+
errors,
|
|
754
|
+
errorSchema,
|
|
755
|
+
schemaValidationErrors,
|
|
756
|
+
schemaValidationErrorSchema,
|
|
757
|
+
},
|
|
758
|
+
() => {
|
|
759
|
+
if (onError) {
|
|
760
|
+
onError(errors);
|
|
761
|
+
} else {
|
|
762
|
+
console.error('Form validation failed', errors);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
);
|
|
766
|
+
return false;
|
|
767
|
+
}
|
|
768
|
+
return true;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
/** Renders the `Form` fields inside the <form> | `tagName` or `_internalFormWrapper`, rendering any errors if
|
|
772
|
+
* needed along with the submit button or any children of the form.
|
|
773
|
+
*/
|
|
774
|
+
render() {
|
|
775
|
+
const {
|
|
776
|
+
children,
|
|
777
|
+
id,
|
|
778
|
+
idPrefix,
|
|
779
|
+
idSeparator,
|
|
780
|
+
className = '',
|
|
781
|
+
tagName,
|
|
782
|
+
name,
|
|
783
|
+
method,
|
|
784
|
+
target,
|
|
785
|
+
action,
|
|
786
|
+
autoComplete,
|
|
787
|
+
enctype,
|
|
788
|
+
acceptcharset,
|
|
789
|
+
noHtml5Validate = false,
|
|
790
|
+
disabled = false,
|
|
791
|
+
readonly = false,
|
|
792
|
+
formContext,
|
|
793
|
+
showErrorList = 'top',
|
|
794
|
+
_internalFormWrapper,
|
|
795
|
+
} = this.props;
|
|
796
|
+
|
|
797
|
+
const { schema, uiSchema, formData, errorSchema, idSchema } = this.state;
|
|
798
|
+
const registry = this.getRegistry();
|
|
799
|
+
const { SchemaField: _SchemaField } = registry.fields;
|
|
800
|
+
const { SubmitButton } = registry.templates.ButtonTemplates;
|
|
801
|
+
// The `semantic-ui` and `material-ui` themes have `_internalFormWrapper`s that take an `as` prop that is the
|
|
802
|
+
// PropTypes.elementType to use for the inner tag, so we'll need to pass `tagName` along if it is provided.
|
|
803
|
+
// NOTE, the `as` prop is native to `semantic-ui` and is emulated in the `material-ui` theme
|
|
804
|
+
const as = _internalFormWrapper ? tagName : undefined;
|
|
805
|
+
const FormTag = _internalFormWrapper || tagName || 'form';
|
|
806
|
+
|
|
807
|
+
let { [SUBMIT_BTN_OPTIONS_KEY]: submitOptions = {} } = getUiOptions<T, S, F>(uiSchema);
|
|
808
|
+
if (disabled) {
|
|
809
|
+
submitOptions = { ...submitOptions, props: { ...submitOptions.props, disabled: true } };
|
|
810
|
+
}
|
|
811
|
+
const submitUiSchema = { [UI_OPTIONS_KEY]: { [SUBMIT_BTN_OPTIONS_KEY]: submitOptions } };
|
|
812
|
+
|
|
813
|
+
return (
|
|
814
|
+
<FormTag
|
|
815
|
+
className={className ? className : 'rjsf'}
|
|
816
|
+
id={id}
|
|
817
|
+
name={name}
|
|
818
|
+
method={method}
|
|
819
|
+
target={target}
|
|
820
|
+
action={action}
|
|
821
|
+
autoComplete={autoComplete}
|
|
822
|
+
encType={enctype}
|
|
823
|
+
acceptCharset={acceptcharset}
|
|
824
|
+
noValidate={noHtml5Validate}
|
|
825
|
+
onSubmit={this.onSubmit}
|
|
826
|
+
as={as}
|
|
827
|
+
ref={this.formElement}
|
|
828
|
+
>
|
|
829
|
+
{showErrorList === 'top' && this.renderErrors(registry)}
|
|
830
|
+
<_SchemaField
|
|
831
|
+
name=''
|
|
832
|
+
schema={schema}
|
|
833
|
+
uiSchema={uiSchema}
|
|
834
|
+
errorSchema={errorSchema}
|
|
835
|
+
idSchema={idSchema}
|
|
836
|
+
idPrefix={idPrefix}
|
|
837
|
+
idSeparator={idSeparator}
|
|
838
|
+
formContext={formContext}
|
|
839
|
+
formData={formData}
|
|
840
|
+
onChange={this.onChange}
|
|
841
|
+
onBlur={this.onBlur}
|
|
842
|
+
onFocus={this.onFocus}
|
|
843
|
+
registry={registry}
|
|
844
|
+
disabled={disabled}
|
|
845
|
+
readonly={readonly}
|
|
846
|
+
/>
|
|
847
|
+
|
|
848
|
+
{children ? children : <SubmitButton uiSchema={submitUiSchema} registry={registry} />}
|
|
849
|
+
{showErrorList === 'bottom' && this.renderErrors(registry)}
|
|
850
|
+
</FormTag>
|
|
851
|
+
);
|
|
852
|
+
}
|
|
853
|
+
}
|