UNPKG

@helpwave/hightide

Version:

helpwave's component and theming library

140 lines (138 loc) 3.83 kB
// src/components/user-action/Checkbox.tsx import { useState } from "react"; import * as CheckboxPrimitive from "@radix-ui/react-checkbox"; import { Check, Minus } from "lucide-react"; import clsx2 from "clsx"; // src/components/user-action/Label.tsx import clsx from "clsx"; 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", className, ...props }) => { return /* @__PURE__ */ jsx("label", { ...props, className: clsx(styleMapping[labelType], className), children: children ? children : name }); }; // src/components/user-action/Checkbox.tsx import { jsx as jsx2, jsxs } from "react/jsx-runtime"; var checkboxSizeMapping = { small: "size-5", medium: "size-6", large: "size-8" }; var checkboxIconSizeMapping = { small: "size-4", medium: "size-5", large: "size-7" }; var Checkbox = ({ 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 = () => { if (disabled) { return; } const newValue = checked === "indeterminate" ? false : !checked; propagateChange(newValue); }; return /* @__PURE__ */ jsxs( "div", { className: clsx2("group flex-row-2 items-center", { "cursor-pointer": !disabled, "cursor-not-allowed": disabled }, containerClassName), onClick: changeValue, children: [ /* @__PURE__ */ jsx2( CheckboxPrimitive.Root, { onCheckedChange: propagateChange, checked, disabled, id, className: clsx2(usedSizeClass, `items-center border-2 rounded outline-none `, { "text-disabled-text border-disabled-outline bg-disabled-background cursor-not-allowed": disabled, "focus:border-primary group-hover:border-primary ": !disabled, "bg-input-background": !disabled && !checked, "bg-primary/30 border-primary text-primary": !disabled && checked === true || checked === "indeterminate" }, 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: clsx2( label.className, { "cursor-pointer": !disabled, "cursor-not-allowed": disabled } ), htmlFor: id } ) ] } ); }; var CheckboxUncontrolled = ({ 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( Checkbox, { ...props, checked, onChangeTristate: handleChange } ); }; export { Checkbox, CheckboxUncontrolled }; //# sourceMappingURL=Checkbox.mjs.map