| 1 |
import { memo } from "@wordpress/element"; |
| 2 |
import { Button } from "@wordpress/components"; |
| 3 |
import { ResetIcon } from "../../icons/icons"; |
| 4 |
import { useDeviceType } from "../../controls/controls"; |
| 5 |
|
| 6 |
export const Units = memo(({ attributes, setAttributes, attributesKey, units, onUnitChange }) => { |
| 7 |
const deviceType = useDeviceType(); |
| 8 |
// Set unit function. |
| 9 |
const setUnit = (newValue) => { |
| 10 |
if (onUnitChange) { |
| 11 |
onUnitChange(newValue); |
| 12 |
return; |
| 13 |
} |
| 14 |
if (attributes.unit?.[deviceType]) { |
| 15 |
setAttributes({ |
| 16 |
[attributesKey]: { |
| 17 |
...attributes, |
| 18 |
unit: { |
| 19 |
...attributes.unit, |
| 20 |
[deviceType]: newValue.toLowerCase(), |
| 21 |
}, |
| 22 |
}, |
| 23 |
}); |
| 24 |
} else { |
| 25 |
setAttributes({ |
| 26 |
[attributesKey]: { |
| 27 |
...attributes, |
| 28 |
unit: newValue.toLowerCase(), |
| 29 |
}, |
| 30 |
}); |
| 31 |
} |
| 32 |
}; |
| 33 |
|
| 34 |
const unit = "object" === typeof attributes?.unit ? attributes.unit?.[deviceType] : attributes?.unit; |
| 35 |
|
| 36 |
return ( |
| 37 |
<div className="sp-smart-post-units"> |
| 38 |
<span className="sp-smart-post-units-indicator-label">{unit}</span> |
| 39 |
<div className="sp-smart-post-units-btn"> |
| 40 |
{units?.map((item, i) => ( |
| 41 |
<Button |
| 42 |
className={unit === item.toLowerCase() ? "active" : ""} |
| 43 |
key={i} |
| 44 |
value={item} |
| 45 |
onClick={(e) => setUnit(e.target.value)} |
| 46 |
> |
| 47 |
{" "} |
| 48 |
{item}{" "} |
| 49 |
</Button> |
| 50 |
))} |
| 51 |
</div> |
| 52 |
</div> |
| 53 |
); |
| 54 |
}); |
| 55 |
|
| 56 |
export const ResetButton = memo(({ onClick }) => { |
| 57 |
return ( |
| 58 |
<Button className="sp-smart-post-header-control-reset" onClick={onClick}> |
| 59 |
<ResetIcon /> |
| 60 |
</Button> |
| 61 |
); |
| 62 |
}); |
| 63 |
|
| 64 |
export const SpLinkedButton = ({ attributes, attributesKey, setAttributes, Icon, updateAllChange }) => { |
| 65 |
const linked = attributes?.allChange || false; |
| 66 |
return ( |
| 67 |
<div |
| 68 |
className={`sp-link-btn${linked ? "" : " active"}`} |
| 69 |
onClick={() => |
| 70 |
updateAllChange |
| 71 |
? updateAllChange(!linked) |
| 72 |
: setAttributes({ |
| 73 |
[attributesKey]: { ...attributes, allChange: !linked }, |
| 74 |
}) |
| 75 |
} |
| 76 |
> |
| 77 |
<span className="sp-link-side-icon">{Icon}</span> |
| 78 |
</div> |
| 79 |
); |
| 80 |
}; |
| 81 |
|