Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initialValue block setting #5978

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/volto/news/5971.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `initialValue` block setting. @wesleybl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import { Icon } from '@plone/volto/components';
import {
applyBlockInitialValue,
getBlocksFieldname,
blockHasValue,
buildStyleClassNamesFromData,
buildStyleObjectFromData,
Expand Down Expand Up @@ -110,7 +112,21 @@ const EditBlockWrapper = (props) => {
if (blockHasValue(data)) {
onSelectBlock(onInsertBlock(id, value));
} else {
onChangeBlock(id, value);
const blocksFieldname = getBlocksFieldname(properties);
const newFormData = applyBlockInitialValue({
id,
value,
blocksConfig,
formData: {
...properties,
[blocksFieldname]: {
...properties[blocksFieldname],
[id]: value || null,
},
},
});
const newValue = newFormData[blocksFieldname][id];
onChangeBlock(id, newValue);
}
}}
onMutateBlock={onMutateBlock}
Expand Down
19 changes: 12 additions & 7 deletions packages/volto/src/helpers/Blocks/Blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function addBlock(formData, type, index, blocksConfig) {

return [
id,
_applyBlockInitialValue({
applyBlockInitialValue({
id,
value,
blocksConfig,
Expand Down Expand Up @@ -193,8 +193,12 @@ export function addBlock(formData, type, index, blocksConfig) {
* to call `onChangeBlock` at their creation time, as this is prone to racing
* issue on block data storage.
*/
const _applyBlockInitialValue = ({ id, value, blocksConfig, formData }) => {
const blocksFieldname = getBlocksFieldname(formData);
export const applyBlockInitialValue = ({
id,
value,
blocksConfig,
formData,
}) => {
const type = value['@type'];
blocksConfig = blocksConfig || config.blocks.blocksConfig;

Expand All @@ -204,6 +208,7 @@ const _applyBlockInitialValue = ({ id, value, blocksConfig, formData }) => {
value,
formData,
});
const blocksFieldname = getBlocksFieldname(formData);
formData[blocksFieldname][id] = value;
}

Expand Down Expand Up @@ -234,7 +239,7 @@ export function mutateBlock(formData, id, value, blocksConfig) {
const trailId = formData[blocksLayoutFieldname].items[index];
if (trailId) {
const block = formData[blocksFieldname][trailId];
newFormData = _applyBlockInitialValue({
newFormData = applyBlockInitialValue({
id,
value,
blocksConfig,
Expand All @@ -252,7 +257,7 @@ export function mutateBlock(formData, id, value, blocksConfig) {
}

const idTrailingBlock = uuid();
newFormData = _applyBlockInitialValue({
newFormData = applyBlockInitialValue({
id,
value,
blocksConfig,
Expand Down Expand Up @@ -303,8 +308,8 @@ export function insertBlock(
});

const newBlockId = uuid();
const newFormData = _applyBlockInitialValue({
id,
const newFormData = applyBlockInitialValue({
id: newBlockId,
value,
blocksConfig,
formData: {
Expand Down
15 changes: 15 additions & 0 deletions packages/volto/src/helpers/Blocks/Blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,21 @@ describe('Blocks', () => {
});
});

it('initializes data for new block with initialValue in insertBlock', () => {
const [newId, form] = insertBlock(
{
blocks: { a: { value: 1 }, b: { value: 2 } },
blocks_layout: { items: ['a', 'b'] },
},
'b',
{ '@type': 'dummyText' },
);
expect(form.blocks[newId]).toStrictEqual({
'@type': 'dummyText',
marker: true,
});
});

it('initializes data for new block based on schema defaults', () => {
const [newId, form] = addBlock(
{
Expand Down
1 change: 1 addition & 0 deletions packages/volto/src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export {
getLanguageIndependentFields,
} from '@plone/volto/helpers/Content/Content';
export {
applyBlockInitialValue,
addBlock,
insertBlock,
blockHasValue,
Expand Down