| 1 |
import { ButtonGroup, Button } from "@wordpress/components"; |
| 2 |
import "./editor.scss"; |
| 3 |
import { useDeviceType } from "../../controls/controls"; |
| 4 |
import Responsive from "../responsive/responsive"; |
| 5 |
|
| 6 |
const SpButtonGroup = ({ |
| 7 |
attributes, |
| 8 |
attributesKey, |
| 9 |
setAttributes, |
| 10 |
label, |
| 11 |
items, |
| 12 |
border = false, |
| 13 |
flexStyle = false, |
| 14 |
onClick = false, |
| 15 |
}) => { |
| 16 |
// Device type |
| 17 |
const deviceType = useDeviceType(); |
| 18 |
|
| 19 |
// Update button group value |
| 20 |
const setButtonGroup = (newValue) => { |
| 21 |
if (attributes?.device) { |
| 22 |
setAttributes({ |
| 23 |
[attributesKey]: { |
| 24 |
...attributes.device, |
| 25 |
[deviceType]: newValue, |
| 26 |
}, |
| 27 |
}); |
| 28 |
} else { |
| 29 |
setAttributes({ [attributesKey]: newValue }); |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
// Get the active value |
| 34 |
const activeValue = attributes?.device ? attributes.device?.[deviceType] : attributes; |
| 35 |
|
| 36 |
// Handle button click |
| 37 |
const handleClick = (value) => { |
| 38 |
if (onClick) { |
| 39 |
onClick(value); |
| 40 |
} else { |
| 41 |
setButtonGroup(value); |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
return ( |
| 46 |
<ButtonGroup |
| 47 |
className={`sp-smart-post-button-group sp-smart-post-component-mb ${ |
| 48 |
flexStyle ? "sp-smart-post-d-flex button-style-2" : "" |
| 49 |
}`} |
| 50 |
> |
| 51 |
{label && ( |
| 52 |
<div className="sp-smart-post-component-top sp-smart-post-component-title"> |
| 53 |
<span>{label}</span> |
| 54 |
{attributes?.device && <Responsive />} |
| 55 |
</div> |
| 56 |
)} |
| 57 |
<div className={`sp-smart-post-button-group-list ${border ? "has-border" : ""}`}> |
| 58 |
{items?.map((item, i) => ( |
| 59 |
<Button |
| 60 |
className={activeValue === item.value ? "active" : ""} |
| 61 |
key={i} |
| 62 |
value={item.value} |
| 63 |
onClick={() => handleClick(item.value)} |
| 64 |
> |
| 65 |
<span>{item.label}</span> |
| 66 |
{item.tooltip && <p>{item.tooltip}</p>} |
| 67 |
</Button> |
| 68 |
))} |
| 69 |
</div> |
| 70 |
</ButtonGroup> |
| 71 |
); |
| 72 |
}; |
| 73 |
|
| 74 |
export default SpButtonGroup; |
| 75 |
|