| 1 |
import { Button } from "@wordpress/components"; |
| 2 |
import Responsive from "../responsive/responsive"; |
| 3 |
import { useDeviceType } from "../../controls/controls"; |
| 4 |
import { ResetIcon } from "../../icons/icons"; |
| 5 |
|
| 6 |
export const BoxSpacing = ({ |
| 7 |
label, |
| 8 |
customClass, |
| 9 |
attributes, |
| 10 |
attributesKey, |
| 11 |
setAttributes, |
| 12 |
boxUnits, |
| 13 |
units, |
| 14 |
labelItem, |
| 15 |
handleReset, |
| 16 |
onChange = false, |
| 17 |
}) => { |
| 18 |
const deviceType = useDeviceType(); |
| 19 |
const haveDevice = typeof attributes?.device !== "undefined" ? true : false; |
| 20 |
const attrValue = haveDevice ? attributes?.device?.[deviceType] : attributes?.value; |
| 21 |
|
| 22 |
const setSpacingData = (newValue, side) => { |
| 23 |
const updateValue = haveDevice |
| 24 |
? { |
| 25 |
...attributes, |
| 26 |
device: { |
| 27 |
...attributes.device, |
| 28 |
[deviceType]: { |
| 29 |
...attributes.device?.[deviceType], |
| 30 |
[side]: parseInt(newValue), |
| 31 |
}, |
| 32 |
}, |
| 33 |
} |
| 34 |
: { |
| 35 |
...attributes, |
| 36 |
value: { |
| 37 |
...attributes["value"], |
| 38 |
[side]: parseInt(newValue), |
| 39 |
}, |
| 40 |
}; |
| 41 |
if (onChange) { |
| 42 |
onChange(attributesKey, updateValue); |
| 43 |
} else { |
| 44 |
setAttributes({ |
| 45 |
[attributesKey]: updateValue, |
| 46 |
}); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
const setUnit = (value) => { |
| 51 |
const updateUnit = haveDevice ? { ...attributes.unit, [deviceType]: value } : value; |
| 52 |
|
| 53 |
if (onChange) { |
| 54 |
onChange(attributesKey, { ...attributes, unit: updateUnit }); |
| 55 |
} else { |
| 56 |
setAttributes({ |
| 57 |
[attributesKey]: { |
| 58 |
...attributes, |
| 59 |
unit: updateUnit, |
| 60 |
}, |
| 61 |
}); |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
return ( |
| 66 |
<div className={`sp-smart-post-spacing${customClass ? " " + customClass : ""} sp-smart-post-component-mb`}> |
| 67 |
<div className="sp-smart-post-spacing-part-1"> |
| 68 |
<div className="sp-smart-post-header-control"> |
| 69 |
<div className="sp-smart-post-header-control-left"> |
| 70 |
<span className="sp-smart-post-component-title">{label}</span> |
| 71 |
{attributes?.device && <Responsive />} |
| 72 |
</div> |
| 73 |
<div className="sp-smart-post-header-control-right"> |
| 74 |
<Button onClick={() => handleReset()} className={`sp-smart-post-header-control-reset`}> |
| 75 |
<ResetIcon /> |
| 76 |
</Button> |
| 77 |
<div className={`sp-smart-post-units ${boxUnits ? "box" : ""}`}> |
| 78 |
<span className={boxUnits ? "box-unit" : ""}> |
| 79 |
{"object" !== typeof attributes?.unit ? attributes?.unit : attributes.unit?.[deviceType]} |
| 80 |
</span> |
| 81 |
<div className="sp-smart-post-units-btn"> |
| 82 |
{units?.map((item, i) => ( |
| 83 |
<Button |
| 84 |
className={attributes?.unit?.[deviceType] === item.toLowerCase() ? "active" : ""} |
| 85 |
key={i} |
| 86 |
value={item} |
| 87 |
onClick={(e) => setUnit(e.target.value)} |
| 88 |
> |
| 89 |
{" "} |
| 90 |
{item}{" "} |
| 91 |
</Button> |
| 92 |
))} |
| 93 |
</div> |
| 94 |
</div> |
| 95 |
</div> |
| 96 |
</div> |
| 97 |
</div> |
| 98 |
<div className="sp-smart-post-spacing-part-2"> |
| 99 |
{["top", "right", "bottom", "left"].map((side, i) => ( |
| 100 |
<div key={i} className={`sp-smart-post-spacing-${side}`}> |
| 101 |
<span className="sp-smart-post-spacing-wrapper"> |
| 102 |
<input |
| 103 |
id={`sp-smart-post-spacing-${side}-${i}`} |
| 104 |
onChange={(e) => setSpacingData(e.target.value, [side])} |
| 105 |
type="number" |
| 106 |
value={attrValue?.[side]} |
| 107 |
/> |
| 108 |
</span> |
| 109 |
<label htmlFor={`sp-smart-post-spacing-${side}-${i}`}>{labelItem?.[side]}</label> |
| 110 |
</div> |
| 111 |
))} |
| 112 |
</div> |
| 113 |
</div> |
| 114 |
); |
| 115 |
}; |
| 116 |
|