UNPKG

@nex-ui/react

Version:

🎉 A beautiful, modern, and reliable React component library.

51 lines (48 loc) • 1.51 kB
"use client"; import { jsx } from 'react/jsx-runtime'; import { useMemo } from 'react'; import { useControlledState } from '@nex-ui/hooks'; import { CheckboxGroupProvider } from './CheckboxGroupContext.mjs'; const CheckboxGroup = (props)=>{ const { name, onValueChange, children, disabled, size, color, radius, defaultValue = [], value: valueProp } = props; const [values, setValues] = useControlledState(valueProp, defaultValue, onValueChange); const ctx = useMemo(()=>({ disabled, name, size, color, radius, toggleValue: (value)=>{ if (disabled) { return; } let newValues; if (values.includes(value)) { newValues = values.filter((existingValue)=>existingValue !== value); } else { newValues = [ ...values, value ]; } setValues(newValues); }, isChecked: (value)=>{ return value ? values.includes(value) : false; } }), [ color, disabled, name, radius, setValues, size, values ]); return /*#__PURE__*/ jsx(CheckboxGroupProvider, { value: ctx, children: children }); }; CheckboxGroup.displayName = 'CheckboxGroup'; export { CheckboxGroup };