/** * WordPress dependencies */ import { BlockControls, AlignmentControl, InspectorControls, } from '@wordpress/block-editor'; import { PanelBody, SelectControl, Button, Modal, TextControl, ToggleControl, Popover, DatePicker, TextareaControl, __experimentalText as Text, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { useState } from '@wordpress/element'; import { svgOptions } from './components/svg'; import PaddingControl from './components/padding-control'; import UnitControl from './components/unit-control'; const Controls = ({ attributes, setAttributes }) => { const { textAlign, counter, fromDate, toDate, textBefore, textAfter, advancedMode, textAdvanced, numberFormat, svgCode, svgIconLocation, svgIconSize, svgIconSizeUnit, } = attributes; const [isFromDatePickerVisible, setIsFromDatePickerVisible] = useState(false); const [isToDatePickerVisible, setIsToDatePickerVisible] = useState(false); const [isSvgModalOpen, setIsSvgModalOpen] = useState(false); const formatDate = (date) => { if (!date) { return ''; } const d = new Date(date); return d.toISOString().split('T')[0]; }; const handleDateChange = (date, isFromDate) => { const formattedDate = formatDate(date); setAttributes({ [isFromDate ? 'fromDate' : 'toDate']: formattedDate, }); setIsFromDatePickerVisible(false); setIsToDatePickerVisible(false); }; const clearDate = (isFromDate) => { setAttributes({ [isFromDate ? 'fromDate' : 'toDate']: '' }); }; const handleSvgSelection = (selectedSvg) => { setAttributes({ svgCode: selectedSvg }); setIsSvgModalOpen(false); }; const clearSvg = () => { setAttributes({ svgCode: '' }); }; return ( <> { setAttributes({ textAlign: nextAlign }); }} /> setAttributes({ counter: value })} /> {counter === 'daily' && ( <>
{__('From', 'top-10')} {fromDate && ( )} {isFromDatePickerVisible && ( setIsFromDatePickerVisible(false) } > handleDateChange(date, true) } /> )}
{__('To', 'top-10')} {toDate && ( )} {isToDatePickerVisible && ( setIsToDatePickerVisible(false) } > handleDateChange(date, false) } /> )}
)} setAttributes({ numberFormat: value }) } />
setAttributes({ advancedMode: value }) } /> {!advancedMode && ( <> setAttributes({ textBefore: value }) } /> setAttributes({ textAfter: value }) } /> )} {advancedMode && ( setAttributes({ textAdvanced: value }) } help={__( 'Use %totalcount% or %dailycount% as placeholders for the count value.', 'top-10' )} /> )}
setAttributes({ svgCode: value }) } />
{svgCode && (
)}
setIsSvgModalOpen(false)} onSelect={handleSvgSelection} /> {svgCode && ( <> setAttributes({ svgIconLocation: newValue }) } help={ svgIconLocation === 'after' ? __( 'Icon will be placed after the text.', 'top-10' ) : __( 'Icon will be placed before the text.', 'top-10' ) } /> setAttributes({ svgIconSize: value }) } onUnitChange={(unit) => setAttributes({ svgIconSizeUnit: unit }) } /> )} ); }; export default Controls; const SvgSelectionModal = ({ isOpen, onClose, onSelect }) => { return isOpen ? (
{svgOptions.map((svg, index) => ( ))}
) : null; };