@wordpress/components
Version:
UI components for WordPress.
166 lines (164 loc) • 5.6 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/components/src/select-control/index.tsx
var select_control_exports = {};
__export(select_control_exports, {
SelectControl: () => SelectControl,
default: () => select_control_default
});
module.exports = __toCommonJS(select_control_exports);
var import_clsx = __toESM(require("clsx"));
var import_compose = require("@wordpress/compose");
var import_element = require("@wordpress/element");
var import_base_control = __toESM(require("../base-control"));
var import_select_control_styles = require("./styles/select-control-styles");
var import_chevron_down = __toESM(require("./chevron-down"));
var import_use_deprecated_props = require("../utils/use-deprecated-props");
var import_deprecated_36px_size = require("../utils/deprecated-36px-size");
var import_jsx_runtime = require("react/jsx-runtime");
function useUniqueId(idProp) {
const instanceId = (0, import_compose.useInstanceId)(SelectControl);
const id = `inspector-select-control-${instanceId}`;
return idProp || id;
}
function SelectOptions({
options
}) {
return options.map(({
id,
label,
value,
...optionProps
}, index) => {
const key = id || `${label}-${value}-${index}`;
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("option", {
value,
...optionProps,
children: label
}, key);
});
}
function UnforwardedSelectControl(props, ref) {
const {
className,
disabled = false,
help,
hideLabelFromVision,
id: idProp,
label,
multiple = false,
onChange,
options = [],
size = "default",
value: valueProp,
labelPosition = "top",
children,
prefix,
suffix,
variant = "default",
__next40pxDefaultSize = false,
__nextHasNoMarginBottom = false,
__shouldNotWarnDeprecated36pxSize,
...restProps
} = (0, import_use_deprecated_props.useDeprecated36pxDefaultSizeProp)(props);
const id = useUniqueId(idProp);
const helpId = help ? `${id}__help` : void 0;
if (!options?.length && !children) {
return null;
}
const handleOnChange = (event) => {
if (props.multiple) {
const selectedOptions = Array.from(event.target.options).filter(({
selected
}) => selected);
const newValues = selectedOptions.map(({
value
}) => value);
props.onChange?.(newValues, {
event
});
return;
}
props.onChange?.(event.target.value, {
event
});
};
const classes = (0, import_clsx.default)("components-select-control", className);
(0, import_deprecated_36px_size.maybeWarnDeprecated36pxSize)({
componentName: "SelectControl",
__next40pxDefaultSize,
size,
__shouldNotWarnDeprecated36pxSize
});
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_base_control.default, {
help,
id,
className: classes,
__nextHasNoMarginBottom,
__associatedWPComponentName: "SelectControl",
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_select_control_styles.StyledInputBase, {
disabled,
hideLabelFromVision,
id,
isBorderless: variant === "minimal",
label,
size,
suffix: suffix || !multiple && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_chevron_down.default, {}),
prefix,
labelPosition,
__unstableInputWidth: variant === "minimal" ? "auto" : void 0,
variant,
__next40pxDefaultSize,
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_select_control_styles.Select, {
...restProps,
__next40pxDefaultSize,
"aria-describedby": helpId,
className: "components-select-control__input",
disabled,
id,
multiple,
onChange: handleOnChange,
ref,
selectSize: size,
value: valueProp,
variant,
children: children || /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectOptions, {
options
})
})
})
});
}
var SelectControl = (0, import_element.forwardRef)(UnforwardedSelectControl);
var select_control_default = SelectControl;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SelectControl
});
//# sourceMappingURL=index.js.map