UNPKG

@wordpress/components

Version:
131 lines (130 loc) • 5.07 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js 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 __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let clsx = require("clsx"); clsx = __toESM(clsx); let _wordpress_element = require("@wordpress/element"); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_deprecated = require("@wordpress/deprecated"); _wordpress_deprecated = __toESM(_wordpress_deprecated); let _wordpress_icons = require("@wordpress/icons"); let ___base_control_index_tsx = require("../base-control/index.cjs"); ___base_control_index_tsx = __toESM(___base_control_index_tsx); let ___h_stack_index_ts = require("../h-stack/index.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/components/src/checkbox-control/index.tsx /** * Checkboxes allow the user to select one or more items from a set. * * ```jsx * import { CheckboxControl } from '@wordpress/components'; * import { useState } from '@wordpress/element'; * * const MyCheckboxControl = () => { * const [ isChecked, setChecked ] = useState( true ); * return ( * <CheckboxControl * label="Is author" * help="Is the user a author or not?" * checked={ isChecked } * onChange={ setChecked } * /> * ); * }; * ``` */ function CheckboxControl(props) { const { __nextHasNoMarginBottom: _, label, className, heading, checked, indeterminate, help, id: idProp, onChange, onClick, ...additionalProps } = props; if (heading) (0, _wordpress_deprecated.default)("`heading` prop in `CheckboxControl`", { alternative: "a separate element to implement a heading", since: "5.8" }); const [showCheckedIcon, setShowCheckedIcon] = (0, _wordpress_element.useState)(false); const [showIndeterminateIcon, setShowIndeterminateIcon] = (0, _wordpress_element.useState)(false); const ref = (0, _wordpress_compose.useRefEffect)((node) => { if (!node) return; node.indeterminate = !!indeterminate; setShowCheckedIcon(node.matches(":checked")); setShowIndeterminateIcon(node.matches(":indeterminate")); }, [checked, indeterminate]); const id = (0, _wordpress_compose.useInstanceId)(CheckboxControl, "inspector-checkbox-control", idProp); const onChangeValue = (event) => onChange(event.target.checked); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___base_control_index_tsx.default, { label: heading, id, help: help && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: "components-checkbox-control__help", children: help }), className: (0, clsx.default)("components-checkbox-control", className), children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(___h_stack_index_ts.HStack, { spacing: 0, justify: "start", alignment: "top", children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", { className: "components-checkbox-control__input-container", children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", { ref, id, className: "components-checkbox-control__input", type: "checkbox", value: "1", onChange: onChangeValue, checked, "aria-describedby": !!help ? id + "__help" : void 0, onClick: (event) => { event.currentTarget.focus(); onClick?.(event); }, ...additionalProps }), showIndeterminateIcon ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_icons.Icon, { icon: _wordpress_icons.reset, className: "components-checkbox-control__indeterminate", role: "presentation", size: 16, viewBox: "4 4 16 16" }) : null, showCheckedIcon ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_icons.Icon, { icon: _wordpress_icons.check, className: "components-checkbox-control__checked", role: "presentation", size: 16, viewBox: "4 4 16 16" }) : null ] }), label && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("label", { className: "components-checkbox-control__label", htmlFor: id, children: label })] }) }); } exports.CheckboxControl = CheckboxControl; exports.default = CheckboxControl; //# sourceMappingURL=index.cjs.map