From b4385d7a4eb115d54c582efada3cc9228ba76f4b Mon Sep 17 00:00:00 2001 From: Eugen Neufeld Date: Tue, 13 Jul 2021 18:44:42 +0200 Subject: [PATCH] Improve label handling in MasterListComponent The MasterListComponent did only show labels for object lists. Furthermore the options property in the uischema was mandatory. Also the property to use as label for objects had to be provided. The fix now handles primitives, a missing options property and in the case of a missing `labelRef` the first primitive property is used. If no primitive property is available the first property in the schema is used. Fix #1779 --- .../src/other/master-detail/master.ts | 17 +++- packages/examples/src/1779.ts | 83 ++++++++++++++++ packages/examples/src/index.ts | 4 +- packages/examples/src/list-with-detail.ts | 99 +++++++++++++++++++ 4 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 packages/examples/src/1779.ts diff --git a/packages/angular-material/src/other/master-detail/master.ts b/packages/angular-material/src/other/master-detail/master.ts index ee44b283f2..c3d54a097f 100644 --- a/packages/angular-material/src/other/master-detail/master.ts +++ b/packages/angular-material/src/other/master-detail/master.ts @@ -35,6 +35,7 @@ import { createDefaultValue, findUISchema, JsonFormsState, + JsonSchema, mapDispatchToArrayControlProps, mapStateToArrayControlProps, RankedTester, @@ -170,7 +171,7 @@ export class MasterListComponent extends JsonFormsArrayControl { const controlElement = uischema as ControlElement; this.propsPath = props.path; const detailUISchema = - controlElement.options.detail || + controlElement.options?.detail || findUISchema( props.uischemas, schema, @@ -180,11 +181,12 @@ export class MasterListComponent extends JsonFormsArrayControl { ); const masterItems = (data || []).map((d: any, index: number) => { - const labelRefInstancePath = removeSchemaKeywords( + const labelRefInstancePath = controlElement.options?.labelRef && removeSchemaKeywords( controlElement.options.labelRef ); + const isPrimitive = d && typeof d !== 'object'; const masterItem = { - label: get(d, labelRefInstancePath), + label: isPrimitive ? d.toString() : get(d, labelRefInstancePath ?? getFirstUsableProperty(schema)), data: d, path: `${path}.${index}`, schema, @@ -247,6 +249,15 @@ export class MasterListComponent extends JsonFormsArrayControl { } } +const getFirstUsableProperty = (schema: JsonSchema): string => { + const prop = Object.entries(schema?.properties).find(e => { + if(e[1].type === 'object' || e[1].properties) return false; + if(e[1].type === 'array' || e[1].items) return false; + return true; + }); + return prop ? prop[0] : Object.keys(schema?.properties)[0]; +} + export const masterDetailTester: RankedTester = rankWith( 4, uiTypeIs('ListWithDetail') diff --git a/packages/examples/src/1779.ts b/packages/examples/src/1779.ts new file mode 100644 index 0000000000..e5053b8e17 --- /dev/null +++ b/packages/examples/src/1779.ts @@ -0,0 +1,83 @@ +/* + The MIT License + + Copyright (c) 2017-2021 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'; + +const data = { + 'an-array-of-strings': ['foo', 'bar', 'foobar'] +}; +const schema = { + type: 'object', + properties: { + 'an-array-of-strings': { + type: 'array', + items: { + type: 'string' + } + } + } +}; +const uischema = { + "type": "ListWithDetail", + "scope": "#/properties/an-array-of-strings" +}; + +registerExamples([ + { + name: '1779', + label: 'List With Detail primitive (string)', + data, + schema, + uischema + } +]); + +const data_number = { + 'an-array-of-numbers': [1, 2, 3] +}; +const schema_number = { + type: 'object', + properties: { + 'an-array-of-numbers': { + type: 'array', + items: { + type: 'number' + } + } + } +}; +const uischema_number = { + "type": "ListWithDetail", + "scope": "#/properties/an-array-of-numbers" +}; + +registerExamples([ + { + name: '1779', + label: 'List With Detail primitive (string)', + data: data_number, + schema: schema_number, + uischema: uischema_number + } +]); \ No newline at end of file diff --git a/packages/examples/src/index.ts b/packages/examples/src/index.ts index b651fb880d..875a5bc103 100644 --- a/packages/examples/src/index.ts +++ b/packages/examples/src/index.ts @@ -74,6 +74,7 @@ import * as booleanToggle from './booleanToggle'; import * as multiEnum from './multi-enum'; import * as enumInArray from './enumInArray'; import * as readonly from './readonly'; +import * as bug_1779 from './1779'; export * from './register'; export * from './example'; @@ -132,5 +133,6 @@ export { booleanToggle, multiEnum, enumInArray, - readonly + readonly, + bug_1779 }; diff --git a/packages/examples/src/list-with-detail.ts b/packages/examples/src/list-with-detail.ts index 444121cd09..54c5edd291 100644 --- a/packages/examples/src/list-with-detail.ts +++ b/packages/examples/src/list-with-detail.ts @@ -195,6 +195,95 @@ const uischema = { } }; +const uischemaNoLabelRef = { + type: 'ListWithDetail', + scope: '#/properties/orders', + options: { + detail: { + type: 'VerticalLayout', + elements: [ + { + type: 'HorizontalLayout', + elements: [ + { + type: 'Control', + scope: '#/properties/title' + }, + { + type: 'Control', + scope: '#/properties/processId' + } + ] + }, + { + type: 'Group', + label: 'Customer', + elements: [ + { + type: 'Control', + label: 'ID', + scope: '#/properties/customer/properties/id' + }, + { + type: 'Control', + label: 'Name', + scope: '#/properties/customer/properties/name' + }, + { + type: 'Control', + label: 'Department', + scope: '#/properties/customer/properties/department' + } + ] + }, + { + type: 'VerticalLayout', + elements: [ + { + type: 'VerticalLayout', + elements: [ + { + type: 'HorizontalLayout', + elements: [ + { + type: 'Control', + scope: '#/properties/ordered', + options: { + toggle: true + } + }, + { + type: 'Control', + scope: '#/properties/assignee' + } + ] + }, + { + type: 'HorizontalLayout', + elements: [ + { + type: 'Control', + scope: '#/properties/startDate' + }, + { + type: 'Control', + scope: '#/properties/endDate' + } + ] + }, + { + type: 'Control', + scope: '#/properties/status' + } + ] + } + ] + } + ] + } + } +}; + registerExamples([ { name: 'list-with-detail', @@ -204,3 +293,13 @@ registerExamples([ uischema } ]); + +registerExamples([ + { + name: 'list-with-detail-no-labelref', + label: 'List With Detail (No Label Ref)', + data, + schema, + uischema:uischemaNoLabelRef + } +]);