You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/docs/docs/api-reference/utility-functions.md
+148Lines changed: 148 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,6 +70,20 @@ Otherwise, the string is wrapped by `Number()` and if that result is not `NaN`,
70
70
71
71
- undefined | null | string | number: The `value` converted to a number when appropriate, otherwise the `value`
72
72
73
+
### bracketNameGenerator()
74
+
75
+
Generates bracketed names for form fields.
76
+
77
+
#### Parameters
78
+
79
+
- path: FieldPathList - The path of field path units to use when generating the name
80
+
- idPrefix: string - The prefix to use at the start of the generated name
81
+
-[isMultiValue]: boolean | undefined - Optional flag, if true, will append `[]` to the end of the name for multi-value fields (e.g., checkboxes, multi-select)
82
+
83
+
#### Returns
84
+
85
+
- string: The generated bracketed name (e.g., `root[tasks][0][title]`, or `root[hobbies][]` for multi-value fields)
86
+
73
87
### buttonId()
74
88
75
89
Return a consistent `id` for the `btn` button element
@@ -167,6 +181,20 @@ Return a consistent `id` for the field description element.
167
181
168
182
- string: The consistent id for the field description element from the given `id`
169
183
184
+
### dotNotationNameGenerator()
185
+
186
+
Generates dot-notation names for form fields. Multi-value fields are handled the same as single-value fields in dot notation.
187
+
188
+
#### Parameters
189
+
190
+
- path: FieldPathList - The path of field path units to use when generating the name
191
+
- idPrefix: string - The prefix to use at the start of the generated name
192
+
-[_isMultiValue]: boolean | undefined - Optional flag (unused in dot notation)
193
+
194
+
#### Returns
195
+
196
+
- string: The generated dot-notation name (e.g., `root.tasks.0.title`)
197
+
170
198
### englishStringTranslator()
171
199
172
200
Translates a `TranslatableString` value `stringToTranslate` into english.
@@ -197,6 +225,25 @@ If it is a single value, then if the enum option value with the `valueIndex` in
197
225
198
226
- EnumOptionsType<S>["value"][]: The updated `selected` list with the `value` removed from it
Returns the index(es) of the options in `allEnumOptions` whose value(s) match the ones in `value`.
@@ -257,6 +304,41 @@ If `valueIndex` is an array, AND it contains an invalid index, the returned arra
257
304
258
305
- EnumOptionsType<S>["value"] | EnumOptionsType<S>["value"][] | undefined: The single or list of values specified by the single or list of indexes if they are valid. Otherwise, `emptyValue` or an empty list.
Decodes a string from a DOM value attribute back to a typed enum value.
310
+
When `format` is `'realValue'`, does a reverse lookup: finds the enum option whose `String(value)` matches the input string and returns the original typed value.
311
+
For object/array values that were encoded as indices, falls back to index resolution.
312
+
When `format` is `'indexed'` (the default), uses index-based resolution via `enumOptionsValueForIndex`.
313
+
314
+
#### Parameters
315
+
316
+
- value: string | string[] - The string value(s) from the DOM
317
+
- enumOptions: EnumOptionsType<S>[] | undefined - The available enum options
318
+
-[format='indexed']: OptionValueFormat - How the values were encoded on the DOM
319
+
- emptyValue: unknown - The value to return for empty/missing selections
320
+
321
+
#### Returns
322
+
323
+
- unknown: The original typed enum value(s)
324
+
325
+
### enumOptionValueEncoder()
326
+
327
+
Encodes an enum option value into a string for a DOM value attribute.
328
+
When `format` is `'realValue'`, primitive values are converted via `String()`.
329
+
Non-primitive values (objects, arrays) fall back to the index since `String()` would produce `"[object Object]"`.
330
+
When `format` is `'indexed'` (the default), returns the index as a string.
331
+
332
+
#### Parameters
333
+
334
+
- value: unknown - The typed enum value
335
+
- index: number - The option's position in the enumOptions array
336
+
-[format='indexed']: OptionValueFormat - How to encode the value for the DOM attribute
337
+
338
+
#### Returns
339
+
340
+
- string: The string to use as the DOM value attribute
341
+
260
342
### errorId()
261
343
262
344
Return a consistent `id` for the field error element.
@@ -383,6 +465,20 @@ This function does not work with discriminators of `"type": "object"` and `"type
383
465
384
466
- number | undefined: index of the matched option
385
467
468
+
### getOptionValueFormat()
469
+
470
+
Resolves the effective `optionValueFormat` for enum-backed widgets.
471
+
Provides a single source of truth for the default DOM encoding format (`'indexed'`) used by `SelectWidget`, `RadioWidget`, and `CheckboxesWidget`.
472
+
Widgets should call this helper once and pass the result to `enumOptionValueEncoder`, `enumOptionValueDecoder`, and `enumOptionSelectedValue` rather than reading `options.optionValueFormat` directly.
473
+
474
+
#### Parameters
475
+
476
+
-[options]: \{ optionValueFormat?: OptionValueFormat } | undefined - The widget options (typically from the `options` prop, already resolved from `ui:options` and `ui:globalOptions`)
477
+
478
+
#### Returns
479
+
480
+
- OptionValueFormat: The resolved `OptionValueFormat`, defaulting to `'indexed'` when not set
481
+
386
482
### getSchemaType()
387
483
388
484
Gets the type of a given `schema`.
@@ -858,6 +954,27 @@ When a `params` array is provided, each value in the array is used to replace an
858
954
859
955
- string: The updated string with any replacement specifiers replaced
860
956
957
+
### resolveUiSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>()
958
+
959
+
Resolves the uiSchema for a given schema, considering `ui:definitions` stored in the registry.
960
+
Called at runtime for each field. When the schema contains a `$ref`, looks up the corresponding uiSchema definition from `registry.uiSchemaDefinitions` and merges it with local overrides.
961
+
For schemas with `oneOf`/`anyOf` branches, also populates `uiSchema[keyword][i]` for branches whose `$ref` matches a definition, so `MultiSchemaField` can read dropdown option titles.
962
+
963
+
Resolution order (later sources override earlier):
964
+
965
+
1.`ui:definitions[$ref]` - base definition from registry
966
+
2.`localUiSchema` - local overrides at current path
967
+
968
+
#### Parameters
969
+
970
+
- schema: S - The JSON schema (may contain `$ref` or `RJSF_REF_KEY`)
971
+
- localUiSchema: UiSchema<T, S, F> | undefined - The uiSchema at the current path (local overrides)
972
+
- registry: Registry<T, S, F> - The registry containing `uiSchemaDefinitions`
973
+
974
+
#### Returns
975
+
976
+
- UiSchema<T, S, F>: The resolved uiSchema with definitions merged in
Check to see if a `schema` specifies that a value must be true. This happens when:
@@ -875,6 +992,20 @@ Check to see if a `schema` specifies that a value must be true. This happens whe
875
992
876
993
- boolean: True if the schema specifies a value that must be true, false otherwise
877
994
995
+
### shallowEquals()
996
+
997
+
Implements a shallow equals comparison that uses `Object.is()` for comparing values.
998
+
This function compares objects by checking if all keys and their values are equal using `Object.is()`.
999
+
1000
+
#### Parameters
1001
+
1002
+
- a: any - The first element to compare
1003
+
- b: any - The second element to compare
1004
+
1005
+
#### Returns
1006
+
1007
+
- boolean: True if the `a` and `b` are shallow equal, false otherwise
1008
+
878
1009
### shouldRender()
879
1010
880
1011
Determines whether the given `component` should be rerendered by comparing its current set of props and state against the next set.
@@ -1291,6 +1422,23 @@ Checks to see if the `schema` combination represents a select
1291
1422
1292
1423
- boolean: True if schema contains a select, otherwise false
1293
1424
1425
+
### removeOptionalEmptyObjects<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>()
1426
+
1427
+
Recursively removes optional objects from the `formData` that are empty (i.e., all their fields are undefined, null, empty strings, or themselves empty optional objects).
1428
+
This solves the problem where interacting with fields inside an optional object "activates" it permanently, making the form unsubmittable when the optional object has required inner fields.
1429
+
An object property is considered "optional" when it is NOT listed in its parent schema's `required` array.
1430
+
1431
+
#### Parameters
1432
+
1433
+
- validator: ValidatorType<T, S, F> - An implementation of the `ValidatorType` interface that will be used when necessary
1434
+
- schema: S - The JSON schema describing the `formData`
1435
+
-[rootSchema]: S | undefined - The root schema, used primarily to look up `$ref`s
1436
+
-[formData]: T | undefined - The current form data to prune
1437
+
1438
+
#### Returns
1439
+
1440
+
- T | undefined: A new copy of `formData` with empty optional objects removed, or `undefined` if the entire formData was pruned
1441
+
1294
1442
### retrieveSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>()
1295
1443
1296
1444
Retrieves an expanded schema that has had all of its conditions, additional properties, references and dependencies
0 commit comments