@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,114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getWidget,
|
|
3
|
+
getUiOptions,
|
|
4
|
+
optionsList,
|
|
5
|
+
FieldProps,
|
|
6
|
+
FormContextType,
|
|
7
|
+
EnumOptionsType,
|
|
8
|
+
RJSFSchema,
|
|
9
|
+
StrictRJSFSchema,
|
|
10
|
+
TranslatableString,
|
|
11
|
+
} from '@rjsf/utils';
|
|
12
|
+
import isObject from 'lodash/isObject';
|
|
13
|
+
|
|
14
|
+
/** The `BooleanField` component is used to render a field in the schema is boolean. It constructs `enumOptions` for the
|
|
15
|
+
* two boolean values based on the various alternatives in the schema.
|
|
16
|
+
*
|
|
17
|
+
* @param props - The `FieldProps` for this template
|
|
18
|
+
*/
|
|
19
|
+
function BooleanField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
20
|
+
props: FieldProps<T, S, F>
|
|
21
|
+
) {
|
|
22
|
+
const {
|
|
23
|
+
schema,
|
|
24
|
+
name,
|
|
25
|
+
uiSchema,
|
|
26
|
+
idSchema,
|
|
27
|
+
formData,
|
|
28
|
+
registry,
|
|
29
|
+
required,
|
|
30
|
+
disabled,
|
|
31
|
+
readonly,
|
|
32
|
+
autofocus,
|
|
33
|
+
onChange,
|
|
34
|
+
onFocus,
|
|
35
|
+
onBlur,
|
|
36
|
+
rawErrors,
|
|
37
|
+
} = props;
|
|
38
|
+
const { title } = schema;
|
|
39
|
+
const { widgets, formContext, translateString, globalUiOptions } = registry;
|
|
40
|
+
const {
|
|
41
|
+
widget = 'checkbox',
|
|
42
|
+
title: uiTitle,
|
|
43
|
+
// Unlike the other fields, don't use `getDisplayLabel()` since it always returns false for the boolean type
|
|
44
|
+
label: displayLabel = true,
|
|
45
|
+
...options
|
|
46
|
+
} = getUiOptions<T, S, F>(uiSchema, globalUiOptions);
|
|
47
|
+
const Widget = getWidget(schema, widget, widgets);
|
|
48
|
+
const yes = translateString(TranslatableString.YesLabel);
|
|
49
|
+
const no = translateString(TranslatableString.NoLabel);
|
|
50
|
+
let enumOptions: EnumOptionsType<S>[] | undefined;
|
|
51
|
+
const label = uiTitle ?? title ?? name;
|
|
52
|
+
if (Array.isArray(schema.oneOf)) {
|
|
53
|
+
enumOptions = optionsList<S>({
|
|
54
|
+
oneOf: schema.oneOf
|
|
55
|
+
.map((option) => {
|
|
56
|
+
if (isObject(option)) {
|
|
57
|
+
return {
|
|
58
|
+
...option,
|
|
59
|
+
title: option.title || (option.const === true ? yes : no),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
})
|
|
64
|
+
.filter((o: any) => o) as S[], // cast away the error that typescript can't grok is fixed
|
|
65
|
+
} as unknown as S);
|
|
66
|
+
} else {
|
|
67
|
+
// We deprecated enumNames in v5. It's intentionally omitted from RSJFSchema type, so we need to cast here.
|
|
68
|
+
const schemaWithEnumNames = schema as S & { enumNames?: string[] };
|
|
69
|
+
const enums = schema.enum ?? [true, false];
|
|
70
|
+
if (!schemaWithEnumNames.enumNames && enums.length === 2 && enums.every((v: any) => typeof v === 'boolean')) {
|
|
71
|
+
enumOptions = [
|
|
72
|
+
{
|
|
73
|
+
value: enums[0],
|
|
74
|
+
label: enums[0] ? yes : no,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
value: enums[1],
|
|
78
|
+
label: enums[1] ? yes : no,
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
} else {
|
|
82
|
+
enumOptions = optionsList<S>({
|
|
83
|
+
enum: enums,
|
|
84
|
+
// NOTE: enumNames is deprecated, but still supported for now.
|
|
85
|
+
enumNames: schemaWithEnumNames.enumNames,
|
|
86
|
+
} as unknown as S);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<Widget
|
|
92
|
+
options={{ ...options, enumOptions }}
|
|
93
|
+
schema={schema}
|
|
94
|
+
uiSchema={uiSchema}
|
|
95
|
+
id={idSchema.$id}
|
|
96
|
+
name={name}
|
|
97
|
+
onChange={onChange}
|
|
98
|
+
onFocus={onFocus}
|
|
99
|
+
onBlur={onBlur}
|
|
100
|
+
label={label}
|
|
101
|
+
hideLabel={!displayLabel}
|
|
102
|
+
value={formData}
|
|
103
|
+
required={required}
|
|
104
|
+
disabled={disabled}
|
|
105
|
+
readonly={readonly}
|
|
106
|
+
registry={registry}
|
|
107
|
+
formContext={formContext}
|
|
108
|
+
autofocus={autofocus}
|
|
109
|
+
rawErrors={rawErrors}
|
|
110
|
+
/>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export default BooleanField;
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { Component } from 'react';
|
|
2
|
+
import get from 'lodash/get';
|
|
3
|
+
import isEmpty from 'lodash/isEmpty';
|
|
4
|
+
import omit from 'lodash/omit';
|
|
5
|
+
import unset from 'lodash/unset';
|
|
6
|
+
import {
|
|
7
|
+
ADDITIONAL_PROPERTY_FLAG,
|
|
8
|
+
deepEquals,
|
|
9
|
+
ERRORS_KEY,
|
|
10
|
+
FieldProps,
|
|
11
|
+
FormContextType,
|
|
12
|
+
getDiscriminatorFieldFromSchema,
|
|
13
|
+
getUiOptions,
|
|
14
|
+
getWidget,
|
|
15
|
+
mergeSchemas,
|
|
16
|
+
RJSFSchema,
|
|
17
|
+
StrictRJSFSchema,
|
|
18
|
+
TranslatableString,
|
|
19
|
+
} from '@rjsf/utils';
|
|
20
|
+
|
|
21
|
+
/** Type used for the state of the `AnyOfField` component */
|
|
22
|
+
type AnyOfFieldState<S extends StrictRJSFSchema = RJSFSchema> = {
|
|
23
|
+
/** The currently selected option */
|
|
24
|
+
selectedOption: number;
|
|
25
|
+
/** The option schemas after retrieving all $refs */
|
|
26
|
+
retrievedOptions: S[];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** The `AnyOfField` component is used to render a field in the schema that is an `anyOf`, `allOf` or `oneOf`. It tracks
|
|
30
|
+
* the currently selected option and cleans up any irrelevant data in `formData`.
|
|
31
|
+
*
|
|
32
|
+
* @param props - The `FieldProps` for this template
|
|
33
|
+
*/
|
|
34
|
+
class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> extends Component<
|
|
35
|
+
FieldProps<T, S, F>,
|
|
36
|
+
AnyOfFieldState<S>
|
|
37
|
+
> {
|
|
38
|
+
/** Constructs an `AnyOfField` with the given `props` to initialize the initially selected option in state
|
|
39
|
+
*
|
|
40
|
+
* @param props - The `FieldProps` for this template
|
|
41
|
+
*/
|
|
42
|
+
constructor(props: FieldProps<T, S, F>) {
|
|
43
|
+
super(props);
|
|
44
|
+
|
|
45
|
+
const {
|
|
46
|
+
formData,
|
|
47
|
+
options,
|
|
48
|
+
registry: { schemaUtils },
|
|
49
|
+
} = this.props;
|
|
50
|
+
// cache the retrieved options in state in case they have $refs to save doing it later
|
|
51
|
+
const retrievedOptions = options.map((opt: S) => schemaUtils.retrieveSchema(opt, formData));
|
|
52
|
+
|
|
53
|
+
this.state = {
|
|
54
|
+
retrievedOptions,
|
|
55
|
+
selectedOption: this.getMatchingOption(0, formData, retrievedOptions),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** React lifecycle method that is called when the props and/or state for this component is updated. It recomputes the
|
|
60
|
+
* currently selected option based on the overall `formData`
|
|
61
|
+
*
|
|
62
|
+
* @param prevProps - The previous `FieldProps` for this template
|
|
63
|
+
* @param prevState - The previous `AnyOfFieldState` for this template
|
|
64
|
+
*/
|
|
65
|
+
componentDidUpdate(prevProps: Readonly<FieldProps<T, S, F>>, prevState: Readonly<AnyOfFieldState>) {
|
|
66
|
+
const { formData, options, idSchema } = this.props;
|
|
67
|
+
const { selectedOption } = this.state;
|
|
68
|
+
let newState = this.state;
|
|
69
|
+
if (!deepEquals(prevProps.options, options)) {
|
|
70
|
+
const {
|
|
71
|
+
registry: { schemaUtils },
|
|
72
|
+
} = this.props;
|
|
73
|
+
// re-cache the retrieved options in state in case they have $refs to save doing it later
|
|
74
|
+
const retrievedOptions = options.map((opt: S) => schemaUtils.retrieveSchema(opt, formData));
|
|
75
|
+
newState = { selectedOption, retrievedOptions };
|
|
76
|
+
}
|
|
77
|
+
if (!deepEquals(formData, prevProps.formData) && idSchema.$id === prevProps.idSchema.$id) {
|
|
78
|
+
const { retrievedOptions } = newState;
|
|
79
|
+
const matchingOption = this.getMatchingOption(selectedOption, formData, retrievedOptions);
|
|
80
|
+
|
|
81
|
+
if (prevState && matchingOption !== selectedOption) {
|
|
82
|
+
newState = { selectedOption: matchingOption, retrievedOptions };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (newState !== this.state) {
|
|
86
|
+
this.setState(newState);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Determines the best matching option for the given `formData` and `options`.
|
|
91
|
+
*
|
|
92
|
+
* @param formData - The new formData
|
|
93
|
+
* @param options - The list of options to choose from
|
|
94
|
+
* @return - The index of the `option` that best matches the `formData`
|
|
95
|
+
*/
|
|
96
|
+
getMatchingOption(selectedOption: number, formData: T | undefined, options: S[]) {
|
|
97
|
+
const {
|
|
98
|
+
schema,
|
|
99
|
+
registry: { schemaUtils },
|
|
100
|
+
} = this.props;
|
|
101
|
+
|
|
102
|
+
const discriminator = getDiscriminatorFieldFromSchema<S>(schema);
|
|
103
|
+
const option = schemaUtils.getClosestMatchingOption(formData, options, selectedOption, discriminator);
|
|
104
|
+
return option;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Callback handler to remember what the currently selected option is. In addition to that the `formData` is updated
|
|
108
|
+
* to remove properties that are not part of the newly selected option schema, and then the updated data is passed to
|
|
109
|
+
* the `onChange` handler.
|
|
110
|
+
*
|
|
111
|
+
* @param option - The new option value being selected
|
|
112
|
+
*/
|
|
113
|
+
onOptionChange = (option?: string) => {
|
|
114
|
+
const { selectedOption, retrievedOptions } = this.state;
|
|
115
|
+
const { formData, onChange, registry } = this.props;
|
|
116
|
+
const { schemaUtils } = registry;
|
|
117
|
+
const intOption = option !== undefined ? parseInt(option, 10) : -1;
|
|
118
|
+
if (intOption === selectedOption) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const newOption = intOption >= 0 ? retrievedOptions[intOption] : undefined;
|
|
122
|
+
const oldOption = selectedOption >= 0 ? retrievedOptions[selectedOption] : undefined;
|
|
123
|
+
|
|
124
|
+
let newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
|
|
125
|
+
if (newFormData && newOption) {
|
|
126
|
+
// Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
|
|
127
|
+
// so that only the root objects themselves are created without adding undefined children properties
|
|
128
|
+
newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, 'excludeObjectChildren') as T;
|
|
129
|
+
}
|
|
130
|
+
onChange(newFormData, undefined, this.getFieldId());
|
|
131
|
+
|
|
132
|
+
this.setState({ selectedOption: intOption });
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
getFieldId() {
|
|
136
|
+
const { idSchema, schema } = this.props;
|
|
137
|
+
return `${idSchema.$id}${schema.oneOf ? '__oneof_select' : '__anyof_select'}`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Renders the `AnyOfField` selector along with a `SchemaField` for the value of the `formData`
|
|
141
|
+
*/
|
|
142
|
+
render() {
|
|
143
|
+
const {
|
|
144
|
+
name,
|
|
145
|
+
disabled = false,
|
|
146
|
+
errorSchema = {},
|
|
147
|
+
formContext,
|
|
148
|
+
onBlur,
|
|
149
|
+
onFocus,
|
|
150
|
+
registry,
|
|
151
|
+
schema,
|
|
152
|
+
uiSchema,
|
|
153
|
+
} = this.props;
|
|
154
|
+
|
|
155
|
+
const { widgets, fields, translateString, globalUiOptions, schemaUtils } = registry;
|
|
156
|
+
const { SchemaField: _SchemaField } = fields;
|
|
157
|
+
const { selectedOption, retrievedOptions } = this.state;
|
|
158
|
+
const {
|
|
159
|
+
widget = 'select',
|
|
160
|
+
placeholder,
|
|
161
|
+
autofocus,
|
|
162
|
+
autocomplete,
|
|
163
|
+
title = schema.title,
|
|
164
|
+
...uiOptions
|
|
165
|
+
} = getUiOptions<T, S, F>(uiSchema, globalUiOptions);
|
|
166
|
+
const Widget = getWidget<T, S, F>({ type: 'number' }, widget, widgets);
|
|
167
|
+
const rawErrors = get(errorSchema, ERRORS_KEY, []);
|
|
168
|
+
const fieldErrorSchema = omit(errorSchema, [ERRORS_KEY]);
|
|
169
|
+
const displayLabel = schemaUtils.getDisplayLabel(schema, uiSchema, globalUiOptions);
|
|
170
|
+
|
|
171
|
+
const option = selectedOption >= 0 ? retrievedOptions[selectedOption] || null : null;
|
|
172
|
+
let optionSchema: S;
|
|
173
|
+
|
|
174
|
+
if (option) {
|
|
175
|
+
const { oneOf, anyOf, ...remaining } = schema;
|
|
176
|
+
// Merge in all the non-oneOf/anyOf properties and also skip the special ADDITIONAL_PROPERTY_FLAG property
|
|
177
|
+
unset(remaining, ADDITIONAL_PROPERTY_FLAG);
|
|
178
|
+
optionSchema = !isEmpty(remaining) ? (mergeSchemas(remaining, option) as S) : option;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const translateEnum: TranslatableString = title
|
|
182
|
+
? TranslatableString.TitleOptionPrefix
|
|
183
|
+
: TranslatableString.OptionPrefix;
|
|
184
|
+
const translateParams = title ? [title] : [];
|
|
185
|
+
const enumOptions = retrievedOptions.map((opt: { title?: string }, index: number) => ({
|
|
186
|
+
label: opt.title || translateString(translateEnum, translateParams.concat(String(index + 1))),
|
|
187
|
+
value: index,
|
|
188
|
+
}));
|
|
189
|
+
|
|
190
|
+
return (
|
|
191
|
+
<div className='panel panel-default panel-body'>
|
|
192
|
+
<div className='form-group'>
|
|
193
|
+
<Widget
|
|
194
|
+
id={this.getFieldId()}
|
|
195
|
+
name={`${name}${schema.oneOf ? '__oneof_select' : '__anyof_select'}`}
|
|
196
|
+
schema={{ type: 'number', default: 0 } as S}
|
|
197
|
+
onChange={this.onOptionChange}
|
|
198
|
+
onBlur={onBlur}
|
|
199
|
+
onFocus={onFocus}
|
|
200
|
+
disabled={disabled || isEmpty(enumOptions)}
|
|
201
|
+
multiple={false}
|
|
202
|
+
rawErrors={rawErrors}
|
|
203
|
+
errorSchema={fieldErrorSchema}
|
|
204
|
+
value={selectedOption >= 0 ? selectedOption : undefined}
|
|
205
|
+
options={{ enumOptions, ...uiOptions }}
|
|
206
|
+
registry={registry}
|
|
207
|
+
formContext={formContext}
|
|
208
|
+
placeholder={placeholder}
|
|
209
|
+
autocomplete={autocomplete}
|
|
210
|
+
autofocus={autofocus}
|
|
211
|
+
label={title ?? name}
|
|
212
|
+
hideLabel={!displayLabel}
|
|
213
|
+
/>
|
|
214
|
+
</div>
|
|
215
|
+
{option !== null && <_SchemaField {...this.props} schema={optionSchema!} />}
|
|
216
|
+
</div>
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export default AnyOfField;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { FieldProps, FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
|
|
3
|
+
|
|
4
|
+
/** The `NullField` component is used to render a field in the schema is null. It also ensures that the `formData` is
|
|
5
|
+
* also set to null if it has no value.
|
|
6
|
+
*
|
|
7
|
+
* @param props - The `FieldProps` for this template
|
|
8
|
+
*/
|
|
9
|
+
function NullField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
10
|
+
props: FieldProps<T, S, F>
|
|
11
|
+
) {
|
|
12
|
+
const { formData, onChange } = props;
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (formData === undefined) {
|
|
15
|
+
onChange(null as unknown as T);
|
|
16
|
+
}
|
|
17
|
+
}, [formData, onChange]);
|
|
18
|
+
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default NullField;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { useState, useCallback } from 'react';
|
|
2
|
+
import { asNumber, FieldProps, FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
|
|
3
|
+
|
|
4
|
+
// Matches a string that ends in a . character, optionally followed by a sequence of
|
|
5
|
+
// digits followed by any number of 0 characters up until the end of the line.
|
|
6
|
+
// Ensuring that there is at least one prefixed character is important so that
|
|
7
|
+
// you don't incorrectly match against "0".
|
|
8
|
+
const trailingCharMatcherWithPrefix = /\.([0-9]*0)*$/;
|
|
9
|
+
|
|
10
|
+
// This is used for trimming the trailing 0 and . characters without affecting
|
|
11
|
+
// the rest of the string. Its possible to use one RegEx with groups for this
|
|
12
|
+
// functionality, but it is fairly complex compared to simply defining two
|
|
13
|
+
// different matchers.
|
|
14
|
+
const trailingCharMatcher = /[0.]0*$/;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The NumberField class has some special handling for dealing with trailing
|
|
18
|
+
* decimal points and/or zeroes. This logic is designed to allow trailing values
|
|
19
|
+
* to be visible in the input element, but not be represented in the
|
|
20
|
+
* corresponding form data.
|
|
21
|
+
*
|
|
22
|
+
* The algorithm is as follows:
|
|
23
|
+
*
|
|
24
|
+
* 1. When the input value changes the value is cached in the component state
|
|
25
|
+
*
|
|
26
|
+
* 2. The value is then normalized, removing trailing decimal points and zeros,
|
|
27
|
+
* then passed to the "onChange" callback
|
|
28
|
+
*
|
|
29
|
+
* 3. When the component is rendered, the formData value is checked against the
|
|
30
|
+
* value cached in the state. If it matches the cached value, the cached
|
|
31
|
+
* value is passed to the input instead of the formData value
|
|
32
|
+
*/
|
|
33
|
+
function NumberField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
34
|
+
props: FieldProps<T, S, F>
|
|
35
|
+
) {
|
|
36
|
+
const { registry, onChange, formData, value: initialValue } = props;
|
|
37
|
+
const [lastValue, setLastValue] = useState(initialValue);
|
|
38
|
+
const { StringField } = registry.fields;
|
|
39
|
+
|
|
40
|
+
let value = formData;
|
|
41
|
+
|
|
42
|
+
/** Handle the change from the `StringField` to properly convert to a number
|
|
43
|
+
*
|
|
44
|
+
* @param value - The current value for the change occurring
|
|
45
|
+
*/
|
|
46
|
+
const handleChange = useCallback(
|
|
47
|
+
(value: FieldProps<T, S, F>['value']) => {
|
|
48
|
+
// Cache the original value in component state
|
|
49
|
+
setLastValue(value);
|
|
50
|
+
|
|
51
|
+
// Normalize decimals that don't start with a zero character in advance so
|
|
52
|
+
// that the rest of the normalization logic is simpler
|
|
53
|
+
if (`${value}`.charAt(0) === '.') {
|
|
54
|
+
value = `0${value}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Check that the value is a string (this can happen if the widget used is a
|
|
58
|
+
// <select>, due to an enum declaration etc) then, if the value ends in a
|
|
59
|
+
// trailing decimal point or multiple zeroes, strip the trailing values
|
|
60
|
+
const processed =
|
|
61
|
+
typeof value === 'string' && value.match(trailingCharMatcherWithPrefix)
|
|
62
|
+
? asNumber(value.replace(trailingCharMatcher, ''))
|
|
63
|
+
: asNumber(value);
|
|
64
|
+
|
|
65
|
+
onChange(processed as unknown as T);
|
|
66
|
+
},
|
|
67
|
+
[onChange]
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
if (typeof lastValue === 'string' && typeof value === 'number') {
|
|
71
|
+
// Construct a regular expression that checks for a string that consists
|
|
72
|
+
// of the formData value suffixed with zero or one '.' characters and zero
|
|
73
|
+
// or more '0' characters
|
|
74
|
+
const re = new RegExp(`${value}`.replace('.', '\\.') + '\\.?0*$');
|
|
75
|
+
|
|
76
|
+
// If the cached "lastValue" is a match, use that instead of the formData
|
|
77
|
+
// value to prevent the input value from changing in the UI
|
|
78
|
+
if (lastValue.match(re)) {
|
|
79
|
+
value = lastValue as unknown as T;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return <StringField {...props} formData={value} onChange={handleChange} />;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export default NumberField;
|