| 1 |
/* eslint-disable @wordpress/no-unsafe-wp-apis */ |
| 2 |
import { |
| 3 |
__experimentalToggleGroupControl as ToggleGroupControl, |
| 4 |
__experimentalToggleGroupControlOption as ToggleGroupControlOption, |
| 5 |
} from "@wordpress/components"; |
| 6 |
import { useDeviceType } from "../../controls/controls"; |
| 7 |
// import Responsive from '../responsive/responsive'; |
| 8 |
import "./editor.scss"; |
| 9 |
import { memo } from "@wordpress/element"; |
| 10 |
import Responsive from "../responsive/responsive"; |
| 11 |
import { priceLink } from "../../blocks/shared/helpFn"; |
| 12 |
|
| 13 |
const SPToggleGroupControl = ({ |
| 14 |
attributes, |
| 15 |
attributesKey, |
| 16 |
setAttributes, |
| 17 |
label = "", |
| 18 |
items, |
| 19 |
border = false, |
| 20 |
flexStyle = false, |
| 21 |
onClick = false, |
| 22 |
hasDivider = true, |
| 23 |
extraClass = "", |
| 24 |
pro = false, |
| 25 |
}) => { |
| 26 |
// Device type |
| 27 |
const deviceType = useDeviceType(); |
| 28 |
|
| 29 |
// Update button group value |
| 30 |
const setButtonGroup = (newValue) => { |
| 31 |
if (attributes?.device) { |
| 32 |
setAttributes({ |
| 33 |
[attributesKey]: { |
| 34 |
...attributes, |
| 35 |
device: { |
| 36 |
...attributes.device, |
| 37 |
[deviceType]: newValue, |
| 38 |
}, |
| 39 |
}, |
| 40 |
}); |
| 41 |
} else { |
| 42 |
setAttributes({ [attributesKey]: newValue }); |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
// Get the active value |
| 47 |
const activeValue = attributes?.device ? attributes.device?.[deviceType] : attributes; |
| 48 |
|
| 49 |
// Handle button click |
| 50 |
const handleClick = (value) => { |
| 51 |
if (onClick) { |
| 52 |
onClick(value); |
| 53 |
} else { |
| 54 |
setButtonGroup(value); |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
return ( |
| 59 |
<div className={`sp-smart-post-toggle-button-group-wrapper sp-smart-post-component-mb${pro ? " sp-is-pro" : ""}`}> |
| 60 |
<div className="sp-smart-post-header"> |
| 61 |
<span |
| 62 |
// onClick={ ( e ) => activeLabel( e ) } |
| 63 |
className="sp-smart-post-component-title" |
| 64 |
> |
| 65 |
{label} |
| 66 |
{pro && <a target="_blank" href={priceLink} className="sp-smart-pro-text"> (Pro) </a>} |
| 67 |
</span> |
| 68 |
{attributes?.device && <Responsive />} |
| 69 |
</div> |
| 70 |
<ToggleGroupControl |
| 71 |
className={`sp-smart-post-toggle-button-group sp-smart-post-component-mb${ |
| 72 |
label ? "" : " sp-negative-space" |
| 73 |
} ${flexStyle ? "sp-smart-post-d-flex button-style-2" : ""}`} |
| 74 |
// label={ hasDevice ? '' : label } |
| 75 |
// aria-label={ |
| 76 |
// label |
| 77 |
// ? label |
| 78 |
// : __( |
| 79 |
// 'Normal and Hover Button Control', |
| 80 |
// 'smart-post-show' |
| 81 |
// ) |
| 82 |
// } |
| 83 |
value={activeValue} |
| 84 |
isBlock |
| 85 |
__nextHasNoMarginBottom |
| 86 |
__next40pxDefaultSize |
| 87 |
onChange={(value) => handleClick(value)} |
| 88 |
> |
| 89 |
<div |
| 90 |
className={`sp-smart-post-toggle-button-group-list ${border ? "has-border" : ""}${ |
| 91 |
hasDivider ? " sp-has-divider" : "" |
| 92 |
} ${extraClass}`} |
| 93 |
> |
| 94 |
{items?.map((item, i) => ( |
| 95 |
<ToggleGroupControlOption |
| 96 |
key={i} |
| 97 |
value={item.value} |
| 98 |
label={item.label} |
| 99 |
showTooltip={item.tooltip ? true : false} |
| 100 |
aria-label={item.tooltip} |
| 101 |
disabled={item.disabled || ""} |
| 102 |
className={activeValue === item.value ? "active" : ""} |
| 103 |
/> |
| 104 |
))} |
| 105 |
</div> |
| 106 |
</ToggleGroupControl> |
| 107 |
</div> |
| 108 |
); |
| 109 |
}; |
| 110 |
|
| 111 |
export default memo(SPToggleGroupControl); |
| 112 |
|