From 31972a5cf2cd97789e687755fc1718464eaff823 Mon Sep 17 00:00:00 2001 From: Eugen Neufeld Date: Mon, 20 Jun 2022 15:07:08 +0200 Subject: [PATCH] Improve vanilla ArrayControl The vanilla ArrayControl was wrapping the `addItem` in a function, whereas the `addItem` function is already wrapped. So the `onClick` can directly be bound to `addItem`. The wrong binding lead to the add functionality not working. Increase rank of the ArrayControl so that it behaves the same as for the react material renderers. Add example to demonstrate different selections of renderers based on the existince of a const property in the schema. Fix wrong selection of array layout if type is missing. Fixes #1948 --- packages/core/src/testers/testers.ts | 21 ++--- packages/examples/src/examples/1948.ts | 87 +++++++++++++++++++ packages/examples/src/index.ts | 2 + .../src/complex/array/ArrayControl.tsx | 2 +- packages/vanilla/src/complex/array/index.ts | 2 +- 5 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 packages/examples/src/examples/1948.ts diff --git a/packages/core/src/testers/testers.ts b/packages/core/src/testers/testers.ts index 962987677..70a89feea 100644 --- a/packages/core/src/testers/testers.ts +++ b/packages/core/src/testers/testers.ts @@ -445,10 +445,7 @@ export const isObjectArrayWithNesting = ( } const schemaPath = (uischema as ControlElement).scope; const resolvedSchema = resolveSchema(schema, schemaPath, rootSchema ?? schema); - const wantedNestingByType: { [key: string]: number } = { - object: 2, - array: 1 - }; + let objectDepth = 0; if (resolvedSchema !== undefined && resolvedSchema.items !== undefined) { // check if nested arrays if ( @@ -459,16 +456,16 @@ export const isObjectArrayWithNesting = ( if (val.$ref !== undefined) { return false; } - // we don't support multiple types - if (typeof val.type !== 'string') { - return true; + if (hasType(val, 'object')) { + objectDepth++; + if (objectDepth === 2) { + return true; + } } - const typeCount = wantedNestingByType[val.type]; - if (typeCount === undefined) { - return false; + if (hasType(val, 'array')) { + return true; } - wantedNestingByType[val.type] = typeCount - 1; - return wantedNestingByType[val.type] === 0; + return false; }, rootSchema) ) { return true; diff --git a/packages/examples/src/examples/1948.ts b/packages/examples/src/examples/1948.ts new file mode 100644 index 000000000..a4d8abbde --- /dev/null +++ b/packages/examples/src/examples/1948.ts @@ -0,0 +1,87 @@ +/* + The MIT License + + Copyright (c) 2022 EclipseSource Munich + https://github.com/eclipsesource/jsonforms + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the 'Software'), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +import { registerExamples } from '../register'; +import { UISchemaElement } from '@jsonforms/core'; + +export const schema = { + type: 'object', + definitions: { + import: { + title: 'Import', + type: 'object', + properties: { + eClass: { + const: 'http://my_schema/1.0.0#//Import' + }, + document: { + type: 'string' + }, + package: { + type: 'string' + }, + prefix: { + type: 'string' + } + } + } + }, + properties: { + import: { + type: 'array', + items: { + $ref: '#/definitions/import' + } + } + } +}; + +export const uischema: UISchemaElement = undefined; + +export const data = { + import: [ + { + document: 'Document1', + package: 'Package1', + prefix: 'Prefix' + } + ] +}; + +registerExamples([ + { + name: '1948_with', + label: 'Issue 1948 with schema', + data, + schema, + uischema + }, + { + name: '1948_without', + label: 'Issue 1948 w/o schema', + data, + schema: undefined, + uischema + } +]); diff --git a/packages/examples/src/index.ts b/packages/examples/src/index.ts index 89fd97102..73fd1cf15 100644 --- a/packages/examples/src/index.ts +++ b/packages/examples/src/index.ts @@ -59,6 +59,7 @@ import * as listWithDetail from './examples/list-with-detail'; import * as listWithDetailRegistered from './examples/list-with-detail-registered'; import * as object from './examples/object'; import * as i18n from './examples/i18n'; +import * as issue_1948 from './examples/1948'; import * as issue_1169 from './examples/1169'; import * as issue_1220 from './examples/1220'; import * as issue_1253 from './examples/1253'; @@ -83,6 +84,7 @@ export * from './example'; import * as ifThenElse from './examples/if_then_else'; export { + issue_1948, defaultExample, allOf, anyOf, diff --git a/packages/vanilla/src/complex/array/ArrayControl.tsx b/packages/vanilla/src/complex/array/ArrayControl.tsx index 859dece46..a9b86a9d0 100644 --- a/packages/vanilla/src/complex/array/ArrayControl.tsx +++ b/packages/vanilla/src/complex/array/ArrayControl.tsx @@ -50,7 +50,7 @@ export const ArrayControl = ({ diff --git a/packages/vanilla/src/complex/array/index.ts b/packages/vanilla/src/complex/array/index.ts index 19757364c..80e8d9d26 100644 --- a/packages/vanilla/src/complex/array/index.ts +++ b/packages/vanilla/src/complex/array/index.ts @@ -33,7 +33,7 @@ import { ArrayControl } from './ArrayControl'; export { ArrayControl, ArrayControlRenderer }; export const arrayControlTester: RankedTester = rankWith( - 2, + 4, isObjectArrayWithNesting );