UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

142 lines (139 loc) 4.2 kB
import { c } from 'react-compiler-runtime'; import React from 'react'; import CheckboxOrRadioGroupCaption from '../internal/components/CheckboxOrRadioGroup/CheckboxOrRadioGroupCaption.js'; import CheckboxOrRadioGroupLabel from '../internal/components/CheckboxOrRadioGroup/CheckboxOrRadioGroupLabel.js'; import CheckboxOrRadioGroupValidation from '../internal/components/CheckboxOrRadioGroup/CheckboxOrRadioGroupValidation.js'; import Checkbox from '../Checkbox/Checkbox.js'; import { CheckboxGroupContext } from './CheckboxGroupContext.js'; import { jsx } from 'react/jsx-runtime'; import { useRenderForcingRef } from '../hooks/useRenderForcingRef.js'; import CheckboxOrRadioGroup from '../internal/components/CheckboxOrRadioGroup/CheckboxOrRadioGroup.js'; import FormControl from '../FormControl/FormControl.js'; const CheckboxGroup = t0 => { const $ = c(24); let children; let disabled; let onChange; let rest; if ($[0] !== t0) { ({ children, disabled, onChange, ...rest } = t0); $[0] = t0; $[1] = children; $[2] = disabled; $[3] = onChange; $[4] = rest; } else { children = $[1]; disabled = $[2]; onChange = $[3]; rest = $[4]; } let t1; if ($[5] !== children) { const formControlComponentChildren = React.Children.toArray(children).filter(_temp).map(_temp2).flat(); t1 = React.Children.toArray(formControlComponentChildren).filter(_temp3).map(_temp4).filter(Boolean); $[5] = children; $[6] = t1; } else { t1 = $[6]; } const checkedCheckboxes = t1; const [selectedCheckboxValues, setSelectedCheckboxValues] = useRenderForcingRef(checkedCheckboxes); let t2; if ($[7] !== selectedCheckboxValues || $[8] !== setSelectedCheckboxValues) { t2 = e => { const { value, checked } = e.currentTarget; if (checked) { setSelectedCheckboxValues([...(selectedCheckboxValues.current || []), value]); return; } setSelectedCheckboxValues((selectedCheckboxValues.current || []).filter(selectedValue => selectedValue !== value)); }; $[7] = selectedCheckboxValues; $[8] = setSelectedCheckboxValues; $[9] = t2; } else { t2 = $[9]; } const updateSelectedCheckboxes = t2; let t3; if ($[10] !== onChange || $[11] !== selectedCheckboxValues || $[12] !== updateSelectedCheckboxes) { t3 = e_0 => { if (onChange) { updateSelectedCheckboxes(e_0); onChange(selectedCheckboxValues.current || [], e_0); } }; $[10] = onChange; $[11] = selectedCheckboxValues; $[12] = updateSelectedCheckboxes; $[13] = t3; } else { t3 = $[13]; } let t4; if ($[14] !== disabled || $[15] !== t3) { t4 = { disabled, onChange: t3 }; $[14] = disabled; $[15] = t3; $[16] = t4; } else { t4 = $[16]; } let t5; if ($[17] !== children || $[18] !== disabled || $[19] !== rest) { t5 = /*#__PURE__*/jsx(CheckboxOrRadioGroup, { disabled: disabled, ...rest, children: children }); $[17] = children; $[18] = disabled; $[19] = rest; $[20] = t5; } else { t5 = $[20]; } let t6; if ($[21] !== t4 || $[22] !== t5) { t6 = /*#__PURE__*/jsx(CheckboxGroupContext.Provider, { value: t4, children: t5 }); $[21] = t4; $[22] = t5; $[23] = t6; } else { t6 = $[23]; } return t6; }; var CheckboxGroup$1 = Object.assign(CheckboxGroup, { Caption: CheckboxOrRadioGroupCaption, Label: CheckboxOrRadioGroupLabel, Validation: CheckboxOrRadioGroupValidation }); function _temp(child) { return /*#__PURE__*/React.isValidElement(child) && child.type === FormControl; } function _temp2(formControlComponent) { return /*#__PURE__*/React.isValidElement(formControlComponent) ? formControlComponent.props.children : []; } function _temp3(child_0) { return /*#__PURE__*/React.isValidElement(child_0) && child_0.type === Checkbox; } function _temp4(checkbox) { return /*#__PURE__*/React.isValidElement(checkbox) && (checkbox.props.checked || checkbox.props.defaultChecked) && checkbox.props.value; } export { CheckboxGroupContext, CheckboxGroup$1 as default };