Skip to content

Commit

Permalink
Fix initialValue block setting (#5978)
Browse files Browse the repository at this point in the history
Co-authored-by: David Glick <[email protected]>
  • Loading branch information
wesleybl and davisagli authored Aug 26, 2024
1 parent a79ebb4 commit 84b7ee8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
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 @@ -111,7 +113,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 @@ -161,7 +161,7 @@ export function addBlock(formData, type, index, blocksConfig) {

return [
id,
_applyBlockInitialValue({
applyBlockInitialValue({
id,
value,
blocksConfig,
Expand Down Expand Up @@ -197,8 +197,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 @@ -208,6 +212,7 @@ const _applyBlockInitialValue = ({ id, value, blocksConfig, formData }) => {
value,
formData,
});
const blocksFieldname = getBlocksFieldname(formData);
formData[blocksFieldname][id] = value;
}

Expand Down Expand Up @@ -238,7 +243,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 @@ -256,7 +261,7 @@ export function mutateBlock(formData, id, value, blocksConfig) {
}

const idTrailingBlock = uuid();
newFormData = _applyBlockInitialValue({
newFormData = applyBlockInitialValue({
id,
value,
blocksConfig,
Expand Down Expand Up @@ -307,8 +312,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 @@ -511,6 +511,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 @@ -40,6 +40,7 @@ export {
getLanguageIndependentFields,
} from '@plone/volto/helpers/Content/Content';
export {
applyBlockInitialValue,
addBlock,
insertBlock,
blockHasValue,
Expand Down

0 comments on commit 84b7ee8

Please sign in to comment.