@wordpress/block-library
Version:
Block library for the WordPress editor.
162 lines (161 loc) • 5.98 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { InspectorControls } from "@wordpress/block-editor";
import { useToolsPanelDropdownMenuProps } from "../../../utils/hooks.mjs";
import { usePublicTaxonomies } from "../../utils.mjs";
import TaxonomyControl from "./taxonomy-control.mjs";
import OrderControl from "./order-control.mjs";
import EmptyTermsControl from "./empty-terms-control.mjs";
import NestedTermsControl from "./nested-terms-control.mjs";
import InheritControl from "./inherit-control.mjs";
import MaxTermsControl from "./max-terms-control.mjs";
import AdvancedControls from "./advanced-controls.mjs";
import IncludeControl from "./include-control.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/terms-query/edit/inspector-controls/index.jsx
function TermsQueryInspectorControls({ attributes, setQuery, setAttributes, clientId, templateSlug }) {
const { termQuery, tagName: TagName } = attributes;
const { taxonomy, orderBy, order, hideEmpty, inherit, showNested, perPage, include } = termQuery;
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const isTaxonomyHierarchical = usePublicTaxonomies().find((_taxonomy) => _taxonomy.slug === taxonomy)?.hierarchical;
const inheritQuery = !!inherit;
const displayInheritControl = [
"taxonomy",
"category",
"tag",
"archive"
].includes(templateSlug) || templateSlug?.startsWith("taxonomy-") || templateSlug?.startsWith("category-") || templateSlug?.startsWith("tag-");
const displayShowNestedControl = isTaxonomyHierarchical;
const hasIncludeFilter = !!include?.length;
const queryTypeControlLabel = __("Query type");
const taxonomyControlLabel = __("Taxonomy");
const orderByControlLabel = __("Order by");
const emptyTermsControlLabel = __("Show empty terms");
const nestedTermsControlLabel = __("Show nested terms");
const maxTermsControlLabel = __("Max terms");
const includeControlLabel = __("Selected terms");
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({ termQuery: {
taxonomy: "category",
order: "asc",
orderBy: "name",
include: [],
hideEmpty: true,
showNested: false,
inherit: false,
perPage: 10
} });
},
dropdownMenuProps,
children: [
displayInheritControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => inherit !== false,
label: queryTypeControlLabel,
onDeselect: () => setQuery({ inherit: false }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(InheritControl, {
label: queryTypeControlLabel,
value: inherit,
onChange: setQuery
})
}),
!inheritQuery && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => taxonomy !== "category",
label: taxonomyControlLabel,
onDeselect: () => {
setQuery({ taxonomy: "category" });
},
isShownByDefault: true,
children: /* @__PURE__ */ jsx(TaxonomyControl, {
label: taxonomyControlLabel,
value: taxonomy,
onChange: (value) => setQuery({
taxonomy: value,
include: []
})
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => orderBy !== "name" || order !== "asc",
label: orderByControlLabel,
onDeselect: () => setQuery({
orderBy: "name",
order: "asc"
}),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(OrderControl, {
label: orderByControlLabel,
orderBy,
order,
onChange: (newOrderBy, newOrder) => {
setQuery({
orderBy: newOrderBy,
order: newOrder
});
},
disabled: hasIncludeFilter,
help: hasIncludeFilter ? __("When specific terms are selected, the order is based on their selection order.") : void 0
})
}),
!inheritQuery && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!include?.length,
label: includeControlLabel,
onDeselect: () => setQuery({
include: [],
orderBy: "name",
order: "asc"
}),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(IncludeControl, {
label: includeControlLabel,
taxonomy,
value: include,
onChange: (value) => setQuery({ include: value })
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => hideEmpty !== true,
label: emptyTermsControlLabel,
onDeselect: () => setQuery({ hideEmpty: true }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(EmptyTermsControl, {
label: emptyTermsControlLabel,
value: hideEmpty,
onChange: (value) => setQuery({ hideEmpty: value })
})
}),
displayShowNestedControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => showNested !== false,
label: nestedTermsControlLabel,
onDeselect: () => setQuery({ showNested: false }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(NestedTermsControl, {
label: nestedTermsControlLabel,
value: showNested,
onChange: (value) => setQuery({ showNested: value }),
disabled: hasIncludeFilter,
help: hasIncludeFilter ? __("When specific terms are selected, only those are displayed.") : void 0
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => perPage !== 10,
label: maxTermsControlLabel,
onDeselect: () => setQuery({ perPage: 10 }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(MaxTermsControl, {
label: maxTermsControlLabel,
value: perPage,
onChange: (value) => setQuery({ perPage: value })
})
})
]
}) }), /* @__PURE__ */ jsx(AdvancedControls, {
TagName,
setAttributes,
clientId
})] });
}
export { TermsQueryInspectorControls as default };
//# sourceMappingURL=index.mjs.map