Save and load radio custom field
[MAILPOET-2453]
This commit is contained in:
committed by
Rostislav Wolný
parent
cb7c7bc1f6
commit
9afba728c6
@@ -2,15 +2,29 @@
|
|||||||
const mapCustomField = (block, customFields, mappedCommonProperties) => {
|
const mapCustomField = (block, customFields, mappedCommonProperties) => {
|
||||||
const customField = customFields.find((cf) => cf.id === block.attributes.customFieldId);
|
const customField = customFields.find((cf) => cf.id === block.attributes.customFieldId);
|
||||||
if (!customField) return null;
|
if (!customField) return null;
|
||||||
return {
|
const typesMap = {
|
||||||
|
'mailpoet-form/custom-text': 'text',
|
||||||
|
'mailpoet-form/custom-textarea': 'textarea',
|
||||||
|
'mailpoet-form/custom-radio': 'radio',
|
||||||
|
};
|
||||||
|
const mapped = {
|
||||||
...mappedCommonProperties,
|
...mappedCommonProperties,
|
||||||
id: block.attributes.customFieldId.toString(),
|
id: block.attributes.customFieldId.toString(),
|
||||||
name: customField.name,
|
name: customField.name,
|
||||||
params: {
|
type: typesMap[block.name],
|
||||||
...mappedCommonProperties.params,
|
|
||||||
validate: block.attributes.validate,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
if (Object.prototype.hasOwnProperty.call(block.attributes, 'validate')) {
|
||||||
|
mapped.params.validate = block.attributes.validate;
|
||||||
|
}
|
||||||
|
if (Object.prototype.hasOwnProperty.call(block.attributes, 'displayLabel') && block.attributes.displayLabel) {
|
||||||
|
mapped.params.display_label = '1';
|
||||||
|
}
|
||||||
|
if (Object.prototype.hasOwnProperty.call(block.attributes, 'values')) {
|
||||||
|
mapped.params.values = block.attributes.values.map((value) => ({
|
||||||
|
value: value.name,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
return mapped;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -7,18 +7,23 @@ const mapCustomField = (item, customFields, mappedCommonProperties) => {
|
|||||||
const namesMap = {
|
const namesMap = {
|
||||||
text: 'mailpoet-form/custom-text',
|
text: 'mailpoet-form/custom-text',
|
||||||
textarea: 'mailpoet-form/custom-textarea',
|
textarea: 'mailpoet-form/custom-textarea',
|
||||||
|
radio: 'mailpoet-form/custom-radio',
|
||||||
};
|
};
|
||||||
const mapped = {
|
const mapped = {
|
||||||
...mappedCommonProperties,
|
...mappedCommonProperties,
|
||||||
name: formatCustomFieldBlockName(namesMap[customField.type], customField),
|
name: formatCustomFieldBlockName(namesMap[customField.type], customField),
|
||||||
};
|
};
|
||||||
mapped.attributes.customFieldId = customField.id;
|
mapped.attributes.customFieldId = customField.id;
|
||||||
if (
|
if (item.params) {
|
||||||
item.params
|
if (Object.prototype.hasOwnProperty.call(item.params, 'validate') && !!item.params.validate) {
|
||||||
&& Object.prototype.hasOwnProperty.call(item.params, 'validate')
|
mapped.attributes.validate = item.params.validate;
|
||||||
&& !!item.params.validate
|
}
|
||||||
) {
|
if (Object.prototype.hasOwnProperty.call(item.params, 'display_label')) {
|
||||||
mapped.attributes.validate = item.params.validate;
|
mapped.attributes.displayLabel = !!item.params.display_label;
|
||||||
|
}
|
||||||
|
if (Object.prototype.hasOwnProperty.call(item.params, 'values') && Array.isArray(item.params.values)) {
|
||||||
|
mapped.attributes.values = item.params.values.map((value) => ({ name: value.value }));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return mapped;
|
return mapped;
|
||||||
};
|
};
|
||||||
|
@@ -71,6 +71,22 @@ const customTextBlock = {
|
|||||||
customFieldId: 1,
|
customFieldId: 1,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const customRadioBlock = {
|
||||||
|
clientId: '4',
|
||||||
|
isValid: true,
|
||||||
|
innerBlocks: [],
|
||||||
|
name: 'mailpoet-form/custom-radio',
|
||||||
|
attributes: {
|
||||||
|
label: 'Options',
|
||||||
|
displayLabel: true,
|
||||||
|
mandatory: true,
|
||||||
|
customFieldId: 2,
|
||||||
|
values: [
|
||||||
|
{ name: 'option 1' },
|
||||||
|
{ name: 'option 2' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const dividerBlock = {
|
const dividerBlock = {
|
||||||
clientId: 'some_random_123',
|
clientId: 'some_random_123',
|
||||||
@@ -261,6 +277,35 @@ describe('Blocks to Form Body', () => {
|
|||||||
expect(input.params.validate).to.eq('alphanum');
|
expect(input.params.validate).to.eq('alphanum');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should map custom radio field', () => {
|
||||||
|
const customField = {
|
||||||
|
created_at: '2019-12-10T15:05:06+00:00',
|
||||||
|
id: 2,
|
||||||
|
name: 'Custom Field name',
|
||||||
|
params: {
|
||||||
|
label: 'Options',
|
||||||
|
required: '1',
|
||||||
|
values: [
|
||||||
|
{ value: 'option 1' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
type: 'radio',
|
||||||
|
updated_at: '2019-12-10T15:05:06+00:00',
|
||||||
|
};
|
||||||
|
const [input] = formBlocksToBody([customRadioBlock], [customField]);
|
||||||
|
checkBodyInputBasics(input);
|
||||||
|
expect(input.id).to.be.equal('2');
|
||||||
|
expect(input.name).to.be.equal('Custom Field name');
|
||||||
|
expect(input.type).to.be.equal('radio');
|
||||||
|
expect(input.position).to.be.equal('1');
|
||||||
|
expect(input.params.label).to.be.equal('Options');
|
||||||
|
expect(input.params.required).to.be.eq('1');
|
||||||
|
expect(input.params.display_label).to.eq('1');
|
||||||
|
expect(input.params.values).to.be.an('Array').that.has.length(2);
|
||||||
|
expect(input.params.values[0]).to.have.property('value', 'option 1');
|
||||||
|
expect(input.params.values[1]).to.have.property('value', 'option 2');
|
||||||
|
});
|
||||||
|
|
||||||
it('Should map multiple blocks at once', () => {
|
it('Should map multiple blocks at once', () => {
|
||||||
const unknownBlock = {
|
const unknownBlock = {
|
||||||
name: 'unknown',
|
name: 'unknown',
|
||||||
|
@@ -85,7 +85,24 @@ const customTextInput = {
|
|||||||
},
|
},
|
||||||
position: null,
|
position: null,
|
||||||
};
|
};
|
||||||
|
const customRadioInput = {
|
||||||
|
type: 'radio',
|
||||||
|
name: 'Options',
|
||||||
|
id: '3',
|
||||||
|
unique: '1',
|
||||||
|
static: '0',
|
||||||
|
params: {
|
||||||
|
required: '',
|
||||||
|
label: 'Options',
|
||||||
|
display_label: '1',
|
||||||
|
values: [
|
||||||
|
{
|
||||||
|
value: 'option 1',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
position: null,
|
||||||
|
};
|
||||||
const divider = {
|
const divider = {
|
||||||
type: 'divider',
|
type: 'divider',
|
||||||
name: 'Divider',
|
name: 'Divider',
|
||||||
@@ -271,6 +288,34 @@ describe('Form Body To Blocks', () => {
|
|||||||
expect(block.attributes.validate).to.be.equal('alphanum');
|
expect(block.attributes.validate).to.be.equal('alphanum');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should map custom radio input to block', () => {
|
||||||
|
const customField = {
|
||||||
|
created_at: '2019-12-10T15:05:06+00:00',
|
||||||
|
id: 3,
|
||||||
|
name: 'Name',
|
||||||
|
params: {
|
||||||
|
required: '1',
|
||||||
|
label: 'Options 123',
|
||||||
|
display_label: '',
|
||||||
|
values: [
|
||||||
|
{ value: 'option 1' },
|
||||||
|
{ value: 'option 2' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
type: 'radio',
|
||||||
|
updated_at: '2019-12-10T15:05:06+00:00',
|
||||||
|
};
|
||||||
|
const [block] = formBodyToBlocks([{ ...customRadioInput, position: '1' }], [customField]);
|
||||||
|
checkBlockBasics(block);
|
||||||
|
expect(block.clientId).to.be.equal('3_0');
|
||||||
|
expect(block.name).to.be.equal('mailpoet-form/custom-radio-name');
|
||||||
|
expect(block.attributes.label).to.be.equal('Options');
|
||||||
|
expect(block.attributes.mandatory).to.be.equal(false);
|
||||||
|
expect(block.attributes.displayLabel).to.be.equal(true);
|
||||||
|
expect(block.attributes.values).to.be.an('Array').that.has.length(1);
|
||||||
|
expect(block.attributes.values[0]).to.have.property('name', 'option 1');
|
||||||
|
});
|
||||||
|
|
||||||
it('Should ignore unknown input type', () => {
|
it('Should ignore unknown input type', () => {
|
||||||
const blocks = formBodyToBlocks([{ ...submitInput, id: 'some-nonsense' }]);
|
const blocks = formBodyToBlocks([{ ...submitInput, id: 'some-nonsense' }]);
|
||||||
expect(blocks).to.be.empty;
|
expect(blocks).to.be.empty;
|
||||||
|
Reference in New Issue
Block a user