From 07fe0f72e1f601f5b46e076d620bdb9bb9eccb72 Mon Sep 17 00:00:00 2001 From: Lucas Koehler Date: Fri, 31 Jan 2025 14:34:08 +0100 Subject: [PATCH] Remove mapStateToCombinatorRendererProps usage from mapStateToAllOfProps In contrast to oneOf and anyOf rendererer, allOf renderers do not need the `indexOfFittingSchema` because all schemas apply at once. Thus, remove using mapStateToCombinatorRendererProps from mapStateToAllOfProps and, with this, the unnecessary calculation of the index. Part of #2371 --- packages/core/src/mappers/renderer.ts | 27 +++++++++++++++++-- .../src/complex/MaterialAllOfRenderer.tsx | 4 +-- packages/react/src/JsonFormsContext.tsx | 7 ++--- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/packages/core/src/mappers/renderer.ts b/packages/core/src/mappers/renderer.ts index 89119aca4..0bde958cb 100644 --- a/packages/core/src/mappers/renderer.ts +++ b/packages/core/src/mappers/renderer.ts @@ -1123,6 +1123,11 @@ export interface StatePropsOfCombinator extends StatePropsOfControl { data: any; } +export type StatePropsOfAllOfRenderer = Omit< + StatePropsOfCombinator, + 'indexOfFittingSchema' +>; + export const mapStateToCombinatorRendererProps = ( state: JsonFormsState, ownProps: OwnPropsOfControl, @@ -1155,6 +1160,12 @@ export const mapStateToCombinatorRendererProps = ( export interface CombinatorRendererProps extends StatePropsOfCombinator, DispatchPropsOfControl {} + +export type AllOfRendererProps = Omit< + CombinatorRendererProps, + 'indexOfFittingSchema' +>; + /** * Map state to all of renderer props. * @param state the store's state @@ -1164,8 +1175,20 @@ export interface CombinatorRendererProps export const mapStateToAllOfProps = ( state: JsonFormsState, ownProps: OwnPropsOfControl -): StatePropsOfCombinator => - mapStateToCombinatorRendererProps(state, ownProps, 'allOf'); +): StatePropsOfAllOfRenderer => { + const { data, schema, rootSchema, i18nKeyPrefix, label, ...props } = + mapStateToControlProps(state, ownProps); + + return { + data, + schema, + rootSchema, + ...props, + i18nKeyPrefix, + label, + uischemas: getUISchemas(state), + }; +}; export const mapStateToAnyOfProps = ( state: JsonFormsState, diff --git a/packages/material-renderers/src/complex/MaterialAllOfRenderer.tsx b/packages/material-renderers/src/complex/MaterialAllOfRenderer.tsx index 213fe41c0..5434d54d4 100644 --- a/packages/material-renderers/src/complex/MaterialAllOfRenderer.tsx +++ b/packages/material-renderers/src/complex/MaterialAllOfRenderer.tsx @@ -31,7 +31,7 @@ import { JsonSchema, RankedTester, rankWith, - StatePropsOfCombinator, + StatePropsOfAllOfRenderer, } from '@jsonforms/core'; import { JsonFormsDispatch, withJsonFormsAllOfProps } from '@jsonforms/react'; @@ -44,7 +44,7 @@ export const MaterialAllOfRenderer = ({ path, uischemas, uischema, -}: StatePropsOfCombinator) => { +}: StatePropsOfAllOfRenderer) => { const delegateUISchema = findMatchingUISchema(uischemas)( schema, uischema.scope, diff --git a/packages/react/src/JsonFormsContext.tsx b/packages/react/src/JsonFormsContext.tsx index b10323b65..53980a5b4 100644 --- a/packages/react/src/JsonFormsContext.tsx +++ b/packages/react/src/JsonFormsContext.tsx @@ -81,6 +81,7 @@ import { arrayDefaultTranslations, getArrayTranslations, ArrayTranslations, + AllOfRendererProps, } from '@jsonforms/core'; import debounce from 'lodash/debounce'; import React, { @@ -561,12 +562,12 @@ const withContextToAnyOfProps = ( }; const withContextToAllOfProps = ( - Component: ComponentType + Component: ComponentType ): ComponentType => function WithContextToAllOfProps({ ctx, props, - }: JsonFormsStateContext & CombinatorRendererProps) { + }: JsonFormsStateContext & AllOfRendererProps) { const allOfProps = ctxToAllOfProps(ctx, props); const dispatchProps = ctxDispatchToControlProps(ctx.dispatch); return ; @@ -764,7 +765,7 @@ export const withJsonFormsAnyOfProps = ( ); export const withJsonFormsAllOfProps = ( - Component: ComponentType, + Component: ComponentType, memoize = true ): ComponentType => withJsonFormsContext(