// eslint-disable-next-line @wordpress/no-unsafe-wp-apis import { __experimentalInputControl as Input } from "@wordpress/components"; import "./editor.scss"; import { useDeviceType } from "../../controls/controls"; import Responsive from "../responsive/responsive"; import { useState } from "@wordpress/element"; import { priceLink } from "../../blocks/shared/helpFn"; const InputControl = ({ attributes, attributesKey, setAttributes, label, ajax = false, flex = true, inputType = "number", placeholder, onChange = false, help = false, min = 1, step = 1, max = 100000, className = "", pro = false, }) => { // Check device (desktop/tablet/mobile). const deviceType = useDeviceType(); const value = attributes?.device ? attributes?.device?.[deviceType] : attributes; const [currentValue, setCurrentValue] = useState(value); const [ajaxLod, setAjaxLoad] = useState(false); const setInputValue = (newValue) => { if (attributes?.device) { setAttributes({ [attributesKey]: { ...attributes, device: { ...attributes.device, [deviceType]: newValue }, }, }); } else { setAttributes({ [attributesKey]: newValue }); } }; const setClearTimeOut = setTimeout(() => { if (ajax && ajaxLod) { setInputValue(currentValue); setAjaxLoad(false); } }, 500); // Set value function. const setValue = (newValue) => { newValue = newValue > max ? max : newValue; if (ajax) { clearTimeout(setClearTimeOut); setCurrentValue(newValue); setAjaxLoad(true); } else { setInputValue(newValue); } }; return ( <>
{label} {pro && (Pro)} {attributes?.device && }
(onChange ? onChange(val) : setValue(val))} placeholder={placeholder} // Use placeholder prop here help={help} step={step} min={min} max={max} __next40pxDefaultSize />
); }; export default InputControl;