Skip to content

Conversation

chathuraa
Copy link
Contributor

Reasons for making this change

This PR implements dynamic uiSchema support for array items, allowing developers to programmatically generate UI schemas based on array item data, index, and context. This addresses the need for more flexible array rendering when UI requirements vary based on data content.

Key features added:

  • Dynamic uiSchema.items function support that receives (itemData, itemIndex, itemCount, formData)
  • Preserved backward compatibility with static uiSchema.items objects/arrays
  • Enhanced error handling for dynamic uiSchema functions
  • Comprehensive test coverage and documentation

The implementation allows for use cases like:

  • Conditional field rendering based on item data
  • Index-based styling (e.g., different styling for first/last items)
  • Context-aware field configuration
  • Dynamic validation rules per array item

Checklist

  • I'm updating documentation
  • I'm adding or updating code
    • I've added and/or updated tests. I've run npx nx run-many --target=build --exclude=@rjsf/docs && npm run test:update to update snapshots, if needed.
    • I've updated docs if needed
    • I've updated the changelog with a description of the PR
  • I'm adding a new feature
    • I've updated the playground with an example use of the feature

Enhances the ArrayField component to support dynamic uiSchema generation through a function-based approach, allowing UI schemas to be conditionally created based on:
- Item data content
- Item index
- Form context

This maintains backward compatibility with existing static uiSchema objects while enabling more flexible, context-aware UI rendering for array items.

Adds comprehensive documentation with examples showing practical use cases and benefits.
Enhances ArrayField component to properly handle dynamic uiSchema.items functions:

- Improves type safety with new ArrayElement utility type
- Adds proper function call handling for both normal and fixed arrays
- Ensures graceful error handling and fallback to empty object
- Correctly passes item data, index and formContext to function
- Adds comprehensive test suite for dynamic uiSchema functionality

This feature allows for conditional UI rendering based on item data,
significantly improving form customization capabilities.
- Clarifies error messages by adding "item at" to provide better context when dynamic uiSchema functions fail
- Adds test case for handling errors in dynamic uiSchema functions for fixed arrays
- Ensures consistent error handling between regular and fixed array implementations

This improves debugging experience for developers using dynamic uiSchema functionality.
Updates the type annotations in ArrayField component to accept undefined
as a valid type for itemUiSchema. This preserves backward compatibility
by not defaulting to an empty object when uiSchema.items is undefined.

Previously, the code always defaulted to an empty object which could
cause unexpected behavior when checking for the existence of UI schema.
Modifies the ArrayField component to properly handle undefined uiSchema values
instead of converting them to empty objects. This maintains backward compatibility
and prevents unnecessary props from being passed to child components.

The change:
- Only applies truthy results from dynamic uiSchema functions
- Preserves undefined values in static object cases
- Updates snapshots to reflect the removed empty uiSchema objects

This fixes inconsistent behavior when working with conditionally defined UI schemas.
Propagates the idSchema.$id to the Fieldset component in the ArrayFieldTemplate,
ensuring consistent and accessible ID attributes in the generated HTML.

Updates test snapshots to reflect the new ID values, replacing previously null
or auto-generated IDs with consistent "root" identifiers and related ARIA
attribute references.
…ractices

Expands DYNAMIC_UISCHEMA_EXAMPLES.md with:
- Additional examples for falsy return values and fixed arrays
- Detailed notes on handling new items and error cases
- Performance considerations and optimization techniques
- Clarified behavior documentation for dynamic UI schemas

Improves developer experience by providing comprehensive guidance
for implementing dynamic UI schemas effectively.
@heath-freenome
Copy link
Member

@chathuraa Looks like your changes are breaking core and chakra-ui theme tests

- Replaces nanoid with lodash/uniqueId for array item ID generation
- Adds prefix "rjsf-array-item-" to generated IDs for better debugging
- Adds null/undefined checks for uiSchema in ArrayField and getUiOptions
- Adds NX Cloud ID to configuration

This change improves robustness when handling undefined UI schemas and
provides more consistent ID generation for array items.
- Updates CheckboxWidget _onBlur and _onFocus handlers to use target.checked instead of target.value
- Adds tests for getUiOptions when uiSchema is undefined or null
- Removes nxCloudId from nx.json configuration
@chathuraa
Copy link
Contributor Author

Hey @heath-freenome ,

I have fixed the issue and added a couple of more tests.

Cheers,
Chat.

@heath-freenome
Copy link
Member

Daisy UI tests are failing...

…ckbox value handling

Refactors ArrayField by extracting computeItemUiSchema helper method to eliminate duplicate code
- Creates a shared private method for computing item UI schema that handles both function and object cases
- Used in both regular arrays and fixed arrays rendering paths
- Improves error handling for dynamic UI schema functions

Fixes CheckboxWidget components in Fluent UI and MUI packages:
- Changes onBlur/onFocus handlers to pass the checkbox checked state instead of its value

Fixes getDisplayLabel to safely handle undefined uiSchema
Updates all CheckboxWidget implementations to use the actual checked state
from the event target in onFocus and onBlur handlers instead of the
component's value prop.

This ensures callbacks receive the current checkbox state rather than
potentially stale values from the component props.
@chathuraa
Copy link
Contributor Author

hi @heath-freenome , I have fixed this issues

@nickgros
Copy link
Contributor

nickgros commented Aug 1, 2025

Sorry @chathuraa , we're still seeing failed tests on this PR

…errors

This commit fixes two potential issues:

1. Updates `createComponent` test utility to ensure Form components always receive
   a validator prop when rerendering, preventing potential test failures

2. Adds null checks for rootSchema in SchemaUtils to prevent errors when
   schema is undefined or null
@chathuraa
Copy link
Contributor Author

Hi @nickgros ,

I have fixed the issue and resubmitted.
CleanShot 2025-08-02 at 22 26 58

Cheers,
Chat.

@heath-freenome
Copy link
Member

@chathuraa looks like there are still some typing issue in the build

…Blur

Updates focus and blur event handlers in checkbox widgets across all UI libraries to use the current value state instead of reading from event target.

This ensures consistency across implementations and fixes potential issues where the event target might not correctly reflect the current checkbox state.
@chathuraa
Copy link
Contributor Author

Hi @heath-freenome ,

I have fixed the build errors.

CleanShot 2025-08-12 at 07 14 55

Cheers,
Chat.

@heath-freenome
Copy link
Member

@chathuraa And, one last thing, can you update the CHANGELOG.md to add your changes into it?

… fields

The change includes:
- documentation with TypeScript examples
- Updated array and uiSchema documentation to reference the new capabilities

Closes rjsf-team#4706
Extends the v6.x upgrade guide to document the new feature that allows dynamic UI schema generation for array items.

Includes a code example demonstrating the feature and references additional documentation.
Copy link
Member

@heath-freenome heath-freenome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few more improvement suggestions

@heath-freenome heath-freenome merged commit bd54461 into rjsf-team:main Aug 12, 2025
4 checks passed
@chathuraa
Copy link
Contributor Author

Thank you very much @nickgros & @heath-freenome .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants