/** * 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')} setIsFromDatePickerVisible( (state) => !state ) } variant="secondary" > {fromDate || __('Select From Date', 'top-10')} {fromDate && ( clearDate(true)} variant="link" isDestructive > {__('Clear', 'top-10')} )} {isFromDatePickerVisible && ( setIsFromDatePickerVisible(false) } > handleDateChange(date, true) } /> )} {__('To', 'top-10')} setIsToDatePickerVisible( (state) => !state ) } variant="secondary" > {toDate || __('Select To Date', 'top-10')} {toDate && ( clearDate(false)} variant="link" isDestructive > {__('Clear', 'top-10')} )} {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 }) } /> setIsSvgModalOpen(true)} variant="secondary" > {__('Select SVG', 'top-10')} {__('Clear', 'top-10')} {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) => ( onSelect(svg.code)} style={{ padding: '1px', border: '0px solid #ddd', display: 'flex', justifyContent: 'center', alignItems: 'center', height: '25px', }} title={svg.name} > ))} ) : null; };