Skip to content

Commit

Permalink
create field for references and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed May 14, 2020
1 parent 9e6ebc5 commit dab3b5c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ describe('createFieldList', () => {
"type": "boolean",
"value": true,
},
Object {
"name": "references",
"type": "array",
"value": "[]",
},
]
`);
});
Expand All @@ -76,6 +81,11 @@ describe('createFieldList', () => {
\\"data\\": \\"value\\"
}",
},
Object {
"name": "references",
"type": "array",
"value": "[]",
},
]
`);
});
Expand All @@ -93,6 +103,48 @@ describe('createFieldList', () => {
1,
2,
3
]",
},
Object {
"name": "references",
"type": "array",
"value": "[]",
},
]
`);
});

it(`generates a field for the object's references`, () => {
const obj = createObject(
{
someString: 'foo',
},
[
{ id: 'ref1', type: 'type', name: 'Ref 1' },
{ id: 'ref12', type: 'other-type', name: 'Ref 2' },
]
);
expect(createFieldList(obj)).toMatchInlineSnapshot(`
Array [
Object {
"name": "someString",
"type": "text",
"value": "foo",
},
Object {
"name": "references",
"type": "array",
"value": "[
{
\\"id\\": \\"ref1\\",
\\"type\\": \\"type\\",
\\"name\\": \\"Ref 1\\"
},
{
\\"id\\": \\"ref12\\",
\\"type\\": \\"other-type\\",
\\"name\\": \\"Ref 2\\"
}
]",
},
]
Expand Down Expand Up @@ -126,6 +178,11 @@ describe('createFieldList', () => {
"type": "text",
"value": "B",
},
Object {
"name": "references",
"type": "array",
"value": "[]",
},
]
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ export function createFieldList(
object: SimpleSavedObject,
service?: SavedObjectLoader
): ObjectField[] {
const fields = Object.entries(object.attributes as Record<string, any>).reduce(
let fields = Object.entries(object.attributes as Record<string, any>).reduce(
(objFields, [key, value]) => {
return [...objFields, ...recursiveCreateFields(key, value)];
return [...objFields, ...createFields(key, value)];
},
[] as ObjectField[]
);
// Special handling for references which isn't within "attributes"
fields = [...fields, ...createFields('references', object.references)];

if (service && (service as any).Class) {
addFieldsFromClass((service as any).Class, fields);
}
Expand All @@ -53,7 +56,7 @@ export function createFieldList(
* @param {array} parents The parent keys to the field
* @returns {array}
*/
const recursiveCreateFields = (key: string, value: any, parents: string[] = []): ObjectField[] => {
const createFields = (key: string, value: any, parents: string[] = []): ObjectField[] => {
const path = [...parents, key];
if (path.length > maxRecursiveIterations) {
return [];
Expand All @@ -78,7 +81,7 @@ const recursiveCreateFields = (key: string, value: any, parents: string[] = []):
} else if (isPlainObject(field.value)) {
let fields: ObjectField[] = [];
forOwn(field.value, (childValue, childKey) => {
fields = [...fields, ...recursiveCreateFields(childKey as string, childValue, path)];
fields = [...fields, ...createFields(childKey as string, childValue, path)];
});
return fields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export class Form extends Component<FormProps, FormState> {
set(source, field.name, value);
});

// we extract the `references` field that does not belong to attributes
const { references, ...attributes } = source;

await onSave({ attributes, references });
Expand Down

0 comments on commit dab3b5c

Please sign in to comment.