@rjsf/core 6.6.1 → 6.7.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/LICENSE.md +201 -0
- package/dist/core.umd.js +694 -584
- package/dist/index.cjs +1191 -1071
- package/dist/index.cjs.map +4 -4
- package/dist/index.esm.js +1030 -984
- package/dist/index.esm.js.map +4 -4
- package/lib/components/Form.d.ts +16 -13
- package/lib/components/Form.d.ts.map +1 -1
- package/lib/components/Form.js +126 -76
- package/lib/components/RichDescription.d.ts +3 -3
- package/lib/components/RichDescription.d.ts.map +1 -1
- package/lib/components/RichDescription.js +1 -1
- package/lib/components/RichHelp.d.ts +3 -3
- package/lib/components/RichHelp.d.ts.map +1 -1
- package/lib/components/RichHelp.js +1 -1
- package/lib/components/SchemaExamples.d.ts +2 -2
- package/lib/components/SchemaExamples.d.ts.map +1 -1
- package/lib/components/SchemaExamples.js +1 -3
- package/lib/components/fields/ArrayField.d.ts +2 -2
- package/lib/components/fields/ArrayField.d.ts.map +1 -1
- package/lib/components/fields/ArrayField.js +87 -88
- package/lib/components/fields/BooleanField.d.ts +2 -2
- package/lib/components/fields/BooleanField.d.ts.map +1 -1
- package/lib/components/fields/BooleanField.js +2 -5
- package/lib/components/fields/CyclicSchemaField.d.ts +8 -0
- package/lib/components/fields/CyclicSchemaField.d.ts.map +1 -0
- package/lib/components/fields/CyclicSchemaField.js +21 -0
- package/lib/components/fields/FallbackField.d.ts +2 -2
- package/lib/components/fields/FallbackField.d.ts.map +1 -1
- package/lib/components/fields/FallbackField.js +2 -2
- package/lib/components/fields/LayoutGridField.d.ts +8 -10
- package/lib/components/fields/LayoutGridField.d.ts.map +1 -1
- package/lib/components/fields/LayoutGridField.js +5 -4
- package/lib/components/fields/LayoutHeaderField.d.ts +2 -2
- package/lib/components/fields/LayoutHeaderField.d.ts.map +1 -1
- package/lib/components/fields/LayoutHeaderField.js +1 -1
- package/lib/components/fields/LayoutMultiSchemaField.d.ts +2 -2
- package/lib/components/fields/LayoutMultiSchemaField.d.ts.map +1 -1
- package/lib/components/fields/LayoutMultiSchemaField.js +4 -1
- package/lib/components/fields/MultiSchemaField.d.ts +2 -41
- package/lib/components/fields/MultiSchemaField.d.ts.map +1 -1
- package/lib/components/fields/MultiSchemaField.js +116 -122
- package/lib/components/fields/NullField.d.ts +1 -1
- package/lib/components/fields/NullField.d.ts.map +1 -1
- package/lib/components/fields/NumberField.d.ts +2 -2
- package/lib/components/fields/NumberField.d.ts.map +1 -1
- package/lib/components/fields/NumberField.js +7 -9
- package/lib/components/fields/ObjectField.d.ts +2 -2
- package/lib/components/fields/ObjectField.d.ts.map +1 -1
- package/lib/components/fields/ObjectField.js +28 -26
- package/lib/components/fields/OptionalDataControlsField.d.ts +2 -2
- package/lib/components/fields/OptionalDataControlsField.d.ts.map +1 -1
- package/lib/components/fields/SchemaField.d.ts +14 -8
- package/lib/components/fields/SchemaField.d.ts.map +1 -1
- package/lib/components/fields/SchemaField.js +43 -30
- package/lib/components/fields/StringField.d.ts +2 -2
- package/lib/components/fields/StringField.d.ts.map +1 -1
- package/lib/components/fields/StringField.js +2 -5
- package/lib/components/fields/index.d.ts +1 -1
- package/lib/components/fields/index.d.ts.map +1 -1
- package/lib/components/fields/index.js +2 -0
- package/lib/components/templates/ArrayFieldDescriptionTemplate.d.ts +2 -2
- package/lib/components/templates/ArrayFieldDescriptionTemplate.d.ts.map +1 -1
- package/lib/components/templates/ArrayFieldDescriptionTemplate.js +1 -1
- package/lib/components/templates/ArrayFieldItemButtonsTemplate.d.ts +2 -2
- package/lib/components/templates/ArrayFieldItemButtonsTemplate.d.ts.map +1 -1
- package/lib/components/templates/ArrayFieldItemButtonsTemplate.js +1 -1
- package/lib/components/templates/ArrayFieldItemTemplate.d.ts +2 -2
- package/lib/components/templates/ArrayFieldItemTemplate.d.ts.map +1 -1
- package/lib/components/templates/ArrayFieldItemTemplate.js +1 -1
- package/lib/components/templates/ArrayFieldTemplate.d.ts +2 -2
- package/lib/components/templates/ArrayFieldTemplate.d.ts.map +1 -1
- package/lib/components/templates/ArrayFieldTemplate.js +1 -1
- package/lib/components/templates/ArrayFieldTitleTemplate.d.ts +2 -2
- package/lib/components/templates/ArrayFieldTitleTemplate.d.ts.map +1 -1
- package/lib/components/templates/ArrayFieldTitleTemplate.js +1 -1
- package/lib/components/templates/BaseInputTemplate.d.ts +2 -2
- package/lib/components/templates/BaseInputTemplate.d.ts.map +1 -1
- package/lib/components/templates/BaseInputTemplate.js +7 -6
- package/lib/components/templates/ButtonTemplates/AddButton.d.ts +2 -2
- package/lib/components/templates/ButtonTemplates/AddButton.d.ts.map +1 -1
- package/lib/components/templates/ButtonTemplates/IconButton.d.ts +14 -7
- package/lib/components/templates/ButtonTemplates/IconButton.d.ts.map +1 -1
- package/lib/components/templates/ButtonTemplates/IconButton.js +14 -6
- package/lib/components/templates/ButtonTemplates/SubmitButton.d.ts +2 -2
- package/lib/components/templates/ButtonTemplates/SubmitButton.d.ts.map +1 -1
- package/lib/components/templates/ButtonTemplates/index.d.ts +1 -1
- package/lib/components/templates/ButtonTemplates/index.d.ts.map +1 -1
- package/lib/components/templates/CyclicSchemaExpandTemplate.d.ts +7 -0
- package/lib/components/templates/CyclicSchemaExpandTemplate.d.ts.map +1 -0
- package/lib/components/templates/CyclicSchemaExpandTemplate.js +12 -0
- package/lib/components/templates/DescriptionField.d.ts +2 -2
- package/lib/components/templates/DescriptionField.d.ts.map +1 -1
- package/lib/components/templates/ErrorList.d.ts +2 -2
- package/lib/components/templates/ErrorList.d.ts.map +1 -1
- package/lib/components/templates/ErrorList.js +2 -4
- package/lib/components/templates/FallbackFieldTemplate.d.ts +2 -2
- package/lib/components/templates/FallbackFieldTemplate.d.ts.map +1 -1
- package/lib/components/templates/FieldErrorTemplate.d.ts +2 -2
- package/lib/components/templates/FieldErrorTemplate.d.ts.map +1 -1
- package/lib/components/templates/FieldErrorTemplate.js +1 -3
- package/lib/components/templates/FieldHelpTemplate.d.ts +2 -2
- package/lib/components/templates/FieldHelpTemplate.d.ts.map +1 -1
- package/lib/components/templates/FieldTemplate/FieldTemplate.d.ts +2 -2
- package/lib/components/templates/FieldTemplate/FieldTemplate.d.ts.map +1 -1
- package/lib/components/templates/FieldTemplate/FieldTemplate.js +1 -1
- package/lib/components/templates/FieldTemplate/Label.d.ts +3 -3
- package/lib/components/templates/FieldTemplate/Label.d.ts.map +1 -1
- package/lib/components/templates/GridTemplate.d.ts +2 -2
- package/lib/components/templates/GridTemplate.d.ts.map +1 -1
- package/lib/components/templates/MultiSchemaFieldTemplate.d.ts +2 -2
- package/lib/components/templates/MultiSchemaFieldTemplate.d.ts.map +1 -1
- package/lib/components/templates/ObjectFieldTemplate.d.ts +2 -2
- package/lib/components/templates/ObjectFieldTemplate.d.ts.map +1 -1
- package/lib/components/templates/ObjectFieldTemplate.js +1 -1
- package/lib/components/templates/OptionalDataControlsTemplate.d.ts +2 -2
- package/lib/components/templates/OptionalDataControlsTemplate.d.ts.map +1 -1
- package/lib/components/templates/OptionalDataControlsTemplate.js +1 -1
- package/lib/components/templates/TitleField.d.ts +2 -2
- package/lib/components/templates/TitleField.d.ts.map +1 -1
- package/lib/components/templates/UnsupportedField.d.ts +2 -2
- package/lib/components/templates/UnsupportedField.d.ts.map +1 -1
- package/lib/components/templates/UnsupportedField.js +2 -2
- package/lib/components/templates/WrapIfAdditionalTemplate.d.ts +2 -2
- package/lib/components/templates/WrapIfAdditionalTemplate.d.ts.map +1 -1
- package/lib/components/templates/WrapIfAdditionalTemplate.js +1 -1
- package/lib/components/templates/index.d.ts +1 -1
- package/lib/components/templates/index.d.ts.map +1 -1
- package/lib/components/templates/index.js +2 -0
- package/lib/components/widgets/AltDateTimeWidget.d.ts +2 -2
- package/lib/components/widgets/AltDateTimeWidget.d.ts.map +1 -1
- package/lib/components/widgets/AltDateWidget.d.ts +2 -2
- package/lib/components/widgets/AltDateWidget.d.ts.map +1 -1
- package/lib/components/widgets/AltDateWidget.js +2 -2
- package/lib/components/widgets/CheckboxWidget.d.ts +2 -2
- package/lib/components/widgets/CheckboxWidget.d.ts.map +1 -1
- package/lib/components/widgets/CheckboxesWidget.d.ts +2 -2
- package/lib/components/widgets/CheckboxesWidget.d.ts.map +1 -1
- package/lib/components/widgets/CheckboxesWidget.js +4 -4
- package/lib/components/widgets/ColorWidget.d.ts +2 -2
- package/lib/components/widgets/ColorWidget.d.ts.map +1 -1
- package/lib/components/widgets/DateTimeWidget.d.ts +2 -2
- package/lib/components/widgets/DateTimeWidget.d.ts.map +1 -1
- package/lib/components/widgets/DateTimeWidget.js +2 -2
- package/lib/components/widgets/DateWidget.d.ts +2 -2
- package/lib/components/widgets/DateWidget.d.ts.map +1 -1
- package/lib/components/widgets/EmailWidget.d.ts +2 -2
- package/lib/components/widgets/EmailWidget.d.ts.map +1 -1
- package/lib/components/widgets/FileWidget.d.ts +2 -2
- package/lib/components/widgets/FileWidget.d.ts.map +1 -1
- package/lib/components/widgets/FileWidget.js +6 -4
- package/lib/components/widgets/HiddenWidget.d.ts +2 -2
- package/lib/components/widgets/HiddenWidget.d.ts.map +1 -1
- package/lib/components/widgets/PasswordWidget.d.ts +2 -2
- package/lib/components/widgets/PasswordWidget.d.ts.map +1 -1
- package/lib/components/widgets/RadioWidget.d.ts +2 -2
- package/lib/components/widgets/RadioWidget.d.ts.map +1 -1
- package/lib/components/widgets/RadioWidget.js +4 -4
- package/lib/components/widgets/RangeWidget.d.ts +2 -2
- package/lib/components/widgets/RangeWidget.d.ts.map +1 -1
- package/lib/components/widgets/RatingWidget.d.ts +2 -2
- package/lib/components/widgets/RatingWidget.d.ts.map +1 -1
- package/lib/components/widgets/RatingWidget.js +15 -15
- package/lib/components/widgets/SelectWidget.d.ts +2 -2
- package/lib/components/widgets/SelectWidget.d.ts.map +1 -1
- package/lib/components/widgets/SelectWidget.js +9 -8
- package/lib/components/widgets/TextWidget.d.ts +2 -2
- package/lib/components/widgets/TextWidget.d.ts.map +1 -1
- package/lib/components/widgets/TextareaWidget.d.ts +2 -2
- package/lib/components/widgets/TextareaWidget.d.ts.map +1 -1
- package/lib/components/widgets/TextareaWidget.js +5 -5
- package/lib/components/widgets/TimeWidget.d.ts +2 -2
- package/lib/components/widgets/TimeWidget.d.ts.map +1 -1
- package/lib/components/widgets/URLWidget.d.ts +2 -2
- package/lib/components/widgets/URLWidget.d.ts.map +1 -1
- package/lib/components/widgets/UpDownWidget.d.ts +2 -2
- package/lib/components/widgets/UpDownWidget.d.ts.map +1 -1
- package/lib/components/widgets/index.d.ts +1 -1
- package/lib/components/widgets/index.d.ts.map +1 -1
- package/lib/getDefaultRegistry.d.ts +1 -1
- package/lib/getDefaultRegistry.d.ts.map +1 -1
- package/lib/getDefaultRegistry.js +1 -1
- package/lib/getTestRegistry.d.ts +1 -1
- package/lib/getTestRegistry.d.ts.map +1 -1
- package/lib/getTestRegistry.js +1 -1
- package/lib/index.d.ts +10 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/withTheme.d.ts +3 -3
- package/lib/withTheme.d.ts.map +1 -1
- package/lib/withTheme.js +6 -6
- package/package.json +28 -28
- package/src/components/Form.tsx +197 -111
- package/src/components/RichDescription.tsx +3 -10
- package/src/components/RichHelp.tsx +3 -10
- package/src/components/SchemaExamples.tsx +6 -4
- package/src/components/fields/ArrayField.tsx +122 -108
- package/src/components/fields/BooleanField.tsx +4 -9
- package/src/components/fields/CyclicSchemaField.tsx +40 -0
- package/src/components/fields/FallbackField.tsx +4 -8
- package/src/components/fields/LayoutGridField.tsx +21 -19
- package/src/components/fields/LayoutHeaderField.tsx +2 -10
- package/src/components/fields/LayoutMultiSchemaField.tsx +15 -10
- package/src/components/fields/MultiSchemaField.tsx +202 -218
- package/src/components/fields/NullField.tsx +1 -1
- package/src/components/fields/NumberField.tsx +8 -10
- package/src/components/fields/ObjectField.tsx +52 -40
- package/src/components/fields/OptionalDataControlsField.tsx +6 -4
- package/src/components/fields/SchemaField.tsx +66 -51
- package/src/components/fields/StringField.tsx +4 -15
- package/src/components/fields/index.ts +3 -1
- package/src/components/templates/ArrayFieldDescriptionTemplate.tsx +2 -9
- package/src/components/templates/ArrayFieldItemButtonsTemplate.tsx +2 -7
- package/src/components/templates/ArrayFieldItemTemplate.tsx +3 -9
- package/src/components/templates/ArrayFieldTemplate.tsx +2 -9
- package/src/components/templates/ArrayFieldTitleTemplate.tsx +2 -10
- package/src/components/templates/BaseInputTemplate.tsx +16 -23
- package/src/components/templates/ButtonTemplates/AddButton.tsx +2 -1
- package/src/components/templates/ButtonTemplates/IconButton.tsx +16 -7
- package/src/components/templates/ButtonTemplates/SubmitButton.tsx +2 -1
- package/src/components/templates/ButtonTemplates/index.ts +1 -1
- package/src/components/templates/CyclicSchemaExpandTemplate.tsx +31 -0
- package/src/components/templates/DescriptionField.tsx +1 -1
- package/src/components/templates/ErrorList.tsx +8 -15
- package/src/components/templates/FallbackFieldTemplate.tsx +2 -1
- package/src/components/templates/FieldErrorTemplate.tsx +8 -8
- package/src/components/templates/FieldHelpTemplate.tsx +2 -1
- package/src/components/templates/FieldTemplate/FieldTemplate.tsx +2 -8
- package/src/components/templates/FieldTemplate/Label.tsx +2 -2
- package/src/components/templates/GridTemplate.tsx +1 -1
- package/src/components/templates/MultiSchemaFieldTemplate.tsx +1 -1
- package/src/components/templates/ObjectFieldTemplate.tsx +2 -7
- package/src/components/templates/OptionalDataControlsTemplate.tsx +3 -2
- package/src/components/templates/TitleField.tsx +1 -1
- package/src/components/templates/UnsupportedField.tsx +4 -3
- package/src/components/templates/WrapIfAdditionalTemplate.tsx +2 -9
- package/src/components/templates/index.ts +3 -1
- package/src/components/widgets/AltDateTimeWidget.tsx +1 -1
- package/src/components/widgets/AltDateWidget.tsx +7 -13
- package/src/components/widgets/CheckboxWidget.tsx +3 -5
- package/src/components/widgets/CheckboxesWidget.tsx +8 -10
- package/src/components/widgets/ColorWidget.tsx +2 -1
- package/src/components/widgets/DateTimeWidget.tsx +3 -10
- package/src/components/widgets/DateWidget.tsx +2 -1
- package/src/components/widgets/EmailWidget.tsx +2 -1
- package/src/components/widgets/FileWidget.tsx +9 -8
- package/src/components/widgets/HiddenWidget.tsx +1 -1
- package/src/components/widgets/PasswordWidget.tsx +2 -1
- package/src/components/widgets/RadioWidget.tsx +8 -10
- package/src/components/widgets/RangeWidget.tsx +1 -1
- package/src/components/widgets/RatingWidget.tsx +48 -47
- package/src/components/widgets/SelectWidget.tsx +48 -31
- package/src/components/widgets/TextWidget.tsx +2 -1
- package/src/components/widgets/TextareaWidget.tsx +10 -7
- package/src/components/widgets/TimeWidget.tsx +2 -1
- package/src/components/widgets/URLWidget.tsx +2 -1
- package/src/components/widgets/UpDownWidget.tsx +2 -1
- package/src/components/widgets/index.ts +1 -1
- package/src/getDefaultRegistry.ts +2 -9
- package/src/getTestRegistry.tsx +2 -7
- package/src/index.ts +10 -5
- package/src/withTheme.tsx +14 -9
|
@@ -1,112 +1,104 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import type { FieldProps, FormContextType, RJSFSchema, StrictRJSFSchema, UiSchema } from '@rjsf/utils';
|
|
2
3
|
import {
|
|
3
4
|
ANY_OF_KEY,
|
|
4
5
|
deepEquals,
|
|
5
6
|
ERRORS_KEY,
|
|
6
|
-
FieldProps,
|
|
7
|
-
FormContextType,
|
|
8
7
|
getDiscriminatorFieldFromSchema,
|
|
9
8
|
getTemplate,
|
|
10
9
|
getUiOptions,
|
|
11
10
|
getWidget,
|
|
11
|
+
hashObject,
|
|
12
12
|
isFormDataAvailable,
|
|
13
13
|
mergeSchemas,
|
|
14
14
|
ONE_OF_KEY,
|
|
15
|
-
RJSFSchema,
|
|
16
15
|
shouldRenderOptionalField,
|
|
17
|
-
StrictRJSFSchema,
|
|
18
16
|
TranslatableString,
|
|
19
|
-
UiSchema,
|
|
20
17
|
} from '@rjsf/utils';
|
|
21
18
|
import get from 'lodash/get';
|
|
22
19
|
import isEmpty from 'lodash/isEmpty';
|
|
23
20
|
import omit from 'lodash/omit';
|
|
24
21
|
|
|
25
|
-
/** Type used for the state of the `AnyOfField` component */
|
|
26
|
-
type AnyOfFieldState<S extends StrictRJSFSchema = RJSFSchema> = {
|
|
27
|
-
/** The currently selected option */
|
|
28
|
-
selectedOption: number;
|
|
29
|
-
/** The option schemas after retrieving all $refs */
|
|
30
|
-
retrievedOptions: S[];
|
|
31
|
-
};
|
|
32
|
-
|
|
33
22
|
/** The `AnyOfField` component is used to render a field in the schema that is an `anyOf`, `allOf` or `oneOf`. It tracks
|
|
34
23
|
* the currently selected option and cleans up any irrelevant data in `formData`.
|
|
35
24
|
*
|
|
36
25
|
* @param props - The `FieldProps` for this template
|
|
37
26
|
*/
|
|
38
|
-
|
|
39
|
-
FieldProps<T, S, F>,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
27
|
+
function AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
28
|
+
props: FieldProps<T, S, F>,
|
|
29
|
+
) {
|
|
30
|
+
const {
|
|
31
|
+
name,
|
|
32
|
+
disabled = false,
|
|
33
|
+
errorSchema = {},
|
|
34
|
+
formData,
|
|
35
|
+
fieldPathId,
|
|
36
|
+
onBlur,
|
|
37
|
+
onChange,
|
|
38
|
+
onFocus,
|
|
39
|
+
options,
|
|
40
|
+
readonly,
|
|
41
|
+
registry,
|
|
42
|
+
required = false,
|
|
43
|
+
schema,
|
|
44
|
+
uiSchema,
|
|
45
|
+
} = props;
|
|
46
|
+
const { schemaUtils } = registry;
|
|
48
47
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
registry: { schemaUtils },
|
|
53
|
-
} = this.props;
|
|
54
|
-
// cache the retrieved options in state in case they have $refs to save doing it later
|
|
55
|
-
const retrievedOptions = options.map((opt: S) => schemaUtils.retrieveSchema(opt, formData));
|
|
48
|
+
// Hash formData by value so the memo only invalidates when data actually changes, not on every
|
|
49
|
+
// new object reference. hashObject(undefined) throws, so null is used as the fallback.
|
|
50
|
+
const formDataHash = hashObject(formData ?? null);
|
|
56
51
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
52
|
+
// retrievedOptions is purely derived from options — useMemo handles re-derivation automatically
|
|
53
|
+
// when options, schemaUtils, or formData's value changes, with no render-phase dispatch needed.
|
|
54
|
+
const retrievedOptions = useMemo(
|
|
55
|
+
() => options.map((opt: S) => schemaUtils.retrieveSchema(opt, formData)),
|
|
56
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps -- formDataHash is the value-stable proxy for formData
|
|
57
|
+
[options, schemaUtils, formDataHash],
|
|
58
|
+
);
|
|
62
59
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
const [selectedOption, setSelectedOption] = useState<number>(() => {
|
|
61
|
+
const discriminator = getDiscriminatorFieldFromSchema<S>(schema);
|
|
62
|
+
return schemaUtils.getClosestMatchingOption(formData, retrievedOptions, 0, discriminator);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/** Flag to skip the formData-change-driven option recalculation when the user just selected an option.
|
|
66
|
+
* Set to true in onOptionChange (before onChange is called), consumed and reset in the update effect.
|
|
67
|
+
* This prevents the matching-option recalculation from overriding a user's explicit choice when
|
|
68
|
+
* getDefaultFormState populates undefined properties that make deepEquals see a false formData change.
|
|
68
69
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (!deepEquals(formData, prevProps.formData) && fieldPathId.$id === prevProps.fieldPathId.$id) {
|
|
82
|
-
const { retrievedOptions } = newState;
|
|
83
|
-
const matchingOption = this.getMatchingOption(selectedOption, formData, retrievedOptions);
|
|
70
|
+
const skipNextOptionRecalculation = useRef(false);
|
|
71
|
+
const prevFormDataRef = useRef<T | undefined>(formData);
|
|
72
|
+
const prevFieldIdRef = useRef(fieldPathId.$id);
|
|
73
|
+
|
|
74
|
+
// Mirrors componentDidUpdate: re-match selectedOption when formData changes on the same field.
|
|
75
|
+
// Runs after every render (no deps array) to compare against prev values stored in refs.
|
|
76
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
const prevFormData = prevFormDataRef.current;
|
|
79
|
+
const prevFieldId = prevFieldIdRef.current;
|
|
80
|
+
prevFormDataRef.current = formData;
|
|
81
|
+
prevFieldIdRef.current = fieldPathId.$id;
|
|
84
82
|
|
|
85
|
-
|
|
86
|
-
|
|
83
|
+
if (!deepEquals(formData, prevFormData) && fieldPathId.$id === prevFieldId) {
|
|
84
|
+
if (skipNextOptionRecalculation.current) {
|
|
85
|
+
skipNextOptionRecalculation.current = false;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const discriminator = getDiscriminatorFieldFromSchema<S>(schema);
|
|
89
|
+
const matchingOption = schemaUtils.getClosestMatchingOption(
|
|
90
|
+
formData,
|
|
91
|
+
retrievedOptions,
|
|
92
|
+
selectedOption,
|
|
93
|
+
discriminator,
|
|
94
|
+
);
|
|
95
|
+
if (matchingOption !== selectedOption) {
|
|
96
|
+
setSelectedOption(matchingOption);
|
|
87
97
|
}
|
|
88
98
|
}
|
|
89
|
-
|
|
90
|
-
this.setState(newState);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Determines the best matching option for the given `formData` and `options`.
|
|
95
|
-
*
|
|
96
|
-
* @param formData - The new formData
|
|
97
|
-
* @param options - The list of options to choose from
|
|
98
|
-
* @return - The index of the `option` that best matches the `formData`
|
|
99
|
-
*/
|
|
100
|
-
getMatchingOption(selectedOption: number, formData: T | undefined, options: S[]) {
|
|
101
|
-
const {
|
|
102
|
-
schema,
|
|
103
|
-
registry: { schemaUtils },
|
|
104
|
-
} = this.props;
|
|
99
|
+
});
|
|
105
100
|
|
|
106
|
-
|
|
107
|
-
const option = schemaUtils.getClosestMatchingOption(formData, options, selectedOption, discriminator);
|
|
108
|
-
return option;
|
|
109
|
-
}
|
|
101
|
+
const fieldId = `${fieldPathId.$id}${schema.oneOf ? '__oneof_select' : '__anyof_select'}`;
|
|
110
102
|
|
|
111
103
|
/** Callback handler to remember what the currently selected option is. In addition to that the `formData` is updated
|
|
112
104
|
* to remove properties that are not part of the newly selected option schema, and then the updated data is passed to
|
|
@@ -114,160 +106,152 @@ class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
|
|
|
114
106
|
*
|
|
115
107
|
* @param option - The new option value being selected
|
|
116
108
|
*/
|
|
117
|
-
onOptionChange = (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
let newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
|
|
129
|
-
if (newOption) {
|
|
130
|
-
// Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
|
|
131
|
-
// so that only the root objects themselves are created without adding undefined children properties
|
|
132
|
-
newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, 'excludeObjectChildren') as T;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
this.setState({ selectedOption: intOption }, () => {
|
|
136
|
-
onChange(newFormData, fieldPathId.path, undefined, this.getFieldId());
|
|
137
|
-
});
|
|
138
|
-
};
|
|
109
|
+
const onOptionChange = useCallback(
|
|
110
|
+
(option?: string) => {
|
|
111
|
+
if (disabled || readonly) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const intOption = option !== undefined ? parseInt(option, 10) : -1;
|
|
115
|
+
if (intOption === selectedOption) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const newOption = intOption >= 0 ? retrievedOptions[intOption] : undefined;
|
|
119
|
+
const oldOption = selectedOption >= 0 ? retrievedOptions[selectedOption] : undefined;
|
|
139
120
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
121
|
+
let newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
|
|
122
|
+
if (newOption) {
|
|
123
|
+
// Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
|
|
124
|
+
// so that only the root objects themselves are created without adding undefined children properties
|
|
125
|
+
newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, 'excludeObjectChildren') as T;
|
|
126
|
+
}
|
|
144
127
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
formData,
|
|
153
|
-
onBlur,
|
|
154
|
-
onFocus,
|
|
155
|
-
readonly,
|
|
156
|
-
required = false,
|
|
157
|
-
registry,
|
|
158
|
-
schema,
|
|
159
|
-
uiSchema,
|
|
160
|
-
} = this.props;
|
|
128
|
+
setSelectedOption(intOption);
|
|
129
|
+
skipNextOptionRecalculation.current = true;
|
|
130
|
+
onChange(newFormData, fieldPathId.path, undefined, fieldId);
|
|
131
|
+
},
|
|
132
|
+
// setSelectedOption is stable (guaranteed by useState); skipNextOptionRecalculation is a ref
|
|
133
|
+
[selectedOption, retrievedOptions, disabled, readonly, schemaUtils, formData, fieldPathId, onChange, fieldId],
|
|
134
|
+
);
|
|
161
135
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
136
|
+
const { widgets, fields, translateString, globalUiOptions } = registry;
|
|
137
|
+
const { SchemaField: SchemaFieldComponent } = fields;
|
|
138
|
+
const MultiSchemaFieldTemplate = getTemplate<'MultiSchemaFieldTemplate', T, S, F>(
|
|
139
|
+
'MultiSchemaFieldTemplate',
|
|
140
|
+
registry,
|
|
141
|
+
globalUiOptions,
|
|
142
|
+
);
|
|
143
|
+
const isOptionalRender = shouldRenderOptionalField<T, S, F>(registry, schema, required, uiSchema);
|
|
144
|
+
const hasFormData = isFormDataAvailable<T>(formData);
|
|
171
145
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const displayLabel = schemaUtils.getDisplayLabel(schema, uiSchema, globalUiOptions);
|
|
146
|
+
const {
|
|
147
|
+
widget = 'select',
|
|
148
|
+
placeholder,
|
|
149
|
+
autofocus,
|
|
150
|
+
autocomplete,
|
|
151
|
+
title = schema.title,
|
|
152
|
+
...uiOptions
|
|
153
|
+
} = getUiOptions<T, S, F>(uiSchema, globalUiOptions);
|
|
154
|
+
const Widget = getWidget<T, S, F>({ type: 'number' }, widget, widgets);
|
|
155
|
+
const rawErrors = get(errorSchema, ERRORS_KEY, []);
|
|
156
|
+
const fieldErrorSchema = omit(errorSchema, [ERRORS_KEY]);
|
|
157
|
+
const displayLabel = schemaUtils.getDisplayLabel(schema, uiSchema, globalUiOptions);
|
|
185
158
|
|
|
186
|
-
|
|
187
|
-
|
|
159
|
+
const option = selectedOption >= 0 ? retrievedOptions[selectedOption] || null : null;
|
|
160
|
+
let optionSchema: S | undefined | null;
|
|
188
161
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
162
|
+
if (option) {
|
|
163
|
+
const { required: schemaRequired, type: schemaType } = schema;
|
|
164
|
+
const parentProps: Partial<S> = {};
|
|
165
|
+
if (schemaRequired) {
|
|
166
|
+
parentProps.required = schemaRequired as S['required'];
|
|
167
|
+
}
|
|
168
|
+
// Propagate the parent schema type to options that don't define their own.
|
|
169
|
+
// This is necessary when the parent constrains the type (e.g. { type: 'string',
|
|
170
|
+
// oneOf: [{ pattern: '...' }, { pattern: '...' }] }) but the option sub-schemas
|
|
171
|
+
// omit the type — without it, getSchemaType returns undefined and the option
|
|
172
|
+
// renders as FallbackField instead of the correct widget (e.g. StringField).
|
|
173
|
+
if (schemaType !== undefined && !('type' in option)) {
|
|
174
|
+
parentProps.type = schemaType;
|
|
194
175
|
}
|
|
176
|
+
// Merge in all the non-oneOf/anyOf properties and also skip the special ADDITIONAL_PROPERTY_FLAG property
|
|
177
|
+
optionSchema = Object.keys(parentProps).length > 0 ? (mergeSchemas(parentProps, option) as S) : option;
|
|
178
|
+
}
|
|
195
179
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
} else if (ANY_OF_KEY in schema && uiSchema && ANY_OF_KEY in uiSchema) {
|
|
205
|
-
if (Array.isArray(uiSchema[ANY_OF_KEY])) {
|
|
206
|
-
optionsUiSchema = uiSchema[ANY_OF_KEY];
|
|
207
|
-
} else {
|
|
208
|
-
console.warn(`uiSchema.anyOf is not an array for "${title || name}"`);
|
|
209
|
-
}
|
|
180
|
+
// First we will check to see if there is an anyOf/oneOf override for the UI schema
|
|
181
|
+
let optionsUiSchema: UiSchema<T, S, F>[] = [];
|
|
182
|
+
if (ONE_OF_KEY in schema && uiSchema && ONE_OF_KEY in uiSchema) {
|
|
183
|
+
if (Array.isArray(uiSchema[ONE_OF_KEY])) {
|
|
184
|
+
optionsUiSchema = uiSchema[ONE_OF_KEY];
|
|
185
|
+
} else {
|
|
186
|
+
// oxlint-disable-next-line no-console
|
|
187
|
+
console.warn(`uiSchema.oneOf is not an array for "${title || name}"`);
|
|
210
188
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
189
|
+
} else if (ANY_OF_KEY in schema && uiSchema && ANY_OF_KEY in uiSchema) {
|
|
190
|
+
if (Array.isArray(uiSchema[ANY_OF_KEY])) {
|
|
191
|
+
optionsUiSchema = uiSchema[ANY_OF_KEY];
|
|
192
|
+
} else {
|
|
193
|
+
// oxlint-disable-next-line no-console
|
|
194
|
+
console.warn(`uiSchema.anyOf is not an array for "${title || name}"`);
|
|
215
195
|
}
|
|
196
|
+
}
|
|
197
|
+
// Then we pick the one that matches the selected option index, if one exists otherwise default to the main uiSchema
|
|
198
|
+
let optionUiSchema = uiSchema;
|
|
199
|
+
if (selectedOption >= 0 && optionsUiSchema.length > selectedOption) {
|
|
200
|
+
optionUiSchema = optionsUiSchema[selectedOption];
|
|
201
|
+
}
|
|
216
202
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
const selector =
|
|
231
|
-
!isOptionalRender || hasFormData ? (
|
|
232
|
-
<Widget
|
|
233
|
-
id={this.getFieldId()}
|
|
234
|
-
name={`${name}${schema.oneOf ? '__oneof_select' : '__anyof_select'}`}
|
|
235
|
-
schema={{ type: 'number', default: 0 } as S}
|
|
236
|
-
onChange={this.onOptionChange}
|
|
237
|
-
onBlur={onBlur}
|
|
238
|
-
onFocus={onFocus}
|
|
239
|
-
disabled={disabled || isEmpty(enumOptions)}
|
|
240
|
-
multiple={false}
|
|
241
|
-
rawErrors={rawErrors}
|
|
242
|
-
errorSchema={fieldErrorSchema}
|
|
243
|
-
value={selectedOption >= 0 ? selectedOption : undefined}
|
|
244
|
-
options={{ enumOptions, ...uiOptions }}
|
|
245
|
-
registry={registry}
|
|
246
|
-
placeholder={placeholder}
|
|
247
|
-
autocomplete={autocomplete}
|
|
248
|
-
autofocus={autofocus}
|
|
249
|
-
label={title ?? name}
|
|
250
|
-
hideLabel={!displayLabel}
|
|
251
|
-
readonly={readonly}
|
|
252
|
-
/>
|
|
253
|
-
) : undefined;
|
|
254
|
-
|
|
255
|
-
const optionsSchemaField =
|
|
256
|
-
(optionSchema && optionSchema.type !== 'null' && (
|
|
257
|
-
<_SchemaField {...this.props} schema={optionSchema} uiSchema={optionUiSchema} />
|
|
258
|
-
)) ||
|
|
259
|
-
null;
|
|
203
|
+
const translateEnum: TranslatableString = title
|
|
204
|
+
? TranslatableString.TitleOptionPrefix
|
|
205
|
+
: TranslatableString.OptionPrefix;
|
|
206
|
+
const translateParams = title ? [title] : [];
|
|
207
|
+
const enumOptions = retrievedOptions.map((opt: { title?: string }, index: number) => {
|
|
208
|
+
const { title: uiTitle = opt.title } = getUiOptions<T, S, F>(optionsUiSchema[index]);
|
|
209
|
+
return {
|
|
210
|
+
label: uiTitle || translateString(translateEnum, translateParams.concat(String(index + 1))),
|
|
211
|
+
value: index,
|
|
212
|
+
};
|
|
213
|
+
});
|
|
260
214
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
215
|
+
const selector =
|
|
216
|
+
!isOptionalRender || hasFormData ? (
|
|
217
|
+
<Widget
|
|
218
|
+
id={fieldId}
|
|
219
|
+
name={`${name}${schema.oneOf ? '__oneof_select' : '__anyof_select'}`}
|
|
220
|
+
schema={{ type: 'number', default: 0 } as S}
|
|
221
|
+
onChange={onOptionChange}
|
|
222
|
+
onBlur={onBlur}
|
|
223
|
+
onFocus={onFocus}
|
|
224
|
+
disabled={disabled || isEmpty(enumOptions)}
|
|
225
|
+
multiple={false}
|
|
226
|
+
rawErrors={rawErrors}
|
|
227
|
+
errorSchema={fieldErrorSchema}
|
|
228
|
+
value={selectedOption >= 0 ? selectedOption : undefined}
|
|
229
|
+
options={{ enumOptions, ...uiOptions }}
|
|
264
230
|
registry={registry}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
231
|
+
placeholder={placeholder}
|
|
232
|
+
autocomplete={autocomplete}
|
|
233
|
+
autofocus={autofocus}
|
|
234
|
+
label={title ?? name}
|
|
235
|
+
hideLabel={!displayLabel}
|
|
236
|
+
readonly={readonly}
|
|
268
237
|
/>
|
|
269
|
-
);
|
|
270
|
-
|
|
238
|
+
) : undefined;
|
|
239
|
+
|
|
240
|
+
const optionsSchemaField =
|
|
241
|
+
(optionSchema && optionSchema.type !== 'null' && (
|
|
242
|
+
<SchemaFieldComponent {...props} schema={optionSchema} uiSchema={optionUiSchema} />
|
|
243
|
+
)) ||
|
|
244
|
+
null;
|
|
245
|
+
|
|
246
|
+
return (
|
|
247
|
+
<MultiSchemaFieldTemplate
|
|
248
|
+
schema={schema}
|
|
249
|
+
registry={registry}
|
|
250
|
+
uiSchema={uiSchema}
|
|
251
|
+
selector={selector}
|
|
252
|
+
optionSchemaField={optionsSchemaField}
|
|
253
|
+
/>
|
|
254
|
+
);
|
|
271
255
|
}
|
|
272
256
|
|
|
273
257
|
export default AnyOfField;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
-
import { FieldProps, FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
|
|
2
|
+
import type { FieldProps, FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
|
|
3
3
|
|
|
4
4
|
/** The `NullField` component is used to render a field in the schema is null. It also ensures that the `formData` is
|
|
5
5
|
* also set to null if it has no value.
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { useState, useCallback } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
asNumber,
|
|
2
|
+
import type {
|
|
4
3
|
ErrorSchema,
|
|
5
4
|
FieldPathList,
|
|
6
5
|
FieldProps,
|
|
@@ -8,6 +7,7 @@ import {
|
|
|
8
7
|
RJSFSchema,
|
|
9
8
|
StrictRJSFSchema,
|
|
10
9
|
} from '@rjsf/utils';
|
|
10
|
+
import { asNumber } from '@rjsf/utils';
|
|
11
11
|
|
|
12
12
|
// Matches a string that ends in a . character, optionally followed by a sequence of
|
|
13
13
|
// digits followed by any number of 0 characters up until the end of the line.
|
|
@@ -52,23 +52,21 @@ function NumberField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends
|
|
|
52
52
|
* @param value - The current value for the change occurring
|
|
53
53
|
*/
|
|
54
54
|
const handleChange = useCallback(
|
|
55
|
-
(
|
|
55
|
+
(newValue: FieldProps<T, S, F>['value'], path: FieldPathList, errorSchema?: ErrorSchema<T>, id?: string) => {
|
|
56
56
|
// Cache the original value in component state
|
|
57
|
-
setLastValue(
|
|
57
|
+
setLastValue(newValue);
|
|
58
58
|
|
|
59
59
|
// Normalize decimals that don't start with a zero character in advance so
|
|
60
60
|
// that the rest of the normalization logic is simpler
|
|
61
|
-
|
|
62
|
-
value = `0${value}`;
|
|
63
|
-
}
|
|
61
|
+
const normalizedValue = `${newValue}`.startsWith('.') ? `0${newValue}` : newValue;
|
|
64
62
|
|
|
65
63
|
// Check that the value is a string (this can happen if the widget used is a
|
|
66
64
|
// <select>, due to an enum declaration etc) then, if the value ends in a
|
|
67
65
|
// trailing decimal point or multiple zeroes, strip the trailing values
|
|
68
66
|
const processed =
|
|
69
|
-
typeof
|
|
70
|
-
? asNumber(
|
|
71
|
-
: asNumber(
|
|
67
|
+
typeof normalizedValue === 'string' && trailingCharMatcherWithPrefix.exec(normalizedValue)
|
|
68
|
+
? asNumber(normalizedValue.replace(trailingCharMatcher, ''))
|
|
69
|
+
: asNumber(normalizedValue);
|
|
72
70
|
|
|
73
71
|
onChange(processed as unknown as T, path, errorSchema, id);
|
|
74
72
|
},
|