@wordpress/components
Version:
UI components for WordPress.
101 lines (100 loc) • 3.34 kB
JavaScript
import clsx from "clsx";
import { useState } from "@wordpress/element";
import { useInstanceId, useRefEffect } from "@wordpress/compose";
import deprecated from "@wordpress/deprecated";
import { Icon, check, reset } from "@wordpress/icons";
import BaseControl from "../base-control/index.mjs";
import { HStack } from "../h-stack/index.mjs";
import { jsx, jsxs } from "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) deprecated("`heading` prop in `CheckboxControl`", {
alternative: "a separate element to implement a heading",
since: "5.8"
});
const [showCheckedIcon, setShowCheckedIcon] = useState(false);
const [showIndeterminateIcon, setShowIndeterminateIcon] = useState(false);
const ref = useRefEffect((node) => {
if (!node) return;
node.indeterminate = !!indeterminate;
setShowCheckedIcon(node.matches(":checked"));
setShowIndeterminateIcon(node.matches(":indeterminate"));
}, [checked, indeterminate]);
const id = useInstanceId(CheckboxControl, "inspector-checkbox-control", idProp);
const onChangeValue = (event) => onChange(event.target.checked);
return /* @__PURE__ */ jsx(BaseControl, {
label: heading,
id,
help: help && /* @__PURE__ */ jsx("span", {
className: "components-checkbox-control__help",
children: help
}),
className: clsx("components-checkbox-control", className),
children: /* @__PURE__ */ jsxs(HStack, {
spacing: 0,
justify: "start",
alignment: "top",
children: [/* @__PURE__ */ jsxs("span", {
className: "components-checkbox-control__input-container",
children: [
/* @__PURE__ */ 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__ */ jsx(Icon, {
icon: reset,
className: "components-checkbox-control__indeterminate",
role: "presentation",
size: 16,
viewBox: "4 4 16 16"
}) : null,
showCheckedIcon ? /* @__PURE__ */ jsx(Icon, {
icon: check,
className: "components-checkbox-control__checked",
role: "presentation",
size: 16,
viewBox: "4 4 16 16"
}) : null
]
}), label && /* @__PURE__ */ jsx("label", {
className: "components-checkbox-control__label",
htmlFor: id,
children: label
})]
})
});
}
export { CheckboxControl, CheckboxControl as default };
//# sourceMappingURL=index.mjs.map