UNPKG

@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.

117 lines (116 loc) 3.89 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useComponentRenderer } from '../utils/useComponentRenderer.js'; import { useCheckboxGroup } from './useCheckboxGroup.js'; import { CheckboxGroupContext } from './CheckboxGroupContext.js'; import { useFieldRootContext } from '../field/root/FieldRootContext.js'; import { jsx as _jsx } from "react/jsx-runtime"; /** * Provides a shared state to a series of checkboxes. * * Documentation: [Base UI Checkbox Group](https://base-ui.com/react/components/checkbox-group) */ const 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 } = useFieldRootContext(); const disabled = fieldDisabled || disabledProp; const { getRootProps, value, setValue, parent } = useCheckboxGroup({ value: externalValue, allValues, defaultValue, onValueChange }); const state = React.useMemo(() => ({ ...fieldState, disabled }), [fieldState, disabled]); const { renderElement } = 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__*/_jsx(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.arrayOf(PropTypes.string), /** * @ignore */ children: PropTypes.node, /** * CSS class applied to the element, or a function that * returns a class based on the component’s state. */ className: PropTypes.oneOfType([PropTypes.func, PropTypes.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.arrayOf(PropTypes.string), /** * Whether the component should ignore user interaction. * @default false */ disabled: PropTypes.bool, /** * Event handler called when a checkbox in the group is ticked or unticked. * Provides the new value as an argument. */ onValueChange: PropTypes.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.oneOfType([PropTypes.element, PropTypes.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.arrayOf(PropTypes.string) } : void 0; export { CheckboxGroup };