@wordpress/block-library
Version:
Block library for the WordPress editor.
50 lines (49 loc) • 1.51 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { ComboboxControl } from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import { store } from "@wordpress/core-data";
import { useMemo, useState } from "@wordpress/element";
import { debounce } from "@wordpress/compose";
import { decodeEntities } from "@wordpress/html-entities";
import { jsx } from "react/jsx-runtime";
// packages/block-library/src/avatar/user-control.jsx
const AUTHORS_QUERY = {
who: "authors",
per_page: 100,
_fields: "id,name",
context: "view"
};
function UserControl({ value, onChange }) {
const [filterValue, setFilterValue] = useState("");
const { authors, isLoading } = useSelect((select) => {
const { getUsers, isResolving } = select(store);
const query = { ...AUTHORS_QUERY };
if (filterValue) {
query.search = filterValue;
query.search_columns = ["name"];
}
return {
authors: getUsers(query),
isLoading: isResolving("getUsers", [query])
};
}, [filterValue]);
const options = useMemo(() => {
return (authors ?? []).map((author) => {
return {
value: author.id,
label: decodeEntities(author.name)
};
});
}, [authors]);
return /* @__PURE__ */ jsx(ComboboxControl, {
label: __("User"),
help: __("Select the avatar user to display, if it is blank it will use the post/page author."),
value,
onChange,
options,
onFilterValueChange: debounce(setFilterValue, 300),
isLoading
});
}
export { UserControl as default };
//# sourceMappingURL=user-control.mjs.map