@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
201 lines (200 loc) • 7.8 kB
JavaScript
"use strict";
'use client';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CheckboxRoot = void 0;
var React = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _CheckboxGroupContext = require("../../checkbox-group/CheckboxGroupContext");
var _FieldRootContext = require("../../field/root/FieldRootContext");
var _useComponentRenderer = require("../../utils/useComponentRenderer");
var _useCustomStyleHookMapping = require("../utils/useCustomStyleHookMapping");
var _useCheckboxRoot = require("./useCheckboxRoot");
var _CheckboxRootContext = require("./CheckboxRootContext");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
* Represents the checkbox itself.
* Renders a `<button>` element and a hidden `<input>` beside.
*
* Documentation: [Base UI Checkbox](https://base-ui.com/react/components/checkbox)
*/const CheckboxRoot = exports.CheckboxRoot = /*#__PURE__*/React.forwardRef(function CheckboxRoot(props, forwardedRef) {
const {
name,
onCheckedChange,
defaultChecked,
parent = false,
readOnly = false,
indeterminate = false,
required = false,
disabled: disabledProp = false,
checked: checkedProp,
render,
className,
inputRef,
value,
...otherProps
} = props;
const groupContext = (0, _CheckboxGroupContext.useCheckboxGroupContext)();
const parentContext = groupContext?.parent;
const isGrouped = parentContext && groupContext.allValues;
let groupProps = {};
if (isGrouped) {
if (parent) {
groupProps = groupContext.parent.getParentProps();
} else if (name) {
groupProps = groupContext.parent.getChildProps(name);
}
}
const {
checked: groupChecked = checkedProp,
indeterminate: groupIndeterminate = indeterminate,
onCheckedChange: groupOnChange = onCheckedChange,
...otherGroupProps
} = groupProps;
const {
state: fieldState,
disabled: fieldDisabled
} = (0, _FieldRootContext.useFieldRootContext)();
const disabled = fieldDisabled || groupContext?.disabled || disabledProp;
const {
checked,
getInputProps,
getButtonProps
} = (0, _useCheckboxRoot.useCheckboxRoot)({
...props,
disabled,
inputRef,
checked: groupChecked,
indeterminate: groupIndeterminate,
onCheckedChange: groupOnChange
});
const computedChecked = isGrouped ? Boolean(groupChecked) : checked;
const computedIndeterminate = isGrouped ? groupIndeterminate : indeterminate;
React.useEffect(() => {
if (parentContext && name) {
parentContext.disabledStatesRef.current.set(name, disabled);
}
}, [parentContext, disabled, name]);
const state = React.useMemo(() => ({
...fieldState,
checked: computedChecked,
disabled,
readOnly,
required,
indeterminate: computedIndeterminate
}), [fieldState, computedChecked, disabled, readOnly, required, computedIndeterminate]);
const customStyleHookMapping = (0, _useCustomStyleHookMapping.useCustomStyleHookMapping)(state);
const {
renderElement
} = (0, _useComponentRenderer.useComponentRenderer)({
propGetter: getButtonProps,
render: render ?? 'button',
ref: forwardedRef,
state,
className,
customStyleHookMapping,
extraProps: {
...otherProps,
...otherGroupProps
}
});
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_CheckboxRootContext.CheckboxRootContext.Provider, {
value: state,
children: [renderElement(), !checked && props.name && /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
type: "hidden",
name: props.name,
value: "off"
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
...getInputProps()
})]
});
});
process.env.NODE_ENV !== "production" ? CheckboxRoot.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Whether the checkbox is currently ticked.
*
* To render an uncontrolled checkbox, use the `defaultChecked` prop instead.
* @default undefined
*/
checked: _propTypes.default.bool,
/**
* @ignore
*/
children: _propTypes.default.node,
/**
* CSS class applied to the element, or a function that
* returns a class based on the component’s state.
*/
className: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.string]),
/**
* Whether the checkbox is initially ticked.
*
* To render a controlled checkbox, use the `checked` prop instead.
* @default false
*/
defaultChecked: _propTypes.default.bool,
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled: _propTypes.default.bool,
/**
* Whether the checkbox is in a mixed state: neither ticked, nor unticked.
* @default false
*/
indeterminate: _propTypes.default.bool,
/**
* A React ref to access the hidden `<input>` element.
*/
inputRef: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.shape({
current: _propTypes.default.object
})]),
/**
* Identifies the field when a form is submitted.
* @default undefined
*/
name: _propTypes.default.string,
/**
* Event handler called when the checkbox is ticked or unticked.
*
* @param {boolean} checked The new checked state.
* @param {Event} event The corresponding event that initiated the change.
*/
onCheckedChange: _propTypes.default.func,
/**
* Whether the checkbox controls a group of child checkboxes.
*
* Must be used in a [Checkbox Group](https://base-ui.com/react/components/checkbox-group).
* @default false
*/
parent: _propTypes.default.bool,
/**
* Whether the user should be unable to tick or untick the checkbox.
* @default false
*/
readOnly: _propTypes.default.bool,
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
*
* Accepts a `ReactElement` or a function that returns the element to render.
*/
render: _propTypes.default.oneOfType([_propTypes.default.element, _propTypes.default.func]),
/**
* Whether the user must tick the checkbox before submitting a form.
* @default false
*/
required: _propTypes.default.bool,
/**
* The value of the selected checkbox.
*/
value: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string])
} : void 0;