@wordpress/block-library
Version:
Block library for the WordPress editor.
115 lines (114 loc) • 3.95 kB
JavaScript
import { Spinner, ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { __, sprintf } from "@wordpress/i18n";
import { InspectorControls, useBlockProps } from "@wordpress/block-editor";
import { useServerSideRender } from "@wordpress/server-side-render";
import { useDisabled } from "@wordpress/compose";
import { SelectControl } from "@wordpress/ui";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import HtmlRenderer from "../utils/html-renderer.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/archives/edit.jsx
const ARCHIVE_TYPE_ITEMS = [
{
label: __("Year"),
value: "yearly"
},
{
label: __("Month"),
value: "monthly"
},
{
label: __("Week"),
value: "weekly"
},
{
label: __("Day"),
value: "daily"
}
];
function ArchivesEdit({ attributes, setAttributes, name }) {
const { showLabel, showPostCounts, displayAsDropdown, type } = attributes;
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const { content, status, error } = useServerSideRender({
attributes,
skipBlockSupportAttributes: true,
block: name
});
const disabledRef = useDisabled();
const blockProps = useBlockProps({ ref: disabledRef });
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
displayAsDropdown: false,
showLabel: true,
showPostCounts: false,
type: "monthly"
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Display as dropdown"),
isShownByDefault: true,
hasValue: () => displayAsDropdown,
onDeselect: () => setAttributes({ displayAsDropdown: false }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Display as dropdown"),
checked: displayAsDropdown,
onChange: () => setAttributes({ displayAsDropdown: !displayAsDropdown })
})
}),
displayAsDropdown && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Show label"),
isShownByDefault: true,
hasValue: () => !showLabel,
onDeselect: () => setAttributes({ showLabel: true }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Show label"),
checked: showLabel,
onChange: () => setAttributes({ showLabel: !showLabel })
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Show post counts"),
isShownByDefault: true,
hasValue: () => showPostCounts,
onDeselect: () => setAttributes({ showPostCounts: false }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Show post counts"),
checked: showPostCounts,
onChange: () => setAttributes({ showPostCounts: !showPostCounts })
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Group by"),
isShownByDefault: true,
hasValue: () => type !== "monthly",
onDeselect: () => setAttributes({ type: "monthly" }),
children: /* @__PURE__ */ jsx(SelectControl, {
label: __("Group by"),
items: ARCHIVE_TYPE_ITEMS,
value: ARCHIVE_TYPE_ITEMS.find((item) => item.value === type) ?? ARCHIVE_TYPE_ITEMS[0],
onValueChange: (item) => item && setAttributes({ type: item.value })
})
})
]
}) }),
status === "loading" && /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx(Spinner, {})
}),
status === "error" && /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx("p", { children: sprintf(__("Error: %s"), error) })
}),
status === "success" && /* @__PURE__ */ jsx(HtmlRenderer, {
wrapperProps: blockProps,
html: content
})
] });
}
export { ArchivesEdit as default };
//# sourceMappingURL=edit.mjs.map