@rjsf/core 6.0.0-beta.8 → 6.0.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/README.md +2 -0
- package/dist/core.umd.js +2042 -1987
- package/dist/index.cjs +4909 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.esm.js +2509 -2389
- package/dist/index.esm.js.map +4 -4
- package/lib/components/Form.d.ts +137 -34
- package/lib/components/Form.d.ts.map +1 -1
- package/lib/components/Form.js +318 -173
- package/lib/components/fields/ArrayField.d.ts +2 -187
- package/lib/components/fields/ArrayField.d.ts.map +1 -1
- package/lib/components/fields/ArrayField.js +526 -492
- package/lib/components/fields/BooleanField.d.ts.map +1 -1
- package/lib/components/fields/BooleanField.js +8 -3
- package/lib/components/fields/FallbackField.d.ts +7 -0
- package/lib/components/fields/FallbackField.d.ts.map +1 -0
- package/lib/components/fields/FallbackField.js +72 -0
- package/lib/components/fields/LayoutGridField.d.ts +109 -186
- package/lib/components/fields/LayoutGridField.d.ts.map +1 -1
- package/lib/components/fields/LayoutGridField.js +426 -426
- package/lib/components/fields/LayoutHeaderField.d.ts +1 -1
- package/lib/components/fields/LayoutHeaderField.js +3 -3
- package/lib/components/fields/LayoutMultiSchemaField.d.ts.map +1 -1
- package/lib/components/fields/LayoutMultiSchemaField.js +6 -6
- package/lib/components/fields/MultiSchemaField.d.ts.map +1 -1
- package/lib/components/fields/MultiSchemaField.js +16 -10
- package/lib/components/fields/NullField.js +3 -3
- package/lib/components/fields/NumberField.d.ts.map +1 -1
- package/lib/components/fields/NumberField.js +3 -3
- package/lib/components/fields/ObjectField.d.ts +2 -68
- package/lib/components/fields/ObjectField.d.ts.map +1 -1
- package/lib/components/fields/ObjectField.js +163 -163
- package/lib/components/fields/OptionalDataControlsField.d.ts +8 -0
- package/lib/components/fields/OptionalDataControlsField.d.ts.map +1 -0
- package/lib/components/fields/OptionalDataControlsField.js +43 -0
- package/lib/components/fields/SchemaField.d.ts.map +1 -1
- package/lib/components/fields/SchemaField.js +52 -30
- package/lib/components/fields/StringField.d.ts.map +1 -1
- package/lib/components/fields/StringField.js +8 -3
- package/lib/components/fields/index.d.ts.map +1 -1
- package/lib/components/fields/index.js +4 -0
- package/lib/components/templates/ArrayFieldDescriptionTemplate.d.ts +1 -1
- package/lib/components/templates/ArrayFieldDescriptionTemplate.js +3 -3
- package/lib/components/templates/ArrayFieldItemButtonsTemplate.d.ts +3 -3
- package/lib/components/templates/ArrayFieldItemButtonsTemplate.d.ts.map +1 -1
- package/lib/components/templates/ArrayFieldItemButtonsTemplate.js +3 -8
- package/lib/components/templates/ArrayFieldItemTemplate.d.ts +3 -3
- 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 +1 -1
- package/lib/components/templates/ArrayFieldTemplate.d.ts.map +1 -1
- package/lib/components/templates/ArrayFieldTemplate.js +4 -5
- package/lib/components/templates/ArrayFieldTitleTemplate.d.ts +1 -1
- package/lib/components/templates/ArrayFieldTitleTemplate.d.ts.map +1 -1
- package/lib/components/templates/ArrayFieldTitleTemplate.js +3 -3
- package/lib/components/templates/BaseInputTemplate.js +2 -2
- package/lib/components/templates/ButtonTemplates/AddButton.d.ts +1 -1
- package/lib/components/templates/ButtonTemplates/AddButton.d.ts.map +1 -1
- package/lib/components/templates/ButtonTemplates/AddButton.js +2 -2
- package/lib/components/templates/FallbackFieldTemplate.d.ts +7 -0
- package/lib/components/templates/FallbackFieldTemplate.d.ts.map +1 -0
- package/lib/components/templates/FallbackFieldTemplate.js +12 -0
- package/lib/components/templates/FieldErrorTemplate.js +2 -2
- package/lib/components/templates/FieldHelpTemplate.js +2 -2
- package/lib/components/templates/MultiSchemaFieldTemplate.d.ts +8 -0
- package/lib/components/templates/MultiSchemaFieldTemplate.d.ts.map +1 -0
- package/lib/components/templates/MultiSchemaFieldTemplate.js +10 -0
- package/lib/components/templates/ObjectFieldTemplate.d.ts.map +1 -1
- package/lib/components/templates/ObjectFieldTemplate.js +3 -2
- package/lib/components/templates/OptionalDataControlsTemplate.d.ts +11 -0
- package/lib/components/templates/OptionalDataControlsTemplate.d.ts.map +1 -0
- package/lib/components/templates/OptionalDataControlsTemplate.js +20 -0
- package/lib/components/templates/TitleField.d.ts.map +1 -1
- package/lib/components/templates/TitleField.js +2 -2
- package/lib/components/templates/UnsupportedField.js +3 -3
- package/lib/components/templates/WrapIfAdditionalTemplate.js +2 -2
- package/lib/components/templates/index.d.ts.map +1 -1
- package/lib/components/templates/index.js +6 -0
- package/lib/components/widgets/AltDateWidget.d.ts +1 -1
- package/lib/components/widgets/AltDateWidget.d.ts.map +1 -1
- package/lib/components/widgets/AltDateWidget.js +5 -46
- package/lib/components/widgets/CheckboxWidget.d.ts +1 -1
- package/lib/components/widgets/CheckboxWidget.d.ts.map +1 -1
- package/lib/components/widgets/CheckboxWidget.js +2 -2
- package/lib/components/widgets/CheckboxesWidget.d.ts +1 -1
- package/lib/components/widgets/CheckboxesWidget.d.ts.map +1 -1
- package/lib/components/widgets/CheckboxesWidget.js +4 -4
- package/lib/components/widgets/FileWidget.d.ts.map +1 -1
- package/lib/components/widgets/FileWidget.js +7 -87
- package/lib/components/widgets/HiddenWidget.d.ts +1 -1
- package/lib/components/widgets/HiddenWidget.d.ts.map +1 -1
- package/lib/components/widgets/HiddenWidget.js +2 -2
- package/lib/components/widgets/RadioWidget.d.ts +1 -1
- package/lib/components/widgets/RadioWidget.d.ts.map +1 -1
- package/lib/components/widgets/RadioWidget.js +2 -2
- package/lib/components/widgets/RatingWidget.d.ts +1 -1
- package/lib/components/widgets/RatingWidget.d.ts.map +1 -1
- package/lib/components/widgets/RatingWidget.js +2 -2
- package/lib/components/widgets/SelectWidget.d.ts +1 -1
- package/lib/components/widgets/SelectWidget.d.ts.map +1 -1
- package/lib/components/widgets/SelectWidget.js +2 -2
- package/lib/components/widgets/TextareaWidget.d.ts +1 -1
- package/lib/components/widgets/TextareaWidget.d.ts.map +1 -1
- package/lib/components/widgets/TextareaWidget.js +2 -2
- package/lib/getDefaultRegistry.d.ts.map +1 -1
- package/lib/getDefaultRegistry.js +6 -1
- package/lib/getTestRegistry.d.ts +5 -0
- package/lib/getTestRegistry.d.ts.map +1 -0
- package/lib/getTestRegistry.js +23 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +35 -20
- package/src/components/Form.tsx +468 -206
- package/src/components/fields/ArrayField.tsx +871 -723
- package/src/components/fields/BooleanField.tsx +14 -5
- package/src/components/fields/FallbackField.tsx +157 -0
- package/src/components/fields/LayoutGridField.tsx +626 -603
- package/src/components/fields/LayoutHeaderField.tsx +3 -3
- package/src/components/fields/LayoutMultiSchemaField.tsx +9 -10
- package/src/components/fields/MultiSchemaField.tsx +57 -36
- package/src/components/fields/NullField.tsx +3 -3
- package/src/components/fields/NumberField.tsx +11 -3
- package/src/components/fields/ObjectField.tsx +308 -239
- package/src/components/fields/OptionalDataControlsField.tsx +84 -0
- package/src/components/fields/SchemaField.tsx +75 -94
- package/src/components/fields/StringField.tsx +14 -5
- package/src/components/fields/index.ts +4 -0
- package/src/components/templates/ArrayFieldDescriptionTemplate.tsx +3 -3
- package/src/components/templates/ArrayFieldItemButtonsTemplate.tsx +16 -21
- package/src/components/templates/ArrayFieldItemTemplate.tsx +3 -3
- package/src/components/templates/ArrayFieldTemplate.tsx +11 -18
- package/src/components/templates/ArrayFieldTitleTemplate.tsx +4 -3
- package/src/components/templates/BaseInputTemplate.tsx +5 -5
- package/src/components/templates/ButtonTemplates/AddButton.tsx +2 -0
- package/src/components/templates/FallbackFieldTemplate.tsx +28 -0
- package/src/components/templates/FieldErrorTemplate.tsx +2 -2
- package/src/components/templates/FieldHelpTemplate.tsx +2 -2
- package/src/components/templates/MultiSchemaFieldTemplate.tsx +20 -0
- package/src/components/templates/ObjectFieldTemplate.tsx +12 -7
- package/src/components/templates/OptionalDataControlsTemplate.tsx +43 -0
- package/src/components/templates/TitleField.tsx +6 -1
- package/src/components/templates/UnsupportedField.tsx +3 -3
- package/src/components/templates/WrapIfAdditionalTemplate.tsx +5 -5
- package/src/components/templates/index.ts +6 -0
- package/src/components/widgets/AltDateWidget.tsx +8 -126
- package/src/components/widgets/CheckboxWidget.tsx +4 -3
- package/src/components/widgets/CheckboxesWidget.tsx +5 -4
- package/src/components/widgets/FileWidget.tsx +11 -102
- package/src/components/widgets/HiddenWidget.tsx +2 -1
- package/src/components/widgets/RadioWidget.tsx +3 -2
- package/src/components/widgets/RatingWidget.tsx +2 -1
- package/src/components/widgets/SelectWidget.tsx +3 -2
- package/src/components/widgets/TextareaWidget.tsx +3 -2
- package/src/getDefaultRegistry.ts +14 -1
- package/src/getTestRegistry.tsx +38 -0
- package/src/index.ts +2 -1
- package/dist/index.js +0 -4834
- package/dist/index.js.map +0 -7
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
1
2
|
import {
|
|
2
3
|
getWidget,
|
|
3
4
|
getUiOptions,
|
|
@@ -5,6 +6,7 @@ import {
|
|
|
5
6
|
FieldProps,
|
|
6
7
|
FormContextType,
|
|
7
8
|
EnumOptionsType,
|
|
9
|
+
ErrorSchema,
|
|
8
10
|
RJSFSchema,
|
|
9
11
|
StrictRJSFSchema,
|
|
10
12
|
TranslatableString,
|
|
@@ -23,7 +25,7 @@ function BooleanField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend
|
|
|
23
25
|
schema,
|
|
24
26
|
name,
|
|
25
27
|
uiSchema,
|
|
26
|
-
|
|
28
|
+
fieldPathId,
|
|
27
29
|
formData,
|
|
28
30
|
registry,
|
|
29
31
|
required,
|
|
@@ -38,7 +40,7 @@ function BooleanField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend
|
|
|
38
40
|
rawErrors,
|
|
39
41
|
} = props;
|
|
40
42
|
const { title: schemaTitle } = schema;
|
|
41
|
-
const { widgets,
|
|
43
|
+
const { widgets, translateString, globalUiOptions } = registry;
|
|
42
44
|
const {
|
|
43
45
|
widget = 'checkbox',
|
|
44
46
|
title: uiTitle,
|
|
@@ -86,15 +88,22 @@ function BooleanField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend
|
|
|
86
88
|
enumOptions = optionsList<T, S, F>({ enum: enums } as S, uiSchema);
|
|
87
89
|
}
|
|
88
90
|
}
|
|
91
|
+
const onWidgetChange = useCallback(
|
|
92
|
+
(value: T | undefined, errorSchema?: ErrorSchema, id?: string) => {
|
|
93
|
+
// Boolean field change passes an empty path array to the parent field which adds the appropriate path
|
|
94
|
+
return onChange(value, fieldPathId.path, errorSchema, id);
|
|
95
|
+
},
|
|
96
|
+
[onChange, fieldPathId],
|
|
97
|
+
);
|
|
89
98
|
|
|
90
99
|
return (
|
|
91
100
|
<Widget
|
|
92
101
|
options={{ ...options, enumOptions }}
|
|
93
102
|
schema={schema}
|
|
94
103
|
uiSchema={uiSchema}
|
|
95
|
-
id={
|
|
104
|
+
id={fieldPathId.$id}
|
|
96
105
|
name={name}
|
|
97
|
-
onChange={
|
|
106
|
+
onChange={onWidgetChange}
|
|
98
107
|
onFocus={onFocus}
|
|
99
108
|
onBlur={onBlur}
|
|
100
109
|
label={label}
|
|
@@ -105,9 +114,9 @@ function BooleanField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend
|
|
|
105
114
|
readonly={readonly}
|
|
106
115
|
hideError={hideError}
|
|
107
116
|
registry={registry}
|
|
108
|
-
formContext={formContext}
|
|
109
117
|
autofocus={autofocus}
|
|
110
118
|
rawErrors={rawErrors}
|
|
119
|
+
htmlName={fieldPathId.name}
|
|
111
120
|
/>
|
|
112
121
|
);
|
|
113
122
|
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FallbackFieldProps,
|
|
3
|
+
FieldPathId,
|
|
4
|
+
FormContextType,
|
|
5
|
+
getTemplate,
|
|
6
|
+
getUiOptions,
|
|
7
|
+
hashObject,
|
|
8
|
+
RJSFSchema,
|
|
9
|
+
StrictRJSFSchema,
|
|
10
|
+
toFieldPathId,
|
|
11
|
+
TranslatableString,
|
|
12
|
+
useDeepCompareMemo,
|
|
13
|
+
} from '@rjsf/utils';
|
|
14
|
+
import { useMemo, useState } from 'react';
|
|
15
|
+
import { JSONSchema7TypeName } from 'json-schema';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Get the schema for the type selection component.
|
|
19
|
+
* @param title - The translated title for the type selection schema.
|
|
20
|
+
*/
|
|
21
|
+
function getFallbackTypeSelectionSchema(title: string): RJSFSchema {
|
|
22
|
+
return {
|
|
23
|
+
type: 'string',
|
|
24
|
+
enum: ['string', 'number', 'boolean'],
|
|
25
|
+
default: 'string',
|
|
26
|
+
title: title,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Determines the JSON Schema type of the given formData.
|
|
32
|
+
* @param formData - The form data whose type is to be determined.
|
|
33
|
+
*/
|
|
34
|
+
function getTypeOfFormData(formData: any): JSONSchema7TypeName {
|
|
35
|
+
const dataType = typeof formData;
|
|
36
|
+
if (dataType === 'string' || dataType === 'number' || dataType === 'boolean') {
|
|
37
|
+
return dataType;
|
|
38
|
+
}
|
|
39
|
+
// Treat everything else as a string
|
|
40
|
+
return 'string';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Casts the given formData to the specified type.
|
|
45
|
+
* @param formData - The form data to be casted.
|
|
46
|
+
* @param newType - The target type to which the form data should be casted.
|
|
47
|
+
*/
|
|
48
|
+
function castToNewType<T = any>(formData: T, newType: JSONSchema7TypeName): T {
|
|
49
|
+
switch (newType) {
|
|
50
|
+
case 'string':
|
|
51
|
+
return String(formData) as T;
|
|
52
|
+
case 'number': {
|
|
53
|
+
const castedNumber = Number(formData);
|
|
54
|
+
return (isNaN(castedNumber) ? 0 : castedNumber) as T;
|
|
55
|
+
}
|
|
56
|
+
case 'boolean':
|
|
57
|
+
return Boolean(formData) as T;
|
|
58
|
+
default:
|
|
59
|
+
return formData;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The `FallbackField` component is used to render a field for unsupported or unknown schema types. If
|
|
65
|
+
* `useFallbackUiForUnsupportedType` is enabled in the `globalUiOptions`, it provides a type selector
|
|
66
|
+
*/
|
|
67
|
+
export default function FallbackField<
|
|
68
|
+
T = any,
|
|
69
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
70
|
+
F extends FormContextType = any,
|
|
71
|
+
>(props: FallbackFieldProps<T, S, F>) {
|
|
72
|
+
const {
|
|
73
|
+
id,
|
|
74
|
+
formData,
|
|
75
|
+
displayLabel = true,
|
|
76
|
+
schema,
|
|
77
|
+
name,
|
|
78
|
+
uiSchema,
|
|
79
|
+
required,
|
|
80
|
+
disabled = false,
|
|
81
|
+
readonly = false,
|
|
82
|
+
onBlur,
|
|
83
|
+
onFocus,
|
|
84
|
+
registry,
|
|
85
|
+
fieldPathId,
|
|
86
|
+
onChange,
|
|
87
|
+
errorSchema,
|
|
88
|
+
} = props;
|
|
89
|
+
const { translateString, fields, globalFormOptions } = registry;
|
|
90
|
+
const [type, setType] = useState<JSONSchema7TypeName>(getTypeOfFormData(formData));
|
|
91
|
+
|
|
92
|
+
const uiOptions = getUiOptions<T, S, F>(uiSchema);
|
|
93
|
+
|
|
94
|
+
const typeSelectorInnerFieldPathId = useDeepCompareMemo<FieldPathId>(
|
|
95
|
+
toFieldPathId('__internal_type_selector', globalFormOptions, fieldPathId),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const schemaTitle = translateString(TranslatableString.Type);
|
|
99
|
+
const typesOptionSchema = useMemo(() => getFallbackTypeSelectionSchema(schemaTitle), [schemaTitle]);
|
|
100
|
+
|
|
101
|
+
const onTypeChange = (newType: T | undefined) => {
|
|
102
|
+
if (newType != null) {
|
|
103
|
+
setType(newType as JSONSchema7TypeName);
|
|
104
|
+
onChange(castToNewType<T>(formData as T, newType as JSONSchema7TypeName), fieldPathId.path, errorSchema, id);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
if (!globalFormOptions.useFallbackUiForUnsupportedType) {
|
|
109
|
+
const UnsupportedFieldTemplate = getTemplate<'UnsupportedFieldTemplate', T, S, F>(
|
|
110
|
+
'UnsupportedFieldTemplate',
|
|
111
|
+
registry,
|
|
112
|
+
uiOptions,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<UnsupportedFieldTemplate
|
|
117
|
+
schema={schema}
|
|
118
|
+
fieldPathId={fieldPathId}
|
|
119
|
+
reason={translateString(TranslatableString.UnknownFieldType, [String(schema.type)])}
|
|
120
|
+
registry={registry}
|
|
121
|
+
/>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const FallbackFieldTemplate = getTemplate<'FallbackFieldTemplate', T, S, F>(
|
|
126
|
+
'FallbackFieldTemplate',
|
|
127
|
+
registry,
|
|
128
|
+
uiOptions,
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const { SchemaField } = fields;
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<FallbackFieldTemplate
|
|
135
|
+
schema={schema}
|
|
136
|
+
registry={registry}
|
|
137
|
+
typeSelector={
|
|
138
|
+
<SchemaField
|
|
139
|
+
key={formData ? hashObject(formData) : '__empty__'}
|
|
140
|
+
fieldPathId={typeSelectorInnerFieldPathId}
|
|
141
|
+
name={`${name}__fallback_type`}
|
|
142
|
+
schema={typesOptionSchema as S}
|
|
143
|
+
formData={type as T}
|
|
144
|
+
onChange={onTypeChange}
|
|
145
|
+
onBlur={onBlur}
|
|
146
|
+
onFocus={onFocus}
|
|
147
|
+
registry={registry}
|
|
148
|
+
hideLabel={!displayLabel}
|
|
149
|
+
disabled={disabled}
|
|
150
|
+
readonly={readonly}
|
|
151
|
+
required={required}
|
|
152
|
+
/>
|
|
153
|
+
}
|
|
154
|
+
schemaField={<SchemaField {...props} schema={{ type, title: translateString(TranslatableString.Value) } as S} />}
|
|
155
|
+
/>
|
|
156
|
+
);
|
|
157
|
+
}
|