@@ -207,7 +207,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
207
207
// Get the default if set from properties to ensure the dependencies conditions are resolved based on it
208
208
const defaultFormData : T = {
209
209
...formData ,
210
- ...getDefaultBasedOnSchemaType ( validator , schema , defaults , computeDefaultsProps ) ,
210
+ ...getDefaultBasedOnSchemaType ( validator , schema , computeDefaultsProps , defaults ) ,
211
211
} ;
212
212
const resolvedSchema = resolveDependencies < T , S , F > ( validator , schema , rootSchema , false , [ ] , defaultFormData ) ;
213
213
schemaToCompute = resolvedSchema [ 0 ] ; // pick the first element from resolve dependencies
@@ -276,7 +276,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
276
276
defaults = schema . default as unknown as T ;
277
277
}
278
278
279
- const defaultBasedOnSchemaType = getDefaultBasedOnSchemaType ( validator , schema , defaults , computeDefaultsProps ) ;
279
+ const defaultBasedOnSchemaType = getDefaultBasedOnSchemaType ( validator , schema , computeDefaultsProps , defaults ) ;
280
280
281
281
return defaultBasedOnSchemaType ?? defaults ;
282
282
}
@@ -285,25 +285,27 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
285
285
*
286
286
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
287
287
* @param rawSchema - The schema for which the default state is desired
288
- * @param defaults - Optional props for this function
289
288
* @param {ComputeDefaultsProps } computeDefaultsProps - Optional props for this function
289
+ * @param defaults - Optional props for this function
290
290
* @returns - The default value based on the schema type if they are defined for object or array schemas.
291
291
*/
292
- function getDefaultBasedOnSchemaType < T = any , S extends StrictRJSFSchema = RJSFSchema , F extends FormContextType = any > (
292
+ export function getDefaultBasedOnSchemaType <
293
+ T = any ,
294
+ S extends StrictRJSFSchema = RJSFSchema ,
295
+ F extends FormContextType = any
296
+ > (
293
297
validator : ValidatorType < T , S , F > ,
294
298
rawSchema : S ,
295
- defaults : T | T [ ] | undefined ,
296
- computeDefaultsProps : ComputeDefaultsProps < T , S > = { }
299
+ computeDefaultsProps : ComputeDefaultsProps < T , S > = { } ,
300
+ defaults ?: T | T [ ] | undefined
297
301
) : T | T [ ] | void {
298
- const schema : S = isObject ( rawSchema ) ? rawSchema : ( { } as S ) ;
299
-
300
- switch ( getSchemaType < S > ( schema ) ) {
302
+ switch ( getSchemaType < S > ( rawSchema ) ) {
301
303
// We need to recurse for object schema inner default values.
302
304
case 'object' : {
303
- return getObjectDefaults ( validator , schema , defaults , computeDefaultsProps ) ;
305
+ return getObjectDefaults ( validator , rawSchema , computeDefaultsProps , defaults ) ;
304
306
}
305
307
case 'array' : {
306
- return getArrayDefaults ( validator , schema , defaults , computeDefaultsProps ) ;
308
+ return getArrayDefaults ( validator , rawSchema , computeDefaultsProps , defaults ) ;
307
309
}
308
310
}
309
311
}
@@ -312,26 +314,26 @@ function getDefaultBasedOnSchemaType<T = any, S extends StrictRJSFSchema = RJSFS
312
314
*
313
315
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
314
316
* @param rawSchema - The schema for which the default state is desired
315
- * @param defaults - Optional props for this function
316
317
* @param {ComputeDefaultsProps } computeDefaultsProps - Optional props for this function
318
+ * @param defaults - Optional props for this function
317
319
* @returns - The default value based on the schema type if they are defined for object or array schemas.
318
320
*/
319
- function getObjectDefaults < T = any , S extends StrictRJSFSchema = RJSFSchema , F extends FormContextType = any > (
321
+ export function getObjectDefaults < T = any , S extends StrictRJSFSchema = RJSFSchema , F extends FormContextType = any > (
320
322
validator : ValidatorType < T , S , F > ,
321
323
rawSchema : S ,
322
- defaults : T | T [ ] | undefined ,
323
324
{
324
325
rawFormData,
325
326
rootSchema = { } as S ,
326
327
includeUndefinedValues = false ,
327
328
_recurseList = [ ] ,
328
329
experimental_defaultFormStateBehavior = undefined ,
329
330
required,
330
- } : ComputeDefaultsProps < T , S > = { }
331
+ } : ComputeDefaultsProps < T , S > = { } ,
332
+ defaults ?: T | T [ ] | undefined
331
333
) : T {
332
334
{
333
335
const formData : T = ( isObject ( rawFormData ) ? rawFormData : { } ) as T ;
334
- const schema : S = isObject ( rawSchema ) ? rawSchema : ( { } as S ) ;
336
+ const schema : S = rawSchema ;
335
337
// This is a custom addition that fixes this issue:
336
338
// https://github.com/rjsf-team/react-jsonschema-form/issues/3832
337
339
const retrievedSchema =
@@ -412,23 +414,23 @@ function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F e
412
414
*
413
415
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
414
416
* @param rawSchema - The schema for which the default state is desired
415
- * @param defaults - Optional props for this function
416
417
* @param {ComputeDefaultsProps } computeDefaultsProps - Optional props for this function
418
+ * @param defaults - Optional props for this function
417
419
* @returns - The default value based on the schema type if they are defined for object or array schemas.
418
420
*/
419
- function getArrayDefaults < T = any , S extends StrictRJSFSchema = RJSFSchema , F extends FormContextType = any > (
421
+ export function getArrayDefaults < T = any , S extends StrictRJSFSchema = RJSFSchema , F extends FormContextType = any > (
420
422
validator : ValidatorType < T , S , F > ,
421
423
rawSchema : S ,
422
- defaults : T | T [ ] | undefined ,
423
424
{
424
425
rawFormData,
425
426
rootSchema = { } as S ,
426
427
_recurseList = [ ] ,
427
428
experimental_defaultFormStateBehavior = undefined ,
428
429
required,
429
- } : ComputeDefaultsProps < T , S > = { }
430
+ } : ComputeDefaultsProps < T , S > = { } ,
431
+ defaults ?: T | T [ ] | undefined
430
432
) : T | T [ ] | undefined {
431
- const schema : S = isObject ( rawSchema ) ? rawSchema : ( { } as S ) ;
433
+ const schema : S = rawSchema ;
432
434
433
435
const neverPopulate = experimental_defaultFormStateBehavior ?. arrayMinItems ?. populate === 'never' ;
434
436
const ignoreMinItemsFlagSet = experimental_defaultFormStateBehavior ?. arrayMinItems ?. populate === 'requiredOnly' ;
0 commit comments