| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import React from "react"; |
| 5 |
import { useBlockEditContext } from "@wordpress/block-editor"; |
| 6 |
import { useSelect, useDispatch } from "@wordpress/data"; |
| 7 |
import { |
| 8 |
SelectControl, |
| 9 |
__experimentalToolsPanelItem as ToolsPanelItem, |
| 10 |
} from "@wordpress/components"; |
| 11 |
|
| 12 |
/** |
| 13 |
* Internal dependencies |
| 14 |
*/ |
| 15 |
import { SelectControlWithToolsPanelProps } from "./types"; |
| 16 |
|
| 17 |
/** |
| 18 |
* Select control component with ToolsPanel integration |
| 19 |
* Self-managed via useBlockEditContext, wrapped in ToolsPanelItem |
| 20 |
*/ |
| 21 |
const SelectControlWithToolsPanel: React.FC< |
| 22 |
SelectControlWithToolsPanelProps |
| 23 |
> = ({ |
| 24 |
label, |
| 25 |
attrKey, |
| 26 |
options, |
| 27 |
defaultValue = "", |
| 28 |
help, |
| 29 |
isShownByDefault = true, |
| 30 |
}) => { |
| 31 |
const { clientId } = useBlockEditContext(); |
| 32 |
|
| 33 |
const attributes = useSelect((select) => |
| 34 |
select("core/block-editor").getBlockAttributes(clientId), |
| 35 |
) as Record<string, any>; |
| 36 |
|
| 37 |
const { updateBlockAttributes } = useDispatch("core/block-editor") as any; |
| 38 |
|
| 39 |
const setAttributes = (newAttributes: Record<string, any>) => { |
| 40 |
updateBlockAttributes(clientId, newAttributes); |
| 41 |
}; |
| 42 |
|
| 43 |
const value = attributes[attrKey]; |
| 44 |
|
| 45 |
return ( |
| 46 |
<ToolsPanelItem |
| 47 |
panelId={clientId} |
| 48 |
isShownByDefault={isShownByDefault} |
| 49 |
label={label} |
| 50 |
hasValue={() => value !== defaultValue} |
| 51 |
onDeselect={() => setAttributes({ [attrKey]: defaultValue })} |
| 52 |
resetAllFilter={() => setAttributes({ [attrKey]: defaultValue })} |
| 53 |
> |
| 54 |
<SelectControl |
| 55 |
label={label} |
| 56 |
value={value} |
| 57 |
options={options} |
| 58 |
onChange={(newValue: any) => { |
| 59 |
setAttributes({ [attrKey]: newValue }); |
| 60 |
}} |
| 61 |
help={help} |
| 62 |
size={"__unstable-large"} |
| 63 |
__next40pxDefaultSize |
| 64 |
__nextHasNoMarginBottom |
| 65 |
/> |
| 66 |
</ToolsPanelItem> |
| 67 |
); |
| 68 |
}; |
| 69 |
|
| 70 |
export default SelectControlWithToolsPanel; |
| 71 |
|