@wordpress/components
Version:
UI components for WordPress.
118 lines (117 loc) • 4.2 kB
JavaScript
import { __, _x } from "@wordpress/i18n";
import AuthorSelect from "./author-select";
import CategorySelect from "./category-select";
import FormTokenField from "../form-token-field";
import RangeControl from "../range-control";
import SelectControl from "../select-control";
import { VStack } from "../v-stack";
import { jsx as _jsx } from "react/jsx-runtime";
const DEFAULT_MIN_ITEMS = 1;
const DEFAULT_MAX_ITEMS = 100;
const MAX_CATEGORIES_SUGGESTIONS = 20;
function isSingleCategorySelection(props) {
return "categoriesList" in props;
}
function isMultipleCategorySelection(props) {
return "categorySuggestions" in props;
}
const defaultOrderByOptions = [{
label: __("Newest to oldest"),
value: "date/desc"
}, {
label: __("Oldest to newest"),
value: "date/asc"
}, {
/* translators: Label for ordering posts by title in ascending order. */
label: __("A \u2192 Z"),
value: "title/asc"
}, {
/* translators: Label for ordering posts by title in descending order. */
label: __("Z \u2192 A"),
value: "title/desc"
}];
function QueryControls({
authorList,
selectedAuthorId,
numberOfItems,
order,
orderBy,
orderByOptions = defaultOrderByOptions,
maxItems = DEFAULT_MAX_ITEMS,
minItems = DEFAULT_MIN_ITEMS,
onAuthorChange,
onNumberOfItemsChange,
onOrderChange,
onOrderByChange,
// Props for single OR multiple category selection are not destructured here,
// but instead are destructured inline where necessary.
...props
}) {
return /* @__PURE__ */ _jsx(VStack, {
spacing: "4",
className: "components-query-controls",
children: [onOrderChange && onOrderByChange && /* @__PURE__ */ _jsx(SelectControl, {
__nextHasNoMarginBottom: true,
__next40pxDefaultSize: true,
label: __("Order by"),
value: orderBy === void 0 || order === void 0 ? void 0 : `${orderBy}/${order}`,
options: orderByOptions,
onChange: (value) => {
if (typeof value !== "string") {
return;
}
const [newOrderBy, newOrder] = value.split("/");
if (newOrder !== order) {
onOrderChange(newOrder);
}
if (newOrderBy !== orderBy) {
onOrderByChange(newOrderBy);
}
}
}, "query-controls-order-select"), isSingleCategorySelection(props) && props.categoriesList && props.onCategoryChange && /* @__PURE__ */ _jsx(CategorySelect, {
__next40pxDefaultSize: true,
categoriesList: props.categoriesList,
label: __("Category"),
noOptionLabel: _x("All", "categories"),
selectedCategoryId: props.selectedCategoryId,
onChange: props.onCategoryChange
}, "query-controls-category-select"), isMultipleCategorySelection(props) && props.categorySuggestions && props.onCategoryChange && /* @__PURE__ */ _jsx(FormTokenField, {
__next40pxDefaultSize: true,
__nextHasNoMarginBottom: true,
label: __("Categories"),
value: props.selectedCategories && props.selectedCategories.map((item) => ({
id: item.id,
// Keeping the fallback to `item.value` for legacy reasons,
// even if items of `selectedCategories` should not have a
// `value` property.
// @ts-expect-error
value: item.name || item.value
})),
suggestions: Object.keys(props.categorySuggestions),
onChange: props.onCategoryChange,
maxSuggestions: MAX_CATEGORIES_SUGGESTIONS
}, "query-controls-categories-select"), onAuthorChange && /* @__PURE__ */ _jsx(AuthorSelect, {
__next40pxDefaultSize: true,
authorList,
label: __("Author"),
noOptionLabel: _x("All", "authors"),
selectedAuthorId,
onChange: onAuthorChange
}, "query-controls-author-select"), onNumberOfItemsChange && /* @__PURE__ */ _jsx(RangeControl, {
__nextHasNoMarginBottom: true,
__next40pxDefaultSize: true,
label: __("Number of items"),
value: numberOfItems,
onChange: onNumberOfItemsChange,
min: minItems,
max: maxItems,
required: true
}, "query-controls-range-control")]
});
}
var query_controls_default = QueryControls;
export {
QueryControls,
query_controls_default as default
};
//# sourceMappingURL=index.js.map