{"version":3,"file":"radioGroup.mjs","names":[],"sources":["../../../../package/common/radioGroup/radioGroup.tsx"],"sourcesContent":["import { useEffect, useId, useRef, useState, type ReactElement } from 'react';\nimport cx from 'classnames';\nimport Tooltip from 'components/common/tooltip';\n\nimport { useComponentDefaults } from '../configProvider';\nimport { RadioGroupOrientation, type RadioGroupProps, type RadioValue } from './radioGroup.types';\nimport styles from './radioGroup.module.pcss';\n\nfunction RadioGroup(props: RadioGroupProps): ReactElement {\n  const {\n    options = [],\n    label,\n    value,\n    defaultValue,\n    onChange = () => {},\n    orientation = RadioGroupOrientation.Vertical,\n    disabled = false,\n    size,\n    tabIndex = 0,\n    formValidator = null,\n    errorMessage = '',\n    isValid = true,\n    className,\n    style,\n    classNames,\n    styles: slotStyles,\n    ...rest\n  } = useComponentDefaults('RadioGroup', props);\n\n  const optionRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const fvRef = useRef(formValidator);\n  useEffect(() => {\n    fvRef.current = formValidator;\n  });\n  const [, forceUpdate] = useState(0);\n  const [internalValue, setInternalValue] = useState<RadioValue | undefined>(defaultValue);\n  const [isFocused, setIsFocused] = useState(false);\n  const [isHovered, setIsHovered] = useState(false);\n\n  useEffect(() => {\n    if (!formValidator) return;\n    if (label) formValidator.label = label;\n    formValidator.validate();\n    const listener = () => forceUpdate((n) => n + 1);\n    formValidator.registerOnUpdateListener(listener);\n    return () => formValidator.removeOnUpdateListener(listener);\n  }, [formValidator, label]);\n\n  const labelId = useId();\n\n  const hasError = formValidator ? formValidator.shouldShowError : !isValid || !!errorMessage;\n  const errorText = formValidator ? formValidator.getCurrentErrorMessage() : errorMessage || '';\n\n  const isControlled = value !== undefined && !formValidator;\n  const currentValue = (formValidator ? formValidator.value : isControlled ? value : internalValue) as\n    | RadioValue\n    | undefined;\n\n  function select(next: RadioValue, event?: React.MouseEvent | React.KeyboardEvent) {\n    if (formValidator) formValidator.set(next);\n    else if (!isControlled) setInternalValue(next);\n    onChange(next, event);\n  }\n\n  function handleBlur(event: React.FocusEvent<HTMLDivElement>) {\n    if (event.currentTarget.contains(event.relatedTarget as Node | null)) return;\n    setIsFocused(false);\n    if (fvRef.current) fvRef.current.touched = true;\n  }\n\n  const selectedIndex = options.findIndex((o) => o.value === currentValue);\n  const firstEnabledIndex = options.findIndex((o) => !o.disabled);\n  const rovingIndex = selectedIndex >= 0 ? selectedIndex : firstEnabledIndex;\n\n  function focusOption(index: number) {\n    optionRefs.current[index]?.focus();\n  }\n\n  function moveTo(step: 1 | -1, fromIndex: number, event: React.KeyboardEvent) {\n    if (disabled || options.length === 0) return;\n    let i = fromIndex;\n    for (let count = 0; count < options.length; count++) {\n      i = (i + step + options.length) % options.length;\n      if (!options[i].disabled) {\n        focusOption(i);\n        select(options[i].value, event);\n        return;\n      }\n    }\n  }\n\n  function handleKeyDown(event: React.KeyboardEvent, index: number) {\n    const option = options[index];\n    switch (event.key) {\n      case 'ArrowDown':\n      case 'ArrowRight':\n        event.preventDefault();\n        moveTo(1, index, event);\n        break;\n      case 'ArrowUp':\n      case 'ArrowLeft':\n        event.preventDefault();\n        moveTo(-1, index, event);\n        break;\n      case ' ':\n      case 'Enter':\n        event.preventDefault();\n        if (!disabled && !option.disabled) select(option.value, event);\n        break;\n      default:\n        break;\n    }\n  }\n\n  return (\n    <div\n      {...rest}\n      role='radiogroup'\n      aria-labelledby={label ? labelId : undefined}\n      aria-orientation={orientation}\n      aria-disabled={disabled || undefined}\n      aria-invalid={hasError || undefined}\n      className={cx(\n        styles.radioGroup,\n        orientation === RadioGroupOrientation.Horizontal && styles.radioGroupHorizontal,\n        disabled && styles.radioGroupDisabled,\n        hasError && styles.radioGroupError,\n        className,\n        classNames?.root,\n      )}\n      style={{ ...(size ? { '--radio-size': size } : null), ...style, ...slotStyles?.root } as React.CSSProperties}\n      onFocus={() => setIsFocused(true)}\n      onBlur={handleBlur}\n      onMouseEnter={() => setIsHovered(true)}\n      onMouseLeave={() => setIsHovered(false)}\n    >\n      {label && (\n        <div\n          id={labelId}\n          className={cx(styles.radioGroupGroupLabel, classNames?.groupLabel)}\n          style={slotStyles?.groupLabel}\n        >\n          {label}\n        </div>\n      )}\n      <div className={cx(styles.radioGroupOptions, classNames?.options)} style={slotStyles?.options}>\n        {options.map((option, index) => {\n          const checked = option.value === currentValue;\n          const optionDisabled = disabled || !!option.disabled;\n          return (\n            <div\n              key={String(option.value)}\n              ref={(el) => {\n                optionRefs.current[index] = el;\n              }}\n              role='radio'\n              aria-checked={checked}\n              aria-disabled={optionDisabled || undefined}\n              tabIndex={optionDisabled ? -1 : index === rovingIndex ? tabIndex : -1}\n              className={cx(\n                styles.radioGroupOption,\n                checked && styles.radioGroupOptionChecked,\n                optionDisabled && styles.radioGroupOptionDisabled,\n                classNames?.option,\n              )}\n              style={slotStyles?.option}\n              onClick={() => {\n                if (!optionDisabled) select(option.value);\n              }}\n              onKeyDown={(e) => handleKeyDown(e, index)}\n            >\n              <div\n                className={cx(styles.radioGroupRadio, checked && styles.radioGroupRadioChecked, classNames?.radio)}\n                style={slotStyles?.radio}\n              >\n                <div\n                  className={cx(styles.radioGroupDot, checked && styles.radioGroupDotChecked, classNames?.dot)}\n                  style={slotStyles?.dot}\n                />\n              </div>\n              {(option.label ?? option.value) !== '' && (\n                <div className={styles.radioGroupText}>\n                  <div className={cx(styles.radioGroupLabel, classNames?.label)} style={slotStyles?.label}>\n                    {option.label ?? option.value}\n                  </div>\n                  {option.description != null && (\n                    <div\n                      className={cx(styles.radioGroupDescription, classNames?.description)}\n                      style={slotStyles?.description}\n                    >\n                      {option.description}\n                    </div>\n                  )}\n                </div>\n              )}\n            </div>\n          );\n        })}\n      </div>\n      <Tooltip variant='error' message={errorText} isVisible={hasError && (isFocused || isHovered)} />\n    </div>\n  );\n}\n\nexport default RadioGroup;\n"],"mappings":"8XAQA,SAAS,EAAW,EAAsC,CACxD,GAAM,CACJ,UAAU,CAAC,EACX,QACA,QACA,eACA,eAAiB,CAAC,EAClB,cAAc,EAAsB,SACpC,WAAW,GACX,O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