Fix custom fields label saving

[MAILPOET-3926]
This commit is contained in:
Pavel Dohnal
2021-11-10 12:27:39 +01:00
committed by Veljko V
parent ae99fe3be2
commit 6b41d4b70b
4 changed files with 21 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
import React, { useState, useEffect, useMemo } from 'react'; import React, { useState, useEffect, useMemo } from 'react';
import { import {
Button, Button,
SelectControl, SelectControl, TextControl,
ToggleControl, ToggleControl,
} from '@wordpress/components'; } from '@wordpress/components';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
@@ -10,6 +10,7 @@ import MailPoet from 'mailpoet';
import CustomFieldDelete from '../custom_field_delete.jsx'; import CustomFieldDelete from '../custom_field_delete.jsx';
const CustomFieldSettings = ({ const CustomFieldSettings = ({
label,
mandatory, mandatory,
validate, validate,
isSaving, isSaving,
@@ -19,16 +20,19 @@ const CustomFieldSettings = ({
onChange, onChange,
fieldType, fieldType,
}) => { }) => {
const [localLabel, setLocalLabel] = useState(label);
const [localMandatory, setLocalMandatory] = useState(mandatory); const [localMandatory, setLocalMandatory] = useState(mandatory);
const [localValidate, setLocalValidate] = useState(validate); const [localValidate, setLocalValidate] = useState(validate);
const localData = useMemo(() => ({ const localData = useMemo(() => ({
label: localLabel,
mandatory: localMandatory, mandatory: localMandatory,
validate: localValidate, validate: localValidate,
}), [localMandatory, localValidate]); }), [localLabel, localMandatory, localValidate]);
const hasUnsavedChanges = localMandatory !== mandatory const hasUnsavedChanges = localMandatory !== mandatory
|| localValidate !== validate; || localValidate !== validate
|| localLabel !== label;
useEffect(() => { useEffect(() => {
if (onChange) { if (onChange) {
@@ -38,6 +42,12 @@ const CustomFieldSettings = ({
return ( return (
<> <>
<TextControl
label={MailPoet.I18n.t('label')}
value={localLabel}
data-automation-id="settings_custom_text_label_input"
onChange={setLocalLabel}
/>
<ToggleControl <ToggleControl
label={MailPoet.I18n.t('blockMandatory')} label={MailPoet.I18n.t('blockMandatory')}
checked={localMandatory} checked={localMandatory}
@@ -89,6 +99,7 @@ const CustomFieldSettings = ({
}; };
CustomFieldSettings.propTypes = { CustomFieldSettings.propTypes = {
label: PropTypes.string,
mandatory: PropTypes.bool, mandatory: PropTypes.bool,
validate: PropTypes.string, validate: PropTypes.string,
onSave: PropTypes.func, onSave: PropTypes.func,
@@ -100,6 +111,7 @@ CustomFieldSettings.propTypes = {
}; };
CustomFieldSettings.defaultProps = { CustomFieldSettings.defaultProps = {
label: '',
mandatory: false, mandatory: false,
fieldType: '', fieldType: '',
isSaving: false, isSaving: false,

View File

@@ -2,7 +2,6 @@ import React from 'react';
import { import {
Panel, Panel,
PanelBody, PanelBody,
TextControl,
ToggleControl, ToggleControl,
} from '@wordpress/components'; } from '@wordpress/components';
import { InspectorControls } from '@wordpress/block-editor'; import { InspectorControls } from '@wordpress/block-editor';
@@ -38,6 +37,7 @@ const CustomTextEdit = ({ attributes, setAttributes, clientId }) => {
<CustomFieldSettings <CustomFieldSettings
updateAttributes={(attrs) => (setAttributes(attrs))} updateAttributes={(attrs) => (setAttributes(attrs))}
customFieldId={attributes.customFieldId} customFieldId={attributes.customFieldId}
label={attributes.label}
mandatory={attributes.mandatory} mandatory={attributes.mandatory}
validate={attributes.validate} validate={attributes.validate}
isSaving={isSaving} isSaving={isSaving}
@@ -50,6 +50,7 @@ const CustomTextEdit = ({ attributes, setAttributes, clientId }) => {
onFinish: () => setAttributes({ onFinish: () => setAttributes({
mandatory: params.mandatory, mandatory: params.mandatory,
validate: params.validate, validate: params.validate,
label: params.label,
}), }),
}); });
}} }}
@@ -64,12 +65,6 @@ const CustomTextEdit = ({ attributes, setAttributes, clientId }) => {
</Panel> </Panel>
<Panel> <Panel>
<PanelBody title={MailPoet.I18n.t('customFieldsFormSettings')} initialOpen> <PanelBody title={MailPoet.I18n.t('customFieldsFormSettings')} initialOpen>
<TextControl
label={MailPoet.I18n.t('label')}
value={attributes.label}
data-automation-id="settings_custom_text_label_input"
onChange={(label) => (setAttributes({ label }))}
/>
<ToggleControl <ToggleControl
label={MailPoet.I18n.t('displayLabelWithinInput')} label={MailPoet.I18n.t('displayLabelWithinInput')}
checked={attributes.labelWithinInput} checked={attributes.labelWithinInput}

View File

@@ -2,7 +2,6 @@ import React, { useRef, useState } from 'react';
import { import {
Panel, Panel,
PanelBody, SelectControl, PanelBody, SelectControl,
TextControl,
ToggleControl, ToggleControl,
} from '@wordpress/components'; } from '@wordpress/components';
import { InspectorControls } from '@wordpress/block-editor'; import { InspectorControls } from '@wordpress/block-editor';
@@ -49,6 +48,7 @@ const CustomTextAreaEdit = ({
<Panel> <Panel>
<PanelBody title={MailPoet.I18n.t('customFieldSettings')} initialOpen> <PanelBody title={MailPoet.I18n.t('customFieldSettings')} initialOpen>
<CustomFieldSettings <CustomFieldSettings
label={attributes.label}
mandatory={attributes.mandatory} mandatory={attributes.mandatory}
validate={attributes.validate} validate={attributes.validate}
isSaving={isSaving} isSaving={isSaving}
@@ -61,6 +61,7 @@ const CustomTextAreaEdit = ({
mandatory: params.mandatory, mandatory: params.mandatory,
validate: params.validate, validate: params.validate,
lines: params.lines, lines: params.lines,
label: params.label,
}), }),
})} })}
onCustomFieldDelete={() => deleteCustomField( onCustomFieldDelete={() => deleteCustomField(
@@ -74,12 +75,6 @@ const CustomTextAreaEdit = ({
</Panel> </Panel>
<Panel> <Panel>
<PanelBody title={MailPoet.I18n.t('formSettings')} initialOpen> <PanelBody title={MailPoet.I18n.t('formSettings')} initialOpen>
<TextControl
label={MailPoet.I18n.t('label')}
value={attributes.label}
data-automation-id="settings_custom_text_label_input"
onChange={(label) => (setAttributes({ label }))}
/>
<ToggleControl <ToggleControl
label={MailPoet.I18n.t('displayLabelWithinInput')} label={MailPoet.I18n.t('displayLabelWithinInput')}
checked={attributes.labelWithinInput} checked={attributes.labelWithinInput}

View File

@@ -33,12 +33,14 @@ function mapFormDataToParams(fieldType, data) {
return { return {
required: data.mandatory ? '1' : '', required: data.mandatory ? '1' : '',
validate: data.validate, validate: data.validate,
label: data.label,
}; };
case 'textarea': case 'textarea':
return { return {
required: data.mandatory ? '1' : '', required: data.mandatory ? '1' : '',
validate: data.validate, validate: data.validate,
lines: data.lines ? data.lines : '1', lines: data.lines ? data.lines : '1',
label: data.label,
}; };
default: default:
throw new Error(`Invalid custom field type ${fieldType}!`); throw new Error(`Invalid custom field type ${fieldType}!`);