From 6c9d24a6edf59f6ea2a62a17cb1683a946301a25 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Thu, 12 Mar 2020 14:59:37 +0100 Subject: [PATCH] Add saving and loading of custom classes [MAILPOET-2746] --- .../form_editor/store/blocks_to_form_body.jsx | 1 + .../form_editor/store/form_body_to_blocks.jsx | 5 ++++ .../store/blocks_to_form_body.spec.js | 25 ++++++++++++++++++- .../store/form_body_to_blocks.spec.js | 23 +++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/assets/js/src/form_editor/store/blocks_to_form_body.jsx b/assets/js/src/form_editor/store/blocks_to_form_body.jsx index 113fdb5ec2..e9206c3f06 100644 --- a/assets/js/src/form_editor/store/blocks_to_form_body.jsx +++ b/assets/js/src/form_editor/store/blocks_to_form_body.jsx @@ -117,6 +117,7 @@ export const blocksToFormBodyFactory = (colorDefinitions, customFields = []) => type: 'text', params: { label: block.attributes.label, + class_name: block.attributes.className || null, }, }; if (block.attributes.mandatory) { diff --git a/assets/js/src/form_editor/store/form_body_to_blocks.jsx b/assets/js/src/form_editor/store/form_body_to_blocks.jsx index 39dad09950..2a501ba9f2 100644 --- a/assets/js/src/form_editor/store/form_body_to_blocks.jsx +++ b/assets/js/src/form_editor/store/form_body_to_blocks.jsx @@ -174,8 +174,12 @@ export const formBodyToBlocksFactory = (colorDefinitions, customFields = []) => attributes: { labelWithinInput: false, mandatory: false, + className: null, }, }; + if (item.params && has(item.params, 'class_name')) { + mapped.attributes.className = item.params.class_name; + } if (item.params && has(item.params, 'required')) { mapped.attributes.mandatory = !!item.params.required; } @@ -273,6 +277,7 @@ export const formBodyToBlocksFactory = (colorDefinitions, customFields = []) => ...mapped, name: 'mailpoet-form/html', attributes: { + className: mapped.attributes.className, content: item.params && item.params.text ? item.params.text : '', nl2br: item.params && item.params.nl2br ? !!item.params.nl2br : false, }, diff --git a/tests/javascript/form_editor/store/blocks_to_form_body.spec.js b/tests/javascript/form_editor/store/blocks_to_form_body.spec.js index 5cbd1ef952..60efcf173d 100644 --- a/tests/javascript/form_editor/store/blocks_to_form_body.spec.js +++ b/tests/javascript/form_editor/store/blocks_to_form_body.spec.js @@ -427,7 +427,7 @@ describe('Blocks to Form Body', () => { expect(mapped2.params.background_color).to.be.equal('#bbbbbb'); }); - it('Should map class name for columns and column', () => { + it('Should map class names', () => { const columns = { ...nestedColumns }; columns.attributes = { className: 'my-class', @@ -441,5 +441,28 @@ describe('Blocks to Form Body', () => { }; const [mappedColumn] = formBlocksToBody([column]); expect(mappedColumn.params.class_name).to.be.equal('my-class-2'); + + const email = { ...emailBlock }; + email.attributes.className = 'my-class-3'; + const [mappedEmail] = formBlocksToBody([email]); + expect(mappedEmail.params.class_name).to.be.equal('my-class-3'); + + const customField = { + created_at: '2019-12-10T15:05:06+00:00', + id: 1, + name: 'Custom Field name', + params: { + label: 'Street name', + required: '1', + validate: '', + }, + type: 'text', + updated_at: '2019-12-10T15:05:06+00:00', + }; + const customText = { ...customTextBlock }; + customText.attributes.className = 'my-class-4'; + const map = blocksToFormBodyFactory(colorDefinitions, [customField]); + const [mappedCustomText] = map([customText]); + expect(mappedCustomText.params.class_name).to.be.equal('my-class-4'); }); }); diff --git a/tests/javascript/form_editor/store/form_body_to_blocks.spec.js b/tests/javascript/form_editor/store/form_body_to_blocks.spec.js index c47ebcd356..83932ddb74 100644 --- a/tests/javascript/form_editor/store/form_body_to_blocks.spec.js +++ b/tests/javascript/form_editor/store/form_body_to_blocks.spec.js @@ -485,6 +485,29 @@ describe('Form Body To Blocks', () => { }; const [block] = formBodyToBlocks([nested]); expect(block.attributes.className).to.be.equal('custom-class'); + + const email = { ...emailInput, position: '1' }; + email.params.class_name = 'custom-class-2'; + const [mappedEmail] = formBodyToBlocks([email]); + expect(mappedEmail.attributes.className).to.be.equal('custom-class-2'); + + const customField = { + created_at: '2019-12-10T15:05:06+00:00', + id: 1, + name: 'Custom Field ^name', + params: { + label: 'Street name', + required: '1', + validate: '', + }, + type: 'text', + updated_at: '2019-12-10T15:05:06+00:00', + }; + const customText = { ...customTextInput, position: '1' }; + customText.params.class_name = 'custom-class-3 custom-class-4'; + const map = formBodyToBlocksFactory(colorDefinitions, [customField]); + const [mappedCustomText] = map([customText]); + expect(mappedCustomText.attributes.className).to.be.equal('custom-class-3 custom-class-4'); }); it('It should map heading', () => {