{"version":3,"file":"Checkbox.mjs","names":[],"sources":["../../../src/checkbox/Checkbox.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\n\nimport { useMergeRefs } from '@floating-ui/react';\nimport classNames from 'classnames';\nimport { useEffect, useRef } from 'react';\n\nimport {\n  useFormFieldGroup,\n  useInlineFieldLabel,\n} from '../_common/form-control';\nimport { type CommonProps, type HtmlAttributes } from '../_common/types';\nimport { useSemiControlledInput } from '../_common/use-semi-controlled-input';\n\ntype CheckboxProps = {\n  /** Whether the checkbox is checked */\n  isChecked?: boolean;\n  /** Checks the input by default in uncontrolled mode */\n  isDefaultChecked?: boolean; // isDefaultChecked sounds weird to me?\n  /** Whether the checkbox is indeterminate */\n  isIndeterminate?: boolean;\n  /** Whether the checkbox is disabled */\n  isDisabled?: boolean;\n  /** Whether the checkbox & label should take the full available width of its container */\n  isFluid?: boolean;\n  /** The value of the checkbox */\n  value?: HtmlAttributes<'input'>['value']; // should this be required?\n  /** Whether the label should be displayed before the checkbox */\n  hasLeadingLabel?: boolean;\n  /** The callback function triggered when the checkbox value changes */\n  onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;\n  /** The callback function triggered when the checkbox is clicked */\n  onClick?: (event: React.MouseEvent<HTMLInputElement>) => void;\n} & (\n  | {\n      /** The aria label of the checkbox. Required if the label is not a string. */\n      ariaLabel: string;\n      // TODO v5: inconsistent naming. Naming the checkbox takes a union of\n      // `label` and `ariaLabel`, so the accessible name comes from a different\n      // prop depending on what the label renders. Combobox instead requires the\n      // label and clips it with `hasVisuallyHiddenLabel`, and TextInput reaches\n      // for htmlAttributes['aria-label'].\n      /** The label of the checkbox. If not a string, supply an aria label for screen reader support. */\n      label?: undefined;\n    }\n  | {\n      ariaLabel?: string;\n      label: React.ReactNode;\n    }\n);\n\nexport const Checkbox = ({\n  isChecked,\n  isDefaultChecked,\n  isIndeterminate = false,\n  isFluid = false,\n  isDisabled = false,\n  hasLeadingLabel,\n  ariaLabel,\n  onChange,\n  onClick,\n  value,\n  label,\n  className,\n  style,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: CommonProps<'input', CheckboxProps>) => {\n  const group = useFormFieldGroup('checkbox');\n  const {\n    'aria-describedby': htmlAriaDescribedBy,\n    'aria-invalid': htmlAriaInvalid,\n    ...inputHtmlAttributes\n  } = htmlAttributes ?? {};\n  const isDisabledState = isDisabled || group?.isDisabled === true;\n  const internalRef = useRef<HTMLInputElement>(null);\n  const mergedRefs = useMergeRefs([internalRef, ref]);\n\n  const [isCheckedState, internalOnChange] = useSemiControlledInput({\n    inputType: 'checkbox',\n    isControlled: isChecked !== undefined,\n    onChange,\n    state: isChecked ?? isDefaultChecked ?? false,\n  });\n\n  const checkboxClasses = classNames('ndl-checkbox', className, {\n    'ndl-disabled': isDisabledState,\n    'ndl-indeterminate': isIndeterminate,\n  });\n\n  useEffect(() => {\n    if (internalRef.current) {\n      internalRef.current.indeterminate = isIndeterminate;\n    }\n  }, [isIndeterminate]);\n\n  const { labelContent, fieldClassName } = useInlineFieldLabel({\n    ariaLabel,\n    componentName: 'Checkbox',\n    isDisabled: isDisabledState,\n    isFluid,\n    label,\n  });\n\n  return (\n    <label className={fieldClassName}>\n      {hasLeadingLabel === true && labelContent}\n      <input\n        ref={mergedRefs}\n        type=\"checkbox\"\n        aria-label={ariaLabel}\n        aria-describedby={htmlAriaDescribedBy}\n        aria-invalid={group?.hasError === true ? true : htmlAriaInvalid}\n        disabled={isDisabledState}\n        checked={isCheckedState}\n        name={group?.name}\n        onClick={onClick}\n        onChange={internalOnChange}\n        value={value}\n        style={style}\n        className={checkboxClasses}\n        {...restProps}\n        {...inputHtmlAttributes}\n      />\n      {Boolean(hasLeadingLabel) === false && labelContent}\n    </label>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsEE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AAKE,MAAA,aAAA,CAAA,oBACA,cAAA;AArBJ,MAAa,YAAY,SAiBkB;;CAjBlB,IAAA,EACvB,WACA,kBACA,kBAAkB,OAClB,UAAU,OACV,aAAa,OACb,iBACA,WACA,UACA,SACA,OACA,OACA,WACA,OACA,gBACA,QAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,QAAQ,kBAAkB,UAAU;CAC1C,MAAA,QAII,mBAAA,QAAA,mBAAA,KAAA,IAAA,iBAAkB,CAAC,GAJjB,EACJ,oBAAoB,qBACpB,gBAAgB,oBAAA,OACb,sBAAA,yBAAA,OAAA,UAAA;CAEL,MAAM,kBAAkB,eAAA,UAAA,QAAA,UAAA,KAAA,IAAA,KAAA,IAAc,MAAO,gBAAe;CAC5D,MAAM,cAAc,OAAyB,IAAI;CACjD,MAAM,aAAa,aAAa,CAAC,aAAa,GAAG,CAAC;CAElD,MAAM,CAAC,gBAAgB,oBAAoB,uBAAuB;EAChE,WAAW;EACX,cAAc,cAAc,KAAA;EAC5B;EACA,QAAA,QAAO,cAAA,QAAA,cAAA,KAAA,IAAA,YAAa,sBAAA,QAAA,UAAA,KAAA,IAAA,QAAoB;CAC1C,CAAC;CAED,MAAM,kBAAkB,WAAW,gBAAgB,WAAW;EAC5D,gBAAgB;EAChB,qBAAqB;CACvB,CAAC;CAED,gBAAgB;EACd,IAAI,YAAY,SACd,YAAY,QAAQ,gBAAgB;CAExC,GAAG,CAAC,eAAe,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