Skip to content

Commit

Permalink
Improve label handling in MasterListComponent
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eneufeld committed Jul 13, 2021
1 parent 96e2c41 commit b4385d7
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 4 deletions.
17 changes: 14 additions & 3 deletions packages/angular-material/src/other/master-detail/master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
createDefaultValue,
findUISchema,
JsonFormsState,
JsonSchema,
mapDispatchToArrayControlProps,
mapStateToArrayControlProps,
RankedTester,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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')
Expand Down
83 changes: 83 additions & 0 deletions packages/examples/src/1779.ts
Original file line number Diff line number Diff line change
@@ -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
}
]);
4 changes: 3 additions & 1 deletion packages/examples/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -132,5 +133,6 @@ export {
booleanToggle,
multiEnum,
enumInArray,
readonly
readonly,
bug_1779
};
99 changes: 99 additions & 0 deletions packages/examples/src/list-with-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -204,3 +293,13 @@ registerExamples([
uischema
}
]);

registerExamples([
{
name: 'list-with-detail-no-labelref',
label: 'List With Detail (No Label Ref)',
data,
schema,
uischema:uischemaNoLabelRef
}
]);

0 comments on commit b4385d7

Please sign in to comment.