Skip to content

Commit 4f015ee

Browse files
committed
Merge remote-tracking branch 'blessed/main' into expose-title-on-error
2 parents f8123e5 + bd54461 commit 4f015ee

File tree

22 files changed

+912
-88
lines changed

22 files changed

+912
-88
lines changed

CHANGELOG.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,45 @@ should change the heading of the (upcoming) version to include a major version b
1717
-->
1818
# 6.0.0-beta.14
1919

20+
## @rjsf/core
21+
22+
- Added support for dynamic UI schema in array fields - the `items` property in `uiSchema` can now accept a function that returns a UI schema based on the array item's data, index, and form context ([#4706](https://github.com/rjsf-team/react-jsonschema-form/pull/4706))
23+
- Fixed checkbox widget to use current value instead of event target in onFocus/onBlur handlers, fixing [#4704](https://github.com/rjsf-team/react-jsonschema-form/issues/4704)
24+
2025
## @rjsf/utils
2126

27+
- Updated `UiSchema` type to support dynamic array item UI schemas - the `items` property can now be either a `UiSchema` object or a function that returns a `UiSchema` ([#4706](https://github.com/rjsf-team/react-jsonschema-form/pull/4706))
2228
- Added `title` property to `RJSFValidationError` [PR](https://github.com/rjsf-team/react-jsonschema-form/pull/4700)
2329

30+
## @rjsf/chakra-ui
31+
32+
- Fixed checkbox widget to use current value instead of event target in onFocus/onBlur handlers, fixing [#4704](https://github.com/rjsf-team/react-jsonschema-form/issues/4704)
33+
34+
## @rjsf/daisyui
35+
36+
- Fixed checkbox widget to use current value instead of event target in onFocus/onBlur handlers, fixing [#4704](https://github.com/rjsf-team/react-jsonschema-form/issues/4704)
37+
38+
## @rjsf/fluentui-rc
39+
40+
- Fixed checkbox widget to use current value instead of event target in onFocus/onBlur handlers, fixing [#4704](https://github.com/rjsf-team/react-jsonschema-form/issues/4704)
41+
42+
## @rjsf/mui
43+
44+
- Fixed checkbox widget to use current value instead of event target in onFocus/onBlur handlers, fixing [#4704](https://github.com/rjsf-team/react-jsonschema-form/issues/4704)
45+
46+
## @rjsf/primereact
47+
48+
- Fixed checkbox widget to use current value instead of event target in onFocus/onBlur handlers, fixing [#4704](https://github.com/rjsf-team/react-jsonschema-form/issues/4704)
49+
50+
## @rjsf/semantic-ui
51+
52+
- Fixed checkbox widget to use current value instead of event target in onFocus/onBlur handlers, fixing [#4704](https://github.com/rjsf-team/react-jsonschema-form/issues/4704)
53+
54+
## Dev / docs / playground
55+
56+
- Added comprehensive documentation for dynamic UI schema feature with TypeScript examples ([#4706](https://github.com/rjsf-team/react-jsonschema-form/pull/4706))
57+
- Updated array documentation to reference the new dynamic UI schema capabilities ([#4706](https://github.com/rjsf-team/react-jsonschema-form/pull/4706))
58+
2459
## @rjsf/validator-ajv8
2560

2661
- Updated `transformRJSFValidationErrors()` to include the `title` property of a field with error fixing #4504 with [PR](https://github.com/rjsf-team/react-jsonschema-form/pull/4700)
@@ -42,11 +77,6 @@ should change the heading of the (upcoming) version to include a major version b
4277
- Extended `Registry` interface to include optional `experimental_componentUpdateStrategy` property
4378
- Added `shallowEquals()` utility function for shallow equality comparisons
4479
- Fixed boolean fields incorrectly set to `{}` when switching oneOf/anyOf options with `mergeDefaultsIntoFormData` set to `useDefaultIfFormDataUndefined`, fixing [#4709](https://github.com/rjsf-team/react-jsonschema-form/issues/4709) ([#4710](https://github.com/rjsf-team/react-jsonschema-form/pull/4710))
45-
46-
# 6.0.0-beta.13
47-
48-
## rjsf/utils
49-
5080
- Always make all references absolute in nested bundled schemas
5181

5282
# 6.0.0-beta.12

packages/chakra-ui/src/CheckboxWidget/CheckboxWidget.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export default function CheckboxWidget<
4848
const description = options.description || schema.description;
4949

5050
const _onChange = ({ checked }: CheckboxCheckedChangeDetails) => onChange(checked);
51-
const _onBlur = ({ target }: FocusEvent<HTMLInputElement | any>) => onBlur(id, target && target.value);
52-
const _onFocus = ({ target }: FocusEvent<HTMLInputElement | any>) => onFocus(id, target && target.value);
51+
const _onBlur = ({ target }: FocusEvent<HTMLInputElement | any>) => onBlur(id, target && target.checked);
52+
const _onFocus = ({ target }: FocusEvent<HTMLInputElement | any>) => onFocus(id, target && target.checked);
5353

5454
const chakraProps = getChakra({ uiSchema });
5555

packages/core/src/components/fields/ArrayField.tsx

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import cloneDeep from 'lodash/cloneDeep';
2222
import get from 'lodash/get';
2323
import isObject from 'lodash/isObject';
2424
import set from 'lodash/set';
25-
import { nanoid } from 'nanoid';
25+
import uniqueId from 'lodash/uniqueId';
2626

2727
/** Type used to represent the keyed form data used in the state */
2828
type KeyedFormDataType<T> = { key: string; item: T };
@@ -37,7 +37,7 @@ type ArrayFieldState<T> = {
3737

3838
/** Used to generate a unique ID for an element in a row */
3939
function generateRowId() {
40-
return nanoid();
40+
return uniqueId('rjsf-array-item-');
4141
}
4242

4343
/** Converts the `formData` into `KeyedFormDataType` data, using the `generateRowId()` function to create the key
@@ -423,6 +423,39 @@ class ArrayField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
423423
onChange(value, undefined, idSchema && idSchema.$id);
424424
};
425425

426+
/** Helper method to compute item UI schema for both normal and fixed arrays
427+
* Handles both static object and dynamic function cases
428+
*
429+
* @param uiSchema - The parent UI schema containing items definition
430+
* @param item - The item data
431+
* @param index - The index of the item
432+
* @param formContext - The form context
433+
* @returns The computed UI schema for the item
434+
*/
435+
private computeItemUiSchema(
436+
uiSchema: UiSchema<T[], S, F>,
437+
item: T,
438+
index: number,
439+
formContext: F,
440+
): UiSchema<T[], S, F> | undefined {
441+
if (typeof uiSchema.items === 'function') {
442+
try {
443+
// Call the function with item data, index, and form context
444+
// TypeScript now correctly infers the types thanks to the ArrayElement type in UiSchema
445+
const result = uiSchema.items(item, index, formContext);
446+
// Only use the result if it's truthy
447+
return result as UiSchema<T[], S, F>;
448+
} catch (e) {
449+
console.error(`Error executing dynamic uiSchema.items function for item at index ${index}:`, e);
450+
// Fall back to undefined to allow the field to still render
451+
return undefined;
452+
}
453+
} else {
454+
// Static object case - preserve undefined to maintain backward compatibility
455+
return uiSchema.items as UiSchema<T[], S, F> | undefined;
456+
}
457+
}
458+
426459
/** Renders the `ArrayField` depending on the specific needs of the schema and uischema elements
427460
*/
428461
render() {
@@ -500,6 +533,10 @@ class ArrayField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
500533
const itemErrorSchema = errorSchema ? (errorSchema[index] as ErrorSchema<T[]>) : undefined;
501534
const itemIdPrefix = idSchema.$id + idSeparator + index;
502535
const itemIdSchema = schemaUtils.toIdSchema(itemSchema, itemIdPrefix, itemCast, idPrefix, idSeparator);
536+
537+
// Compute the item UI schema using the helper method
538+
const itemUiSchema = this.computeItemUiSchema(uiSchema, item, index, formContext);
539+
503540
return this.renderArrayFieldItem({
504541
key,
505542
index,
@@ -512,7 +549,7 @@ class ArrayField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
512549
itemIdSchema,
513550
itemErrorSchema,
514551
itemData: itemCast,
515-
itemUiSchema: uiSchema.items,
552+
itemUiSchema,
516553
autofocus: autofocus && index === 0,
517554
onBlur,
518555
onFocus,
@@ -751,11 +788,20 @@ class ArrayField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
751788
: itemSchemas[index]) || {};
752789
const itemIdPrefix = idSchema.$id + idSeparator + index;
753790
const itemIdSchema = schemaUtils.toIdSchema(itemSchema, itemIdPrefix, itemCast, idPrefix, idSeparator);
754-
const itemUiSchema = additional
755-
? uiSchema.additionalItems || {}
756-
: Array.isArray(uiSchema.items)
757-
? uiSchema.items[index]
758-
: uiSchema.items || {};
791+
// Compute the item UI schema - handle both static and dynamic cases
792+
let itemUiSchema: UiSchema<T[], S, F> | undefined;
793+
if (additional) {
794+
// For additional items, use additionalItems uiSchema
795+
itemUiSchema = uiSchema.additionalItems as UiSchema<T[], S, F>;
796+
} else {
797+
// For fixed items, uiSchema.items can be an array, a function, or a single object
798+
if (Array.isArray(uiSchema.items)) {
799+
itemUiSchema = uiSchema.items[index] as UiSchema<T[], S, F>;
800+
} else {
801+
// Use the helper method for function or static object cases
802+
itemUiSchema = this.computeItemUiSchema(uiSchema, item, index, formContext);
803+
}
804+
}
759805
const itemErrorSchema = errorSchema ? (errorSchema[index] as ErrorSchema<T[]>) : undefined;
760806

761807
return this.renderArrayFieldItem({
@@ -811,7 +857,7 @@ class ArrayField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
811857
canMoveDown: boolean;
812858
itemSchema: S;
813859
itemData: T[];
814-
itemUiSchema: UiSchema<T[], S, F>;
860+
itemUiSchema: UiSchema<T[], S, F> | undefined;
815861
itemIdSchema: IdSchema<T[]>;
816862
itemErrorSchema?: ErrorSchema<T[]>;
817863
autofocus?: boolean;

0 commit comments

Comments
 (0)