import { useState, useEffect } from '@wordpress/element'; import { Popover, Button, } from '@wordpress/components'; type Props = { units: { label: string, value: string }[], value: string, device: device, onChange: handleUnitChange }; /** * Unit select or text. */ export default function UnitField({ units, value, device, onChange, }:Props) { // console.log(units); const [isVisible, setIsVisible] = useState(false); const toggleVisible = () => { setIsVisible((state) => !state); }; const handleUnitButtonClick = (unitValue: string, device: device) => { if (value !== unitValue) { onChange(unitValue, device); } setIsVisible(false); } const currentUnit = units.find(unit => unit.value === value); return (
{units.length > 1 ? (
{isVisible && setIsVisible(false)} >
{units.map((unit => (
)))}
}
) : {currentUnit?.label || value || '-'} }
); }