@wordpress/block-library
Version:
Block library for the WordPress editor.
107 lines (106 loc) • 3.8 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_components = require("@wordpress/components");
let _wordpress_data = require("@wordpress/data");
let _wordpress_core_data = require("@wordpress/core-data");
let _wordpress_element = require("@wordpress/element");
let _wordpress_compose = require("@wordpress/compose");
let ______utils_js = require("../../utils.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/query/edit/inspector-controls/parent-control.jsx
const EMPTY_ARRAY = [];
const BASE_QUERY = {
order: "asc",
_fields: "id,title",
context: "view"
};
function ParentControl({ parents, postType, onChange }) {
const [search, setSearch] = (0, _wordpress_element.useState)("");
const [value, setValue] = (0, _wordpress_element.useState)(EMPTY_ARRAY);
const [suggestions, setSuggestions] = (0, _wordpress_element.useState)(EMPTY_ARRAY);
const debouncedSearch = (0, _wordpress_compose.useDebounce)(setSearch, 250);
const { searchResults, searchHasResolved } = (0, _wordpress_data.useSelect)((select) => {
if (!search) return {
searchResults: EMPTY_ARRAY,
searchHasResolved: true
};
const { getEntityRecords, hasFinishedResolution } = select(_wordpress_core_data.store);
const selectorArgs = [
"postType",
postType,
{
...BASE_QUERY,
search,
orderby: "relevance",
exclude: parents,
per_page: 20
}
];
return {
searchResults: getEntityRecords(...selectorArgs),
searchHasResolved: hasFinishedResolution("getEntityRecords", selectorArgs)
};
}, [
search,
postType,
parents
]);
const currentParents = (0, _wordpress_data.useSelect)((select) => {
if (!parents?.length) return EMPTY_ARRAY;
const { getEntityRecords } = select(_wordpress_core_data.store);
return getEntityRecords("postType", postType, {
...BASE_QUERY,
include: parents,
per_page: parents.length
});
}, [parents, postType]);
(0, _wordpress_element.useEffect)(() => {
if (!parents?.length) setValue(EMPTY_ARRAY);
if (!currentParents?.length) return;
const currentParentsInfo = (0, ______utils_js.getEntitiesInfo)((0, ______utils_js.mapToIHasNameAndId)(currentParents, "title.rendered"));
const sanitizedValue = parents.reduce((accumulator, id) => {
const entity = currentParentsInfo.mapById[id];
if (entity) accumulator.push({
id,
value: entity.name
});
return accumulator;
}, []);
setValue(sanitizedValue);
}, [parents, currentParents]);
const entitiesInfo = (0, _wordpress_element.useMemo)(() => {
if (!searchResults?.length) return EMPTY_ARRAY;
return (0, ______utils_js.getEntitiesInfo)((0, ______utils_js.mapToIHasNameAndId)(searchResults, "title.rendered"));
}, [searchResults]);
(0, _wordpress_element.useEffect)(() => {
if (!searchHasResolved) return;
setSuggestions(entitiesInfo.names);
}, [entitiesInfo.names, searchHasResolved]);
const getIdByValue = (entitiesMappedByName, entity) => {
const id = entity?.id || entitiesMappedByName?.[entity]?.id;
if (id) return id;
};
const onParentChange = (newValue) => {
const ids = Array.from(newValue.reduce((accumulator, entity) => {
const id = getIdByValue(entitiesInfo.mapByName, entity);
if (id) accumulator.add(id);
return accumulator;
}, /* @__PURE__ */ new Set()));
setSuggestions(EMPTY_ARRAY);
onChange({ parents: ids });
};
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.FormTokenField, {
label: (0, _wordpress_i18n.__)("Parents"),
value,
onInputChange: debouncedSearch,
suggestions,
onChange: onParentChange,
help: ""
});
}
exports.default = ParentControl;
//# sourceMappingURL=parent-control.cjs.map