{"version":3,"file":"Radio.mjs","names":[],"sources":["../../../src/radio/Radio.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 */\nimport classNames from 'classnames';\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 RadioProps = {\n  /** Whether the radio is checked. Ignored when a parent `RadioGroup` has a `value` */\n  isChecked?: boolean;\n  /** Selects the radio by default in uncontrolled mode */\n  isDefaultChecked?: boolean;\n  /** Whether the radio is disabled */\n  isDisabled?: boolean;\n  /** Whether the radio & label should take the full available width of its container */\n  isFluid?: boolean;\n  /** Whether the label should be displayed before the radio */\n  hasLeadingLabel?: boolean;\n  /** The callback function triggered when the radio value changes */\n  onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;\n  /** The value of the radio */\n  value?: HtmlAttributes<'input'>['value'];\n  /** The callback function triggered when the radio is clicked */\n  onClick?: (event: React.MouseEvent<HTMLInputElement>) => void;\n} & (\n  | {\n      /** The aria label of the radio. Required if the label is not a string. */\n      ariaLabel: string;\n      // TODO v5: inconsistent naming. Naming the radio takes a union of `label`\n      // and `ariaLabel`, so the accessible name comes from a different prop\n      // 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 radio. 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 Radio = ({\n  ariaLabel,\n  isDisabled = false,\n  isDefaultChecked,\n  label,\n  isFluid = false,\n  isChecked,\n  hasLeadingLabel,\n  onChange,\n  className,\n  style,\n  onClick,\n  value,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: CommonProps<'input', RadioProps>) => {\n  const group = useFormFieldGroup('radio');\n  const { 'aria-describedby': htmlAriaDescribedBy, ...inputHtmlAttributes } =\n    htmlAttributes ?? {};\n  const isDisabledState = isDisabled || group?.isDisabled === true;\n  const isGroupControlled = group?.value !== undefined;\n  const isUsingNativeGroupState =\n    group !== undefined && !isGroupControlled && isChecked === undefined;\n  const [isCheckedState, internalOnChange] = useSemiControlledInput({\n    inputType: 'radio',\n    isControlled: isChecked !== undefined,\n    onChange,\n    state: isChecked ?? isDefaultChecked ?? false,\n  });\n  const isResolvedChecked = isGroupControlled\n    ? value !== undefined && String(value) === group?.value\n    : isCheckedState;\n\n  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {\n    internalOnChange(event);\n    group?.onChange?.(event);\n  };\n\n  const radioClasses = classNames('ndl-radio', className, {\n    'ndl-disabled': isDisabledState,\n  });\n\n  const { labelContent, fieldClassName } = useInlineFieldLabel({\n    ariaLabel,\n    componentName: 'Radio',\n    isDisabled: isDisabledState,\n    isFluid,\n    // TODO v5: decide whether all selection controls should truncate; today\n    // only Radio does.\n    isTruncated: true,\n    label,\n  });\n\n  return (\n    <label className={fieldClassName}>\n      {hasLeadingLabel === true && labelContent}\n      <input\n        ref={ref}\n        type=\"radio\"\n        aria-label={ariaLabel}\n        aria-describedby={htmlAriaDescribedBy}\n        disabled={isDisabledState}\n        checked={isUsingNativeGroupState ? undefined : isResolvedChecked}\n        defaultChecked={isUsingNativeGroupState ? isDefaultChecked : undefined}\n        name={group?.name}\n        required={group?.isRequired || undefined}\n        onClick={onClick}\n        onChange={handleChange}\n        value={value}\n        style={style}\n        className={radioClasses}\n        {...restProps}\n        {...inputHtmlAttributes}\n      />\n      {Boolean(hasLeadingLabel) === false && labelContent}\n    </label>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiEE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AAIQ,MAAA,aAAA,CAAA,kBAAA;AAlBV,MAAa,SAAS,SAgBkB;;CAhBlB,IAAA,EACpB,WACA,aAAa,OACb,kBACA,OACA,UAAU,OACV,WACA,iBACA,UACA,WACA,OACA,SACA,OACA,gBACA,QAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,QAAQ,kBAAkB,OAAO;CACvC,MAAA,QACE,mBAAA,QAAA,mBAAA,KAAA,IAAA,iBAAkB,CAAC,GADf,EAAE,oBAAoB,wBAAA,OAAwB,sBAAA,yBAAA,OAAA,UAAA;CAEpD,MAAM,kBAAkB,eAAA,UAAA,QAAA,UAAA,KAAA,IAAA,KAAA,IAAc,MAAO,gBAAe;CAC5D,MAAM,qBAAA,UAAA,QAAA,UAAA,KAAA,IAAA,KAAA,IAAoB,MAAO,WAAU,KAAA;CAC3C,MAAM,0BACJ,UAAU,KAAA,KAAa,CAAC,qBAAqB,cAAc,KAAA;CAC7D,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;CACD,MAAM,oBAAoB,oBACtB,UAAU,KAAA,KAAa,OAAO,KAAK,OAAA,UAAA,QAAA,UAAA,KAAA,IAAA,KAAA,IAAM,MAAO,SAChD;CAEJ,MAAM,gBAAgB,UAA+C;;EACnE,iBAAiB,KAAK;EACtB,UAAA,QAAA,UAAA,KAAA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