UNPKG

@helpwave/hightide

Version:

helpwave's component and theming library

112 lines (110 loc) 3.34 kB
// src/components/user-input/Checkbox.tsx import { useState } from "react"; import * as CheckboxPrimitive from "@radix-ui/react-checkbox"; import { Check, Minus } from "lucide-react"; import clsx from "clsx"; // src/components/user-input/Label.tsx import { jsx } from "react/jsx-runtime"; var styleMapping = { labelSmall: "textstyle-label-sm", labelMedium: "textstyle-label-md", labelBig: "textstyle-label-lg" }; var Label = ({ children, name, labelType = "labelSmall", ...props }) => { return /* @__PURE__ */ jsx("label", { ...props, children: children ? children : /* @__PURE__ */ jsx("span", { className: styleMapping[labelType], children: name }) }); }; // src/components/user-input/Checkbox.tsx import { jsx as jsx2, jsxs } from "react/jsx-runtime"; var checkboxSizeMapping = { small: "size-4", medium: "size-6", large: "size-8" }; var checkboxIconSizeMapping = { small: "size-3", medium: "size-5", large: "size-7" }; var ControlledCheckbox = ({ id, label, checked, disabled, onChange, onChangeTristate, size = "medium", className = "", containerClassName }) => { const usedSizeClass = checkboxSizeMapping[size]; const innerIconSize = checkboxIconSizeMapping[size]; const propagateChange = (checked2) => { if (onChangeTristate) { onChangeTristate(checked2); } if (onChange) { onChange(checked2 === "indeterminate" ? false : checked2); } }; const changeValue = () => { const newValue = checked === "indeterminate" ? false : !checked; propagateChange(newValue); }; return /* @__PURE__ */ jsxs("div", { className: clsx("row justify-center items-center", containerClassName), children: [ /* @__PURE__ */ jsx2( CheckboxPrimitive.Root, { onCheckedChange: propagateChange, checked, disabled, id, className: clsx(usedSizeClass, `items-center border-2 rounded outline-none focus:border-primary`, { "text-disabled-text border-disabled-text": disabled, "border-on-background": !disabled, "bg-primary/30 border-primary text-primary": checked === true || checked === "indeterminate", "hover:border-gray-400 focus:hover:border-primary": !checked }, className), children: /* @__PURE__ */ jsxs(CheckboxPrimitive.Indicator, { children: [ checked === true && /* @__PURE__ */ jsx2(Check, { className: innerIconSize }), checked === "indeterminate" && /* @__PURE__ */ jsx2(Minus, { className: innerIconSize }) ] }) } ), label && /* @__PURE__ */ jsx2(Label, { ...label, className: clsx("cursor-pointer", label.className), htmlFor: id, onClick: changeValue }) ] }); }; var UncontrolledCheckbox = ({ onChange, onChangeTristate, defaultValue = false, ...props }) => { const [checked, setChecked] = useState(defaultValue); const handleChange = (checked2) => { if (onChangeTristate) { onChangeTristate(checked2); } if (onChange) { onChange(checked2 === "indeterminate" ? false : checked2); } setChecked(checked2); }; return /* @__PURE__ */ jsx2( ControlledCheckbox, { ...props, checked, onChangeTristate: handleChange } ); }; export { ControlledCheckbox as Checkbox, UncontrolledCheckbox }; //# sourceMappingURL=Checkbox.mjs.map