@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.
124 lines (123 loc) • 5.15 kB
JavaScript
"use strict";
'use client';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CheckboxGroup = void 0;
var React = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _useComponentRenderer = require("../utils/useComponentRenderer");
var _useCheckboxGroup = require("./useCheckboxGroup");
var _CheckboxGroupContext = require("./CheckboxGroupContext");
var _FieldRootContext = require("../field/root/FieldRootContext");
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; }
/**
* Provides a shared state to a series of checkboxes.
*
* Documentation: [Base UI Checkbox Group](https://base-ui.com/react/components/checkbox-group)
*/
const CheckboxGroup = exports.CheckboxGroup = /*#__PURE__*/React.forwardRef(function CheckboxGroup(props, forwardedRef) {
const {
render,
className,
value: externalValue,
defaultValue,
onValueChange,
allValues,
disabled: disabledProp = false,
...otherProps
} = props;
const {
disabled: fieldDisabled,
state: fieldState
} = (0, _FieldRootContext.useFieldRootContext)();
const disabled = fieldDisabled || disabledProp;
const {
getRootProps,
value,
setValue,
parent
} = (0, _useCheckboxGroup.useCheckboxGroup)({
value: externalValue,
allValues,
defaultValue,
onValueChange
});
const state = React.useMemo(() => ({
...fieldState,
disabled
}), [fieldState, disabled]);
const {
renderElement
} = (0, _useComponentRenderer.useComponentRenderer)({
propGetter: getRootProps,
render: render ?? 'div',
className,
state,
ref: forwardedRef,
extraProps: otherProps
});
const contextValue = React.useMemo(() => ({
allValues,
value,
defaultValue,
setValue,
parent,
disabled
}), [allValues, value, defaultValue, setValue, parent, disabled]);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_CheckboxGroupContext.CheckboxGroupContext.Provider, {
value: contextValue,
children: renderElement()
});
});
process.env.NODE_ENV !== "production" ? CheckboxGroup.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Names of all checkboxes in the group. Use this when creating a parent checkbox.
*/
allValues: _propTypes.default.arrayOf(_propTypes.default.string),
/**
* @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]),
/**
* Names of the checkboxes in the group that should be initially ticked.
*
* To render a controlled checkbox group, use the `value` prop instead.
*/
defaultValue: _propTypes.default.arrayOf(_propTypes.default.string),
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled: _propTypes.default.bool,
/**
* Event handler called when a checkbox in the group is ticked or unticked.
* Provides the new value as an argument.
*/
onValueChange: _propTypes.default.func,
/**
* 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]),
/**
* Names of the checkboxes in the group that should be ticked.
*
* To render an uncontrolled checkbox group, use the `defaultValue` prop instead.
*/
value: _propTypes.default.arrayOf(_propTypes.default.string)
} : void 0;