| 1 |
import { SelectControl } from "@wordpress/components"; |
| 2 |
import "./editor.scss"; |
| 3 |
import { memo } from "@wordpress/element"; |
| 4 |
import { useDeviceType } from "../../controls/controls"; |
| 5 |
import Responsive from "../responsive/responsive"; |
| 6 |
|
| 7 |
const SelectField = ({ |
| 8 |
attributes, |
| 9 |
attributesKey, |
| 10 |
setAttributes, |
| 11 |
label, |
| 12 |
items, |
| 13 |
flexStyle = false, |
| 14 |
onChange = false, |
| 15 |
value = false, |
| 16 |
defaultOption = false, |
| 17 |
extraClassName = "", |
| 18 |
pro = false, |
| 19 |
}) => { |
| 20 |
// Device check fn |
| 21 |
const deviceType = useDeviceType(); |
| 22 |
// Set Button value |
| 23 |
const setNewValue = (newValue) => { |
| 24 |
if (attributes?.device) { |
| 25 |
setAttributes({ |
| 26 |
[attributesKey]: { |
| 27 |
device: { ...attributes?.device, [deviceType]: newValue }, |
| 28 |
}, |
| 29 |
}); |
| 30 |
} else { |
| 31 |
setAttributes({ [attributesKey]: newValue }); |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
// Get active button value |
| 36 |
const activeValue = attributes?.device ? attributes?.device?.[deviceType] : attributes; |
| 37 |
|
| 38 |
let selectItems = []; |
| 39 |
if (defaultOption) { |
| 40 |
selectItems = [{ label: "Default", value: "" }, ...items]; |
| 41 |
} else { |
| 42 |
selectItems = items; |
| 43 |
} |
| 44 |
|
| 45 |
return ( |
| 46 |
<div |
| 47 |
className={`sp-smart-post-select-field sp-smart-post-component-mb ${ |
| 48 |
flexStyle ? "sp-smart-post-d-flex" : "sp-smart-post-d-block" |
| 49 |
}${extraClassName}${pro ? " sp-is-pro" : ""}`} |
| 50 |
> |
| 51 |
<div className="sp-smart-post-header"> |
| 52 |
<span |
| 53 |
className={ |
| 54 |
attributes?.device ? "sp-smart-post-select-component-title" : "sp-smart-post-component-title" |
| 55 |
} |
| 56 |
> |
| 57 |
{" "} |
| 58 |
{label} |
| 59 |
{pro && <a target="_blank" href="https://wpsmartpost.com/pricing/?ref=1" className="sp-smart-pro-text"> (Pro) </a>} |
| 60 |
</span> |
| 61 |
{attributes?.device && <Responsive />} |
| 62 |
</div> |
| 63 |
<SelectControl |
| 64 |
className="custom-select-control" |
| 65 |
value={value ? value : activeValue} |
| 66 |
options={selectItems} |
| 67 |
onChange={(newField) => (onChange ? onChange(newField) : setNewValue(newField))} |
| 68 |
__nextHasNoMarginBottom |
| 69 |
__next40pxDefaultSize |
| 70 |
/> |
| 71 |
</div> |
| 72 |
); |
| 73 |
}; |
| 74 |
|
| 75 |
export default memo(SelectField); |
| 76 |
|