| 1 |
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis |
| 2 |
import { __experimentalInputControl as Input } from "@wordpress/components"; |
| 3 |
import "./editor.scss"; |
| 4 |
import { useDeviceType } from "../../controls/controls"; |
| 5 |
import Responsive from "../responsive/responsive"; |
| 6 |
import { useState } from "@wordpress/element"; |
| 7 |
import { priceLink } from "../../blocks/shared/helpFn"; |
| 8 |
|
| 9 |
const InputControl = ({ |
| 10 |
attributes, |
| 11 |
attributesKey, |
| 12 |
setAttributes, |
| 13 |
label, |
| 14 |
ajax = false, |
| 15 |
flex = true, |
| 16 |
inputType = "number", |
| 17 |
placeholder, |
| 18 |
onChange = false, |
| 19 |
help = false, |
| 20 |
min = 1, |
| 21 |
step = 1, |
| 22 |
max = 100000, |
| 23 |
className = "", |
| 24 |
pro = false, |
| 25 |
}) => { |
| 26 |
// Check device (desktop/tablet/mobile). |
| 27 |
const deviceType = useDeviceType(); |
| 28 |
|
| 29 |
const value = attributes?.device ? attributes?.device?.[deviceType] : attributes; |
| 30 |
|
| 31 |
const [currentValue, setCurrentValue] = useState(value); |
| 32 |
const [ajaxLod, setAjaxLoad] = useState(false); |
| 33 |
|
| 34 |
const setInputValue = (newValue) => { |
| 35 |
if (attributes?.device) { |
| 36 |
setAttributes({ |
| 37 |
[attributesKey]: { |
| 38 |
...attributes, |
| 39 |
device: { ...attributes.device, [deviceType]: newValue }, |
| 40 |
}, |
| 41 |
}); |
| 42 |
} else { |
| 43 |
setAttributes({ [attributesKey]: newValue }); |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
const setClearTimeOut = setTimeout(() => { |
| 48 |
if (ajax && ajaxLod) { |
| 49 |
setInputValue(currentValue); |
| 50 |
setAjaxLoad(false); |
| 51 |
} |
| 52 |
}, 500); |
| 53 |
|
| 54 |
// Set value function. |
| 55 |
const setValue = (newValue) => { |
| 56 |
newValue = newValue > max ? max : newValue; |
| 57 |
|
| 58 |
if (ajax) { |
| 59 |
clearTimeout(setClearTimeOut); |
| 60 |
setCurrentValue(newValue); |
| 61 |
setAjaxLoad(true); |
| 62 |
} else { |
| 63 |
setInputValue(newValue); |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
return ( |
| 68 |
<> |
| 69 |
<div className={`sp-smart-post-input-control sp-smart-post-component-mb ${className}${pro ? " sp-is-pro" : ""}`}> |
| 70 |
<div className="sp-smart-post-spacing-part-1"> |
| 71 |
<div className={`sp-smart-post-header-input-control ${flex ? "d-flex" : "d-block"}`}> |
| 72 |
<div className="sp-smart-post-header-control-left"> |
| 73 |
<span className="sp-smart-post-component-title"> |
| 74 |
{label} |
| 75 |
{pro && <a target="_blank" href={priceLink} className="sp-smart-pro-text">(Pro)</a>} |
| 76 |
</span> |
| 77 |
{attributes?.device && <Responsive />} |
| 78 |
</div> |
| 79 |
<div className="sp-smart-post-header-control-right"> |
| 80 |
<Input |
| 81 |
type={inputType} |
| 82 |
value={ajax ? currentValue : value} |
| 83 |
onChange={(val) => (onChange ? onChange(val) : setValue(val))} |
| 84 |
placeholder={placeholder} // Use placeholder prop here |
| 85 |
help={help} |
| 86 |
step={step} |
| 87 |
min={min} |
| 88 |
max={max} |
| 89 |
__next40pxDefaultSize |
| 90 |
/> |
| 91 |
</div> |
| 92 |
</div> |
| 93 |
</div> |
| 94 |
</div> |
| 95 |
</> |
| 96 |
); |
| 97 |
}; |
| 98 |
|
| 99 |
export default InputControl; |
| 100 |
|