Better select for date formats
[MAILPOET-2453]
This commit is contained in:
committed by
Rostislav Wolný
parent
c35e6451b5
commit
12af14fd90
@@ -2,7 +2,7 @@ import React, { useState } from 'react';
|
|||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
ToggleControl,
|
ToggleControl,
|
||||||
TextControl,
|
SelectControl,
|
||||||
} from '@wordpress/components';
|
} from '@wordpress/components';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import MailPoet from 'mailpoet';
|
import MailPoet from 'mailpoet';
|
||||||
@@ -12,6 +12,7 @@ const CustomFieldSettings = ({
|
|||||||
dateType,
|
dateType,
|
||||||
dateFormat,
|
dateFormat,
|
||||||
defaultToday,
|
defaultToday,
|
||||||
|
dateSettings,
|
||||||
isSaving,
|
isSaving,
|
||||||
onSave,
|
onSave,
|
||||||
}) => {
|
}) => {
|
||||||
@@ -20,6 +21,24 @@ const CustomFieldSettings = ({
|
|||||||
const [localDateType, setLocalLocalDateType] = useState(dateType);
|
const [localDateType, setLocalLocalDateType] = useState(dateType);
|
||||||
const [localDateFormat, setLocalLocalDateFormat] = useState(dateFormat);
|
const [localDateFormat, setLocalLocalDateFormat] = useState(dateFormat);
|
||||||
|
|
||||||
|
const createDateFormatsSelect = () => {
|
||||||
|
const dateFormats = dateSettings.dateFormats[localDateType];
|
||||||
|
if (Array.isArray(dateFormats) && dateFormats.length === 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<SelectControl
|
||||||
|
label={MailPoet.I18n.t('customFieldDateFormat')}
|
||||||
|
value={localDateFormat}
|
||||||
|
onChange={(value) => setLocalLocalDateFormat(value)}
|
||||||
|
options={dateFormats.map((format) => ({
|
||||||
|
value: format,
|
||||||
|
label: format,
|
||||||
|
}))}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="custom-field-settings">
|
<div className="custom-field-settings">
|
||||||
<Button
|
<Button
|
||||||
@@ -47,16 +66,19 @@ const CustomFieldSettings = ({
|
|||||||
checked={localDefaultToday}
|
checked={localDefaultToday}
|
||||||
onChange={setLocalLocalDefaultToday}
|
onChange={setLocalLocalDefaultToday}
|
||||||
/>
|
/>
|
||||||
<TextControl
|
<SelectControl
|
||||||
label={MailPoet.I18n.t('customFieldDateType')}
|
label={MailPoet.I18n.t('customFieldDateType')}
|
||||||
value={localDateType}
|
value={localDateType}
|
||||||
onChange={setLocalLocalDateType}
|
onChange={(value) => {
|
||||||
/>
|
setLocalLocalDateType(value);
|
||||||
<TextControl
|
const dateFormats = dateSettings.dateFormats[value];
|
||||||
label={MailPoet.I18n.t('customFieldDateFormat')}
|
if (dateFormats.length === 1) {
|
||||||
value={localDateFormat}
|
setLocalLocalDateFormat(dateFormats[0]);
|
||||||
onChange={setLocalLocalDateFormat}
|
}
|
||||||
|
}}
|
||||||
|
options={dateSettings.dateTypes}
|
||||||
/>
|
/>
|
||||||
|
{createDateFormatsSelect()}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -68,6 +90,14 @@ CustomFieldSettings.propTypes = {
|
|||||||
defaultToday: PropTypes.bool,
|
defaultToday: PropTypes.bool,
|
||||||
onSave: PropTypes.func.isRequired,
|
onSave: PropTypes.func.isRequired,
|
||||||
isSaving: PropTypes.bool,
|
isSaving: PropTypes.bool,
|
||||||
|
dateSettings: PropTypes.shape({
|
||||||
|
dateTypes: PropTypes.arrayOf(PropTypes.shape({
|
||||||
|
label: PropTypes.string,
|
||||||
|
value: PropTypes.string,
|
||||||
|
})),
|
||||||
|
dateFormats: PropTypes.objectOf(PropTypes.arrayOf(PropTypes.string)),
|
||||||
|
months: PropTypes.arrayOf(PropTypes.string),
|
||||||
|
}).isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
CustomFieldSettings.defaultProps = {
|
CustomFieldSettings.defaultProps = {
|
||||||
|
@@ -16,6 +16,11 @@ const CustomDateEdit = ({ attributes, setAttributes }) => {
|
|||||||
(sel) => sel('mailpoet-form-editor').getIsCustomFieldSaving(),
|
(sel) => sel('mailpoet-form-editor').getIsCustomFieldSaving(),
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
const dateSettings = useSelect(
|
||||||
|
(sel) => sel('mailpoet-form-editor').getDateSettingsData(),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
const { saveCustomField } = useDispatch('mailpoet-form-editor');
|
const { saveCustomField } = useDispatch('mailpoet-form-editor');
|
||||||
const inspectorControls = (
|
const inspectorControls = (
|
||||||
<InspectorControls>
|
<InspectorControls>
|
||||||
@@ -23,6 +28,7 @@ const CustomDateEdit = ({ attributes, setAttributes }) => {
|
|||||||
<PanelBody title={MailPoet.I18n.t('customFieldSettings')} initialOpen>
|
<PanelBody title={MailPoet.I18n.t('customFieldSettings')} initialOpen>
|
||||||
<CustomFieldSettings
|
<CustomFieldSettings
|
||||||
mandatory={attributes.mandatory}
|
mandatory={attributes.mandatory}
|
||||||
|
dateSettings={dateSettings}
|
||||||
defaultToday={attributes.defaultToday}
|
defaultToday={attributes.defaultToday}
|
||||||
dateFormat={attributes.dateFormat}
|
dateFormat={attributes.dateFormat}
|
||||||
dateType={attributes.dateType}
|
dateType={attributes.dateType}
|
||||||
|
Reference in New Issue
Block a user