UNPKG

@trail-ui/react

Version:
67 lines (64 loc) 2.31 kB
import { CheckboxGroupThemeContext } from "./chunk-ONDCPUNY.mjs"; import { CheckboxIcon } from "./chunk-T6I3VURN.mjs"; // src/checkbox/checkbox.tsx import { clsx } from "@trail-ui/shared-utils"; import { checkbox } from "@trail-ui/theme"; import { cloneElement, forwardRef, useContext, useMemo } from "react"; import { Checkbox as AriaCheckbox } from "react-aria-components"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; function Checkbox(props, ref) { var _a, _b; const groupThemeContext = useContext(CheckboxGroupThemeContext); const { icon = /* @__PURE__ */ jsx(CheckboxIcon, {}), lineThrough = (_a = groupThemeContext == null ? void 0 : groupThemeContext.lineThrough) != null ? _a : false, isInvalid = (_b = groupThemeContext == null ? void 0 : groupThemeContext.isInvalid) != null ? _b : false, className, classNames, children, ...otherProps } = props; const slots = useMemo( () => checkbox({ lineThrough, isInvalid }), [isInvalid, lineThrough] ); const baseStyles = clsx(classNames == null ? void 0 : classNames.base, className); return /* @__PURE__ */ jsx(AriaCheckbox, { ref, className: slots.base({ class: baseStyles }), ...otherProps, children: (renderProps) => { const iconProps = { "data-checked": String(renderProps.isSelected), isSelected: renderProps.isSelected, isIndeterminate: renderProps.isIndeterminate, isInvalid: renderProps.isInvalid, className: slots.icon({ class: classNames == null ? void 0 : classNames.icon }) }; const clonedIcon = typeof icon === "function" ? icon(iconProps) : cloneElement(icon, iconProps); return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx( "span", { "aria-hidden": "true", className: slots.control({ class: classNames == null ? void 0 : classNames.control }), "data-indeterminate": props.isIndeterminate, children: clonedIcon } ), children && /* @__PURE__ */ jsx("span", { className: slots.label({ class: classNames == null ? void 0 : classNames.label }), children: typeof children === "function" ? children(renderProps) : children }) ] }); } }); } var _Checkbox = forwardRef(Checkbox); export { _Checkbox };