@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.
Files changed (262) hide show
  1. package/LICENSE.md +201 -0
  2. package/dist/core.umd.js +694 -584
  3. package/dist/index.cjs +1191 -1071
  4. package/dist/index.cjs.map +4 -4
  5. package/dist/index.esm.js +1030 -984
  6. package/dist/index.esm.js.map +4 -4
  7. package/lib/components/Form.d.ts +16 -13
  8. package/lib/components/Form.d.ts.map +1 -1
  9. package/lib/components/Form.js +126 -76
  10. package/lib/components/RichDescription.d.ts +3 -3
  11. package/lib/components/RichDescription.d.ts.map +1 -1
  12. package/lib/components/RichDescription.js +1 -1
  13. package/lib/components/RichHelp.d.ts +3 -3
  14. package/lib/components/RichHelp.d.ts.map +1 -1
  15. package/lib/components/RichHelp.js +1 -1
  16. package/lib/components/SchemaExamples.d.ts +2 -2
  17. package/lib/components/SchemaExamples.d.ts.map +1 -1
  18. package/lib/components/SchemaExamples.js +1 -3
  19. package/lib/components/fields/ArrayField.d.ts +2 -2
  20. package/lib/components/fields/ArrayField.d.ts.map +1 -1
  21. package/lib/components/fields/ArrayField.js +87 -88
  22. package/lib/components/fields/BooleanField.d.ts +2 -2
  23. package/lib/components/fields/BooleanField.d.ts.map +1 -1
  24. package/lib/components/fields/BooleanField.js +2 -5
  25. package/lib/components/fields/CyclicSchemaField.d.ts +8 -0
  26. package/lib/components/fields/CyclicSchemaField.d.ts.map +1 -0
  27. package/lib/components/fields/CyclicSchemaField.js +21 -0
  28. package/lib/components/fields/FallbackField.d.ts +2 -2
  29. package/lib/components/fields/FallbackField.d.ts.map +1 -1
  30. package/lib/components/fields/FallbackField.js +2 -2
  31. package/lib/components/fields/LayoutGridField.d.ts +8 -10
  32. package/lib/components/fields/LayoutGridField.d.ts.map +1 -1
  33. package/lib/components/fields/LayoutGridField.js +5 -4
  34. package/lib/components/fields/LayoutHeaderField.d.ts +2 -2
  35. package/lib/components/fields/LayoutHeaderField.d.ts.map +1 -1
  36. package/lib/components/fields/LayoutHeaderField.js +1 -1
  37. package/lib/components/fields/LayoutMultiSchemaField.d.ts +2 -2
  38. package/lib/components/fields/LayoutMultiSchemaField.d.ts.map +1 -1
  39. package/lib/components/fields/LayoutMultiSchemaField.js +4 -1
  40. package/lib/components/fields/MultiSchemaField.d.ts +2 -41
  41. package/lib/components/fields/MultiSchemaField.d.ts.map +1 -1
  42. package/lib/components/fields/MultiSchemaField.js +116 -122
  43. package/lib/components/fields/NullField.d.ts +1 -1
  44. package/lib/components/fields/NullField.d.ts.map +1 -1
  45. package/lib/components/fields/NumberField.d.ts +2 -2
  46. package/lib/components/fields/NumberField.d.ts.map +1 -1
  47. package/lib/components/fields/NumberField.js +7 -9
  48. package/lib/components/fields/ObjectField.d.ts +2 -2
  49. package/lib/components/fields/ObjectField.d.ts.map +1 -1
  50. package/lib/components/fields/ObjectField.js +28 -26
  51. package/lib/components/fields/OptionalDataControlsField.d.ts +2 -2
  52. package/lib/components/fields/OptionalDataControlsField.d.ts.map +1 -1
  53. package/lib/components/fields/SchemaField.d.ts +14 -8
  54. package/lib/components/fields/SchemaField.d.ts.map +1 -1
  55. package/lib/components/fields/SchemaField.js +43 -30
  56. package/lib/components/fields/StringField.d.ts +2 -2
  57. package/lib/components/fields/StringField.d.ts.map +1 -1
  58. package/lib/components/fields/StringField.js +2 -5
  59. package/lib/components/fields/index.d.ts +1 -1
  60. package/lib/components/fields/index.d.ts.map +1 -1
  61. package/lib/components/fields/index.js +2 -0
  62. package/lib/components/templates/ArrayFieldDescriptionTemplate.d.ts +2 -2
  63. package/lib/components/templates/ArrayFieldDescriptionTemplate.d.ts.map +1 -1
  64. package/lib/components/templates/ArrayFieldDescriptionTemplate.js +1 -1
  65. package/lib/components/templates/ArrayFieldItemButtonsTemplate.d.ts +2 -2
  66. package/lib/components/templates/ArrayFieldItemButtonsTemplate.d.ts.map +1 -1
  67. package/lib/components/templates/ArrayFieldItemButtonsTemplate.js +1 -1
  68. package/lib/components/templates/ArrayFieldItemTemplate.d.ts +2 -2
  69. package/lib/components/templates/ArrayFieldItemTemplate.d.ts.map +1 -1
  70. package/lib/components/templates/ArrayFieldItemTemplate.js +1 -1
  71. package/lib/components/templates/ArrayFieldTemplate.d.ts +2 -2
  72. package/lib/components/templates/ArrayFieldTemplate.d.ts.map +1 -1
  73. package/lib/components/templates/ArrayFieldTemplate.js +1 -1
  74. package/lib/components/templates/ArrayFieldTitleTemplate.d.ts +2 -2
  75. package/lib/components/templates/ArrayFieldTitleTemplate.d.ts.map +1 -1
  76. package/lib/components/templates/ArrayFieldTitleTemplate.js +1 -1
  77. package/lib/components/templates/BaseInputTemplate.d.ts +2 -2
  78. package/lib/components/templates/BaseInputTemplate.d.ts.map +1 -1
  79. package/lib/components/templates/BaseInputTemplate.js +7 -6
  80. package/lib/components/templates/ButtonTemplates/AddButton.d.ts +2 -2
  81. package/lib/components/templates/ButtonTemplates/AddButton.d.ts.map +1 -1
  82. package/lib/components/templates/ButtonTemplates/IconButton.d.ts +14 -7
  83. package/lib/components/templates/ButtonTemplates/IconButton.d.ts.map +1 -1
  84. package/lib/components/templates/ButtonTemplates/IconButton.js +14 -6
  85. package/lib/components/templates/ButtonTemplates/SubmitButton.d.ts +2 -2
  86. package/lib/components/templates/ButtonTemplates/SubmitButton.d.ts.map +1 -1
  87. package/lib/components/templates/ButtonTemplates/index.d.ts +1 -1
  88. package/lib/components/templates/ButtonTemplates/index.d.ts.map +1 -1
  89. package/lib/components/templates/CyclicSchemaExpandTemplate.d.ts +7 -0
  90. package/lib/components/templates/CyclicSchemaExpandTemplate.d.ts.map +1 -0
  91. package/lib/components/templates/CyclicSchemaExpandTemplate.js +12 -0
  92. package/lib/components/templates/DescriptionField.d.ts +2 -2
  93. package/lib/components/templates/DescriptionField.d.ts.map +1 -1
  94. package/lib/components/templates/ErrorList.d.ts +2 -2
  95. package/lib/components/templates/ErrorList.d.ts.map +1 -1
  96. package/lib/components/templates/ErrorList.js +2 -4
  97. package/lib/components/templates/FallbackFieldTemplate.d.ts +2 -2
  98. package/lib/components/templates/FallbackFieldTemplate.d.ts.map +1 -1
  99. package/lib/components/templates/FieldErrorTemplate.d.ts +2 -2
  100. package/lib/components/templates/FieldErrorTemplate.d.ts.map +1 -1
  101. package/lib/components/templates/FieldErrorTemplate.js +1 -3
  102. package/lib/components/templates/FieldHelpTemplate.d.ts +2 -2
  103. package/lib/components/templates/FieldHelpTemplate.d.ts.map +1 -1
  104. package/lib/components/templates/FieldTemplate/FieldTemplate.d.ts +2 -2
  105. package/lib/components/templates/FieldTemplate/FieldTemplate.d.ts.map +1 -1
  106. package/lib/components/templates/FieldTemplate/FieldTemplate.js +1 -1
  107. package/lib/components/templates/FieldTemplate/Label.d.ts +3 -3
  108. package/lib/components/templates/FieldTemplate/Label.d.ts.map +1 -1
  109. package/lib/components/templates/GridTemplate.d.ts +2 -2
  110. package/lib/components/templates/GridTemplate.d.ts.map +1 -1
  111. package/lib/components/templates/MultiSchemaFieldTemplate.d.ts +2 -2
  112. package/lib/components/templates/MultiSchemaFieldTemplate.d.ts.map +1 -1
  113. package/lib/components/templates/ObjectFieldTemplate.d.ts +2 -2
  114. package/lib/components/templates/ObjectFieldTemplate.d.ts.map +1 -1
  115. package/lib/components/templates/ObjectFieldTemplate.js +1 -1
  116. package/lib/components/templates/OptionalDataControlsTemplate.d.ts +2 -2
  117. package/lib/components/templates/OptionalDataControlsTemplate.d.ts.map +1 -1
  118. package/lib/components/templates/OptionalDataControlsTemplate.js +1 -1
  119. package/lib/components/templates/TitleField.d.ts +2 -2
  120. package/lib/components/templates/TitleField.d.ts.map +1 -1
  121. package/lib/components/templates/UnsupportedField.d.ts +2 -2
  122. package/lib/components/templates/UnsupportedField.d.ts.map +1 -1
  123. package/lib/components/templates/UnsupportedField.js +2 -2
  124. package/lib/components/templates/WrapIfAdditionalTemplate.d.ts +2 -2
  125. package/lib/components/templates/WrapIfAdditionalTemplate.d.ts.map +1 -1
  126. package/lib/components/templates/WrapIfAdditionalTemplate.js +1 -1
  127. package/lib/components/templates/index.d.ts +1 -1
  128. package/lib/components/templates/index.d.ts.map +1 -1
  129. package/lib/components/templates/index.js +2 -0
  130. package/lib/components/widgets/AltDateTimeWidget.d.ts +2 -2
  131. package/lib/components/widgets/AltDateTimeWidget.d.ts.map +1 -1
  132. package/lib/components/widgets/AltDateWidget.d.ts +2 -2
  133. package/lib/components/widgets/AltDateWidget.d.ts.map +1 -1
  134. package/lib/components/widgets/AltDateWidget.js +2 -2
  135. package/lib/components/widgets/CheckboxWidget.d.ts +2 -2
  136. package/lib/components/widgets/CheckboxWidget.d.ts.map +1 -1
  137. package/lib/components/widgets/CheckboxesWidget.d.ts +2 -2
  138. package/lib/components/widgets/CheckboxesWidget.d.ts.map +1 -1
  139. package/lib/components/widgets/CheckboxesWidget.js +4 -4
  140. package/lib/components/widgets/ColorWidget.d.ts +2 -2
  141. package/lib/components/widgets/ColorWidget.d.ts.map +1 -1
  142. package/lib/components/widgets/DateTimeWidget.d.ts +2 -2
  143. package/lib/components/widgets/DateTimeWidget.d.ts.map +1 -1
  144. package/lib/components/widgets/DateTimeWidget.js +2 -2
  145. package/lib/components/widgets/DateWidget.d.ts +2 -2
  146. package/lib/components/widgets/DateWidget.d.ts.map +1 -1
  147. package/lib/components/widgets/EmailWidget.d.ts +2 -2
  148. package/lib/components/widgets/EmailWidget.d.ts.map +1 -1
  149. package/lib/components/widgets/FileWidget.d.ts +2 -2
  150. package/lib/components/widgets/FileWidget.d.ts.map +1 -1
  151. package/lib/components/widgets/FileWidget.js +6 -4
  152. package/lib/components/widgets/HiddenWidget.d.ts +2 -2
  153. package/lib/components/widgets/HiddenWidget.d.ts.map +1 -1
  154. package/lib/components/widgets/PasswordWidget.d.ts +2 -2
  155. package/lib/components/widgets/PasswordWidget.d.ts.map +1 -1
  156. package/lib/components/widgets/RadioWidget.d.ts +2 -2
  157. package/lib/components/widgets/RadioWidget.d.ts.map +1 -1
  158. package/lib/components/widgets/RadioWidget.js +4 -4
  159. package/lib/components/widgets/RangeWidget.d.ts +2 -2
  160. package/lib/components/widgets/RangeWidget.d.ts.map +1 -1
  161. package/lib/components/widgets/RatingWidget.d.ts +2 -2
  162. package/lib/components/widgets/RatingWidget.d.ts.map +1 -1
  163. package/lib/components/widgets/RatingWidget.js +15 -15
  164. package/lib/components/widgets/SelectWidget.d.ts +2 -2
  165. package/lib/components/widgets/SelectWidget.d.ts.map +1 -1
  166. package/lib/components/widgets/SelectWidget.js +9 -8
  167. package/lib/components/widgets/TextWidget.d.ts +2 -2
  168. package/lib/components/widgets/TextWidget.d.ts.map +1 -1
  169. package/lib/components/widgets/TextareaWidget.d.ts +2 -2
  170. package/lib/components/widgets/TextareaWidget.d.ts.map +1 -1
  171. package/lib/components/widgets/TextareaWidget.js +5 -5
  172. package/lib/components/widgets/TimeWidget.d.ts +2 -2
  173. package/lib/components/widgets/TimeWidget.d.ts.map +1 -1
  174. package/lib/components/widgets/URLWidget.d.ts +2 -2
  175. package/lib/components/widgets/URLWidget.d.ts.map +1 -1
  176. package/lib/components/widgets/UpDownWidget.d.ts +2 -2
  177. package/lib/components/widgets/UpDownWidget.d.ts.map +1 -1
  178. package/lib/components/widgets/index.d.ts +1 -1
  179. package/lib/components/widgets/index.d.ts.map +1 -1
  180. package/lib/getDefaultRegistry.d.ts +1 -1
  181. package/lib/getDefaultRegistry.d.ts.map +1 -1
  182. package/lib/getDefaultRegistry.js +1 -1
  183. package/lib/getTestRegistry.d.ts +1 -1
  184. package/lib/getTestRegistry.d.ts.map +1 -1
  185. package/lib/getTestRegistry.js +1 -1
  186. package/lib/index.d.ts +10 -5
  187. package/lib/index.d.ts.map +1 -1
  188. package/lib/tsconfig.tsbuildinfo +1 -1
  189. package/lib/withTheme.d.ts +3 -3
  190. package/lib/withTheme.d.ts.map +1 -1
  191. package/lib/withTheme.js +6 -6
  192. package/package.json +28 -28
  193. package/src/components/Form.tsx +197 -111
  194. package/src/components/RichDescription.tsx +3 -10
  195. package/src/components/RichHelp.tsx +3 -10
  196. package/src/components/SchemaExamples.tsx +6 -4
  197. package/src/components/fields/ArrayField.tsx +122 -108
  198. package/src/components/fields/BooleanField.tsx +4 -9
  199. package/src/components/fields/CyclicSchemaField.tsx +40 -0
  200. package/src/components/fields/FallbackField.tsx +4 -8
  201. package/src/components/fields/LayoutGridField.tsx +21 -19
  202. package/src/components/fields/LayoutHeaderField.tsx +2 -10
  203. package/src/components/fields/LayoutMultiSchemaField.tsx +15 -10
  204. package/src/components/fields/MultiSchemaField.tsx +202 -218
  205. package/src/components/fields/NullField.tsx +1 -1
  206. package/src/components/fields/NumberField.tsx +8 -10
  207. package/src/components/fields/ObjectField.tsx +52 -40
  208. package/src/components/fields/OptionalDataControlsField.tsx +6 -4
  209. package/src/components/fields/SchemaField.tsx +66 -51
  210. package/src/components/fields/StringField.tsx +4 -15
  211. package/src/components/fields/index.ts +3 -1
  212. package/src/components/templates/ArrayFieldDescriptionTemplate.tsx +2 -9
  213. package/src/components/templates/ArrayFieldItemButtonsTemplate.tsx +2 -7
  214. package/src/components/templates/ArrayFieldItemTemplate.tsx +3 -9
  215. package/src/components/templates/ArrayFieldTemplate.tsx +2 -9
  216. package/src/components/templates/ArrayFieldTitleTemplate.tsx +2 -10
  217. package/src/components/templates/BaseInputTemplate.tsx +16 -23
  218. package/src/components/templates/ButtonTemplates/AddButton.tsx +2 -1
  219. package/src/components/templates/ButtonTemplates/IconButton.tsx +16 -7
  220. package/src/components/templates/ButtonTemplates/SubmitButton.tsx +2 -1
  221. package/src/components/templates/ButtonTemplates/index.ts +1 -1
  222. package/src/components/templates/CyclicSchemaExpandTemplate.tsx +31 -0
  223. package/src/components/templates/DescriptionField.tsx +1 -1
  224. package/src/components/templates/ErrorList.tsx +8 -15
  225. package/src/components/templates/FallbackFieldTemplate.tsx +2 -1
  226. package/src/components/templates/FieldErrorTemplate.tsx +8 -8
  227. package/src/components/templates/FieldHelpTemplate.tsx +2 -1
  228. package/src/components/templates/FieldTemplate/FieldTemplate.tsx +2 -8
  229. package/src/components/templates/FieldTemplate/Label.tsx +2 -2
  230. package/src/components/templates/GridTemplate.tsx +1 -1
  231. package/src/components/templates/MultiSchemaFieldTemplate.tsx +1 -1
  232. package/src/components/templates/ObjectFieldTemplate.tsx +2 -7
  233. package/src/components/templates/OptionalDataControlsTemplate.tsx +3 -2
  234. package/src/components/templates/TitleField.tsx +1 -1
  235. package/src/components/templates/UnsupportedField.tsx +4 -3
  236. package/src/components/templates/WrapIfAdditionalTemplate.tsx +2 -9
  237. package/src/components/templates/index.ts +3 -1
  238. package/src/components/widgets/AltDateTimeWidget.tsx +1 -1
  239. package/src/components/widgets/AltDateWidget.tsx +7 -13
  240. package/src/components/widgets/CheckboxWidget.tsx +3 -5
  241. package/src/components/widgets/CheckboxesWidget.tsx +8 -10
  242. package/src/components/widgets/ColorWidget.tsx +2 -1
  243. package/src/components/widgets/DateTimeWidget.tsx +3 -10
  244. package/src/components/widgets/DateWidget.tsx +2 -1
  245. package/src/components/widgets/EmailWidget.tsx +2 -1
  246. package/src/components/widgets/FileWidget.tsx +9 -8
  247. package/src/components/widgets/HiddenWidget.tsx +1 -1
  248. package/src/components/widgets/PasswordWidget.tsx +2 -1
  249. package/src/components/widgets/RadioWidget.tsx +8 -10
  250. package/src/components/widgets/RangeWidget.tsx +1 -1
  251. package/src/components/widgets/RatingWidget.tsx +48 -47
  252. package/src/components/widgets/SelectWidget.tsx +48 -31
  253. package/src/components/widgets/TextWidget.tsx +2 -1
  254. package/src/components/widgets/TextareaWidget.tsx +10 -7
  255. package/src/components/widgets/TimeWidget.tsx +2 -1
  256. package/src/components/widgets/URLWidget.tsx +2 -1
  257. package/src/components/widgets/UpDownWidget.tsx +2 -1
  258. package/src/components/widgets/index.ts +1 -1
  259. package/src/getDefaultRegistry.ts +2 -9
  260. package/src/getTestRegistry.tsx +2 -7
  261. package/src/index.ts +10 -5
  262. package/src/withTheme.tsx +14 -9
@@ -1,112 +1,104 @@
1
- import { Component } from 'react';
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
- class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> extends Component<
39
- FieldProps<T, S, F>,
40
- AnyOfFieldState<S>
41
- > {
42
- /** Constructs an `AnyOfField` with the given `props` to initialize the initially selected option in state
43
- *
44
- * @param props - The `FieldProps` for this template
45
- */
46
- constructor(props: FieldProps<T, S, F>) {
47
- super(props);
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
- const {
50
- formData,
51
- options,
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
- this.state = {
58
- retrievedOptions,
59
- selectedOption: this.getMatchingOption(0, formData, retrievedOptions),
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
- /** React lifecycle method that is called when the props and/or state for this component is updated. It recomputes the
64
- * currently selected option based on the overall `formData`
65
- *
66
- * @param prevProps - The previous `FieldProps` for this template
67
- * @param prevState - The previous `AnyOfFieldState` for this template
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
- componentDidUpdate(prevProps: Readonly<FieldProps<T, S, F>>, prevState: Readonly<AnyOfFieldState>) {
70
- const { formData, options, fieldPathId } = this.props;
71
- const { selectedOption } = this.state;
72
- let newState = this.state;
73
- if (!deepEquals(prevProps.options, options)) {
74
- const {
75
- registry: { schemaUtils },
76
- } = this.props;
77
- // re-cache the retrieved options in state in case they have $refs to save doing it later
78
- const retrievedOptions = options.map((opt: S) => schemaUtils.retrieveSchema(opt, formData));
79
- newState = { selectedOption, retrievedOptions };
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
- if (prevState && matchingOption !== selectedOption) {
86
- newState = { selectedOption: matchingOption, retrievedOptions };
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
- if (newState !== this.state) {
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
- const discriminator = getDiscriminatorFieldFromSchema<S>(schema);
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 = (option?: string) => {
118
- const { selectedOption, retrievedOptions } = this.state;
119
- const { formData, onChange, registry, fieldPathId } = this.props;
120
- const { schemaUtils } = registry;
121
- const intOption = option !== undefined ? parseInt(option, 10) : -1;
122
- if (intOption === selectedOption) {
123
- return;
124
- }
125
- const newOption = intOption >= 0 ? retrievedOptions[intOption] : undefined;
126
- const oldOption = selectedOption >= 0 ? retrievedOptions[selectedOption] : undefined;
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
- getFieldId() {
141
- const { fieldPathId, schema } = this.props;
142
- return `${fieldPathId.$id}${schema.oneOf ? '__oneof_select' : '__anyof_select'}`;
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
- /** Renders the `AnyOfField` selector along with a `SchemaField` for the value of the `formData`
146
- */
147
- render() {
148
- const {
149
- name,
150
- disabled = false,
151
- errorSchema = {},
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
- const { widgets, fields, translateString, globalUiOptions, schemaUtils } = registry;
163
- const { SchemaField: _SchemaField } = fields;
164
- const MultiSchemaFieldTemplate = getTemplate<'MultiSchemaFieldTemplate', T, S, F>(
165
- 'MultiSchemaFieldTemplate',
166
- registry,
167
- globalUiOptions,
168
- );
169
- const isOptionalRender = shouldRenderOptionalField<T, S, F>(registry, schema, required, uiSchema);
170
- const hasFormData = isFormDataAvailable<T>(formData);
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
- const { selectedOption, retrievedOptions } = this.state;
173
- const {
174
- widget = 'select',
175
- placeholder,
176
- autofocus,
177
- autocomplete,
178
- title = schema.title,
179
- ...uiOptions
180
- } = getUiOptions<T, S, F>(uiSchema, globalUiOptions);
181
- const Widget = getWidget<T, S, F>({ type: 'number' }, widget, widgets);
182
- const rawErrors = get(errorSchema, ERRORS_KEY, []);
183
- const fieldErrorSchema = omit(errorSchema, [ERRORS_KEY]);
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
- const option = selectedOption >= 0 ? retrievedOptions[selectedOption] || null : null;
187
- let optionSchema: S | undefined | null;
159
+ const option = selectedOption >= 0 ? retrievedOptions[selectedOption] || null : null;
160
+ let optionSchema: S | undefined | null;
188
161
 
189
- if (option) {
190
- // merge top level required field
191
- const { required } = schema;
192
- // Merge in all the non-oneOf/anyOf properties and also skip the special ADDITIONAL_PROPERTY_FLAG property
193
- optionSchema = required ? (mergeSchemas({ required }, option) as S) : option;
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
- // First we will check to see if there is an anyOf/oneOf override for the UI schema
197
- let optionsUiSchema: UiSchema<T, S, F>[] = [];
198
- if (ONE_OF_KEY in schema && uiSchema && ONE_OF_KEY in uiSchema) {
199
- if (Array.isArray(uiSchema[ONE_OF_KEY])) {
200
- optionsUiSchema = uiSchema[ONE_OF_KEY];
201
- } else {
202
- console.warn(`uiSchema.oneOf is not an array for "${title || name}"`);
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
- // Then we pick the one that matches the selected option index, if one exists otherwise default to the main uiSchema
212
- let optionUiSchema = uiSchema;
213
- if (selectedOption >= 0 && optionsUiSchema.length > selectedOption) {
214
- optionUiSchema = optionsUiSchema[selectedOption];
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
- const translateEnum: TranslatableString = title
218
- ? TranslatableString.TitleOptionPrefix
219
- : TranslatableString.OptionPrefix;
220
- const translateParams = title ? [title] : [];
221
- const enumOptions = retrievedOptions.map((opt: { title?: string }, index: number) => {
222
- // Also see if there is an override title in the uiSchema for each option, otherwise use the title from the option
223
- const { title: uiTitle = opt.title } = getUiOptions<T, S, F>(optionsUiSchema[index]);
224
- return {
225
- label: uiTitle || translateString(translateEnum, translateParams.concat(String(index + 1))),
226
- value: index,
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
- return (
262
- <MultiSchemaFieldTemplate
263
- schema={schema}
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
- uiSchema={uiSchema}
266
- selector={selector}
267
- optionSchemaField={optionsSchemaField}
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
- (value: FieldProps<T, S, F>['value'], path: FieldPathList, errorSchema?: ErrorSchema<T>, id?: string) => {
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(value);
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
- if (`${value}`.charAt(0) === '.') {
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 value === 'string' && value.match(trailingCharMatcherWithPrefix)
70
- ? asNumber(value.replace(trailingCharMatcher, ''))
71
- : asNumber(value);
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
  },