Merge pull request #16158 from strapi/fix/single-components-always-rendering

This commit is contained in:
Josh 2023-03-22 11:59:55 +00:00 committed by GitHub
commit 31b09b3441
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 135 additions and 21 deletions

View File

@ -51,6 +51,27 @@
"basic.relation",
"basic.simple"
]
},
"single_relation": {
"type": "component",
"repeatable": false,
"pluginOptions": {
"i18n": {
"localized": true
}
},
"component": "basic.relation"
},
"require_single_relation": {
"type": "component",
"repeatable": false,
"pluginOptions": {
"i18n": {
"localized": true
}
},
"component": "basic.relation",
"required": true
}
}
}

View File

@ -220,7 +220,9 @@ const reducer = (state, action) =>
const findAllRelationsAndReplaceWithEmptyArray = findAllAndReplace(
components,
(value) => value.type === 'relation',
(value) => {
return value.type === 'relation';
},
(_, { path }) => {
if (state.modifiedData?.id === data.id && get(state.modifiedData, path)) {
return get(state.modifiedData, path);

View File

@ -571,7 +571,6 @@ describe('CONTENT MANAGER | COMPONENTS | EditViewDataManagerProvider | reducer',
level_one_repeatable: [
{
__temp_key__: 0,
level_two_single_component: {},
},
],
},

View File

@ -26,7 +26,11 @@ const findAllAndReplaceSetup = (components, predicate = () => false, replacement
/**
* @type {<TData extends object = object>(data: TData, attributes: Attributes, options?: { ignoreFalseyValues?: boolean}) => TData}
*/
const findAllAndReplace = (data, attributes, { ignoreFalseyValues = false, path = [] } = {}) => {
const findAllAndReplace = (
data,
attributes,
{ ignoreFalseyValues = false, path = [], parent = attributes } = {}
) => {
return Object.entries(attributes).reduce(
(acc, [key, value]) => {
if (
@ -36,7 +40,7 @@ const findAllAndReplaceSetup = (components, predicate = () => false, replacement
return acc;
}
if (predicate(value, { path: [...path, key], parent: acc })) {
if (predicate(value, { path: [...path, key], parent })) {
acc[key] =
typeof replacement === 'function'
? replacement(acc[key], { path: [...path, key], parent: acc })
@ -46,16 +50,18 @@ const findAllAndReplaceSetup = (components, predicate = () => false, replacement
if (value.type === 'component') {
const componentAttributes = components[value.component].attributes;
if (!value.repeatable) {
if (!value.repeatable && acc[key] && typeof acc[key] === 'object') {
acc[key] = findAllAndReplace(acc[key], componentAttributes, {
ignoreFalseyValues,
path: [...path, key],
parent: attributes[key],
});
} else if (value.repeatable && Array.isArray(acc[key])) {
acc[key] = acc[key].map((datum, index) => {
const data = findAllAndReplace(datum, componentAttributes, {
ignoreFalseyValues,
path: [...path, key, index],
parent: attributes[key],
});
return data;
@ -67,6 +73,7 @@ const findAllAndReplaceSetup = (components, predicate = () => false, replacement
const data = findAllAndReplace(datum, componentAttributes, {
ignoreFalseyValues,
path: [...path, key, index],
parent: attributes[key],
});
return data;

View File

@ -69,14 +69,11 @@ describe('findAllAndReplace', () => {
expect(data).toMatchInlineSnapshot(`
{
"categories": "replaced",
"comp_relation": {
"categories": "replaced",
},
}
`);
});
it('should replace relations in single components', () => {
it('should not replace relations in single components', () => {
const data = findAllAndReplace(
components,
(value) => value.type === 'relation',
@ -86,9 +83,6 @@ describe('findAllAndReplace', () => {
expect(data).toMatchInlineSnapshot(`
{
"categories": "replaced",
"comp_relation": {
"categories": "replaced",
},
}
`);
});
@ -103,9 +97,6 @@ describe('findAllAndReplace', () => {
expect(data).toMatchInlineSnapshot(`
{
"categories": "replaced",
"comp_relation": {
"categories": "replaced",
},
}
`);
});
@ -120,9 +111,6 @@ describe('findAllAndReplace', () => {
expect(data).toMatchInlineSnapshot(`
{
"categories": "replaced",
"comp_relation": {
"categories": "replaced",
},
}
`);
});
@ -137,9 +125,106 @@ describe('findAllAndReplace', () => {
expect(data).toMatchInlineSnapshot(`
{
"categories": "replaced",
"comp_relation": {
"categories": "replaced",
},
}
`);
});
});
describe('null values', () => {
const nullishData = {
categories: null,
repeatable_repeatable_relations: null,
repeatable_relations: null,
dynamic_relations: null,
comp_relation: null,
};
it('should replace the first level of relations', () => {
const data = findAllAndReplace(
components,
(value) => value.type === 'relation',
'replaced'
)(nullishData, schema);
expect(data).toMatchInlineSnapshot(`
{
"categories": "replaced",
"comp_relation": null,
"dynamic_relations": null,
"repeatable_relations": null,
"repeatable_repeatable_relations": null,
}
`);
});
it('should not replace relations in single components', () => {
const data = findAllAndReplace(
components,
(value) => value.type === 'relation',
'replaced'
)(nullishData, schema);
expect(data).toMatchInlineSnapshot(`
{
"categories": "replaced",
"comp_relation": null,
"dynamic_relations": null,
"repeatable_relations": null,
"repeatable_repeatable_relations": null,
}
`);
});
it('should not replace relation instances in a repeatable component', () => {
const data = findAllAndReplace(
components,
(value) => value.type === 'relation',
'replaced'
)(nullishData, schema);
expect(data).toMatchInlineSnapshot(`
{
"categories": "replaced",
"comp_relation": null,
"dynamic_relations": null,
"repeatable_relations": null,
"repeatable_repeatable_relations": null,
}
`);
});
it('should not replace all relation instances in nested repeatable components', () => {
const data = findAllAndReplace(
components,
(value) => value.type === 'relation',
'replaced'
)(nullishData, schema);
expect(data).toMatchInlineSnapshot(`
{
"categories": "replaced",
"comp_relation": null,
"dynamic_relations": null,
"repeatable_relations": null,
"repeatable_repeatable_relations": null,
}
`);
});
it('should not replace relation instances in dynamic zones correctly', () => {
const data = findAllAndReplace(
components,
(value) => value.type === 'relation',
'replaced'
)(nullishData, schema);
expect(data).toMatchInlineSnapshot(`
{
"categories": "replaced",
"comp_relation": null,
"dynamic_relations": null,
"repeatable_relations": null,
"repeatable_repeatable_relations": null,
}
`);
});