UNPKG

@wordpress/block-library

Version:
156 lines (154 loc) 5.01 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-library/src/terms-query/edit/inspector-controls/include-control.js var include_control_exports = {}; __export(include_control_exports, { default: () => IncludeControl }); module.exports = __toCommonJS(include_control_exports); var import_components = require("@wordpress/components"); var import_data = require("@wordpress/data"); var import_core_data = require("@wordpress/core-data"); var import_element = require("@wordpress/element"); var import_compose = require("@wordpress/compose"); var import_html_entities = require("@wordpress/html-entities"); var import_jsx_runtime = require("react/jsx-runtime"); var EMPTY_ARRAY = []; var BASE_QUERY = { order: "asc", _fields: "id,name", context: "view" }; function IncludeControl({ value: include, taxonomy, onChange, ...props }) { const [search, setSearch] = (0, import_element.useState)(""); const [value, setValue] = (0, import_element.useState)(EMPTY_ARRAY); const [suggestions, setSuggestions] = (0, import_element.useState)(EMPTY_ARRAY); const debouncedSearch = (0, import_compose.useDebounce)(setSearch, 250); const { searchResults, searchHasResolved } = (0, import_data.useSelect)( (select) => { if (!search) { return { searchResults: EMPTY_ARRAY, searchHasResolved: true }; } const { getEntityRecords, hasFinishedResolution } = select(import_core_data.store); const selectorArgs = [ "taxonomy", taxonomy, { ...BASE_QUERY, search, orderby: "name", exclude: include, per_page: 20 } ]; return { searchResults: getEntityRecords(...selectorArgs), searchHasResolved: hasFinishedResolution( "getEntityRecords", selectorArgs ) }; }, [search, taxonomy, include] ); const currentTerms = (0, import_data.useSelect)( (select) => { if (!include?.length) { return EMPTY_ARRAY; } const { getEntityRecords } = select(import_core_data.store); return getEntityRecords("taxonomy", taxonomy, { ...BASE_QUERY, include, per_page: include.length }); }, [include, taxonomy] ); (0, import_element.useEffect)(() => { if (!include?.length) { setValue(EMPTY_ARRAY); } if (!currentTerms?.length) { return; } const sanitizedValue = include.reduce((accumulator, id) => { const entity = currentTerms.find((term) => term.id === id); if (entity) { accumulator.push({ id, value: (0, import_html_entities.decodeEntities)(entity.name) }); } return accumulator; }, []); setValue(sanitizedValue); }, [include, currentTerms]); const entitiesInfo = (0, import_element.useMemo)(() => { if (!searchResults?.length) { return { names: EMPTY_ARRAY, mapByName: {} }; } const names = []; const mapByName = {}; searchResults.forEach((result) => { const decodedName = (0, import_html_entities.decodeEntities)(result.name); names.push(decodedName); mapByName[decodedName] = result; }); return { names, mapByName }; }, [searchResults]); (0, import_element.useEffect)(() => { if (!searchHasResolved) { return; } setSuggestions(entitiesInfo.names); }, [entitiesInfo.names, searchHasResolved]); const getIdByValue = (entitiesMappedByName, entity) => entity?.id || entitiesMappedByName?.[entity]?.id; const onTermChange = (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(ids); }; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.FormTokenField, { __next40pxDefaultSize: true, value, onInputChange: debouncedSearch, suggestions, onChange: onTermChange, __experimentalShowHowTo: false, ...props } ); } //# sourceMappingURL=include-control.js.map