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AG,CAAC,UAAC,MAA0B;gBACtC,OAAO,CACL,oBAAC,yBAAiB,qBAChB,OAAO,EAAE,MAAM,CAAC,GAAG,IACf,MAAM,IACV,GAAG,EAAE,MAAM,CAAC,GAAG,EACf,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,cAAc,EACxB,OAAO,EAAE,MAAM,CAAC,GAAG,KAAK,UAAU,EAClC,OAAO,EAAE,MAAM,CAAC,GAAG,KAAK,UAAU,EAClC,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,QAAQ,EACrC,EAAE,EAAE,WAAW,CAAC,MAAM,EAAE,EAAE,CAAC,EAC3B,OAAO,EAAE,MAAM,CAAC,OAAO,IAAO,OAAO,SAAI,MAAM,CAAC,GAAK,EACrD,IAAI,EAAE,IAAI,IAAI,EAAE,EAChB,QAAQ,EAAE,QAAQ,IAClB,CACH,CAAC;YACJ,CAAC,CAAC,CACE,CACF,CACF,CACP,CAAC;AACJ,CAAC,CAAC,CAAC;AACH,uBAAe,CAAC,WAAW,GAAG,cAAc,CAAC;AAE7C,SAAS,gBAAgB,CAAC,KAAwB;IAChD,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;QACzC,gFAAgF;QAChF,yBAAW,CAAC;YACV,IAAI,EAAE,cAAc;YACpB,KAAK,OAAA;YACL,iBAAiB,EAAE;gBACjB,WAAW,EAAE,oBAAoB;aAClC;SACF,CAAC,CAAC;KACJ;AACH,CAAC","sourcesContent":["import * as React from 'react';\nimport { Label } from '../../Label';\nimport {\n classNamesFunction,\n find,\n getNativeProps,\n divProperties,\n setFocusVisibility,\n useFocusRects,\n} from '../../Utilities';\nimport { ChoiceGroupOption } from './ChoiceGroupOption/index';\nimport { useId, useControllableValue, useMergedRefs, useWarnings } from '@fluentui/react-hooks';\nimport type { IRefObject } from '../../Utilities';\nimport type {\n IChoiceGroupOption,\n IChoiceGroupProps,\n IChoiceGroupStyleProps,\n IChoiceGroupStyles,\n IChoiceGroup,\n} from './ChoiceGroup.types';\nimport type { IChoiceGroupOptionProps } from './ChoiceGroupOption/ChoiceGroupOption.types';\n\nconst getClassNames = classNamesFunction<IChoiceGroupStyleProps, IChoiceGroupStyles>();\n\nconst getOptionId = (option: IChoiceGroupOption, id: string): string => {\n return `${id}-${option.key}`;\n};\n\nconst findOption = (options: IChoiceGroupOption[], key: IChoiceGroupProps['selectedKey']) => {\n return key === undefined ? undefined : find(options, value => value.key === key);\n};\n\nconst focusSelectedOption = (\n options: IChoiceGroupOption[],\n keyChecked: IChoiceGroupProps['selectedKey'],\n id: string,\n) => {\n const optionToFocus = findOption(options, keyChecked) || options.filter(option => !option.disabled)[0];\n const elementToFocus = optionToFocus && document.getElementById(getOptionId(optionToFocus, id));\n\n if (elementToFocus) {\n elementToFocus.focus();\n setFocusVisibility(true, elementToFocus as Element);\n }\n};\n\nconst focusFromFocusTrapZone = (evt: React.FocusEvent<HTMLElement>): boolean => {\n return evt.relatedTarget instanceof HTMLElement && evt.relatedTarget.dataset.isFocusTrapZoneBumper === 'true';\n};\n\nconst useComponentRef = (\n options: IChoiceGroupOption[],\n keyChecked: IChoiceGroupProps['selectedKey'],\n id: string,\n componentRef?: IRefObject<IChoiceGroup>,\n) => {\n React.useImperativeHandle(\n componentRef,\n () => ({\n get checkedOption() {\n return findOption(options, keyChecked);\n },\n focus() {\n focusSelectedOption(options, keyChecked, id);\n },\n }),\n [options, keyChecked, id],\n );\n};\n\nconst COMPONENT_NAME = 'ChoiceGroup';\n\nexport const ChoiceGroupBase: React.FunctionComponent<IChoiceGroupProps> = React.forwardRef<\n HTMLDivElement,\n IChoiceGroupProps\n>((props, forwardedRef) => {\n const {\n className,\n theme,\n styles,\n options = [],\n label,\n required,\n disabled,\n name,\n defaultSelectedKey,\n componentRef,\n onChange,\n } = props;\n const id = useId('ChoiceGroup');\n const labelId = useId('ChoiceGroupLabel');\n\n const divProps = getNativeProps<React.HTMLAttributes<HTMLDivElement>>(props, divProperties, [\n 'onChange',\n 'className',\n 'required',\n ]);\n\n const classNames = getClassNames(styles!, {\n theme: theme!,\n className,\n optionsContainIconOrImage: options.some(option => !!(option.iconProps || option.imageSrc)),\n });\n\n const ariaLabelledBy = props.ariaLabelledBy || (label ? labelId : props['aria-labelledby']);\n\n const [keyChecked, setKeyChecked] = useControllableValue(props.selectedKey, defaultSelectedKey);\n const [keyFocused, setKeyFocused] = React.useState<string | number>();\n\n const rootRef = React.useRef<HTMLDivElement | null>(null);\n const mergedRootRefs: React.Ref<HTMLDivElement> = useMergedRefs(rootRef, forwardedRef);\n\n useDebugWarnings(props);\n useComponentRef(options, keyChecked, id, componentRef);\n useFocusRects(rootRef);\n\n const onFocus = React.useCallback((ev?: React.FocusEvent<HTMLElement>, option?: IChoiceGroupOptionProps) => {\n if (option) {\n setKeyFocused(option.itemKey);\n option.onFocus?.(ev);\n }\n }, []);\n\n const onBlur = React.useCallback((ev: React.FocusEvent<HTMLElement>, option?: IChoiceGroupOptionProps) => {\n setKeyFocused(undefined);\n option?.onBlur?.(ev);\n }, []);\n\n const onOptionChange = React.useCallback(\n (evt?: React.FormEvent<HTMLElement | HTMLInputElement>, option?: IChoiceGroupOptionProps) => {\n if (!option) {\n return;\n }\n setKeyChecked(option.itemKey);\n\n option.onChange?.(evt);\n onChange?.(evt, findOption(options, option.itemKey));\n },\n [onChange, options, setKeyChecked],\n );\n\n const onRadioFocus = React.useCallback(\n (evt: React.FocusEvent<HTMLElement>) => {\n // Handles scenarios like this bug: https://github.com/microsoft/fluentui/issues/20173\n if (focusFromFocusTrapZone(evt)) {\n focusSelectedOption(options, keyChecked, id);\n }\n },\n [options, keyChecked, id],\n );\n\n return (\n <div className={classNames.root} {...divProps} ref={mergedRootRefs}>\n <div role=\"radiogroup\" {...(ariaLabelledBy && { 'aria-labelledby': ariaLabelledBy })} onFocus={onRadioFocus}>\n {label && (\n <Label className={classNames.label} required={required} id={labelId} disabled={disabled}>\n {label}\n </Label>\n )}\n <div className={classNames.flexContainer}>\n {options.map((option: IChoiceGroupOption) => {\n return (\n <ChoiceGroupOption\n itemKey={option.key}\n {...option}\n key={option.key}\n onBlur={onBlur}\n onFocus={onFocus}\n onChange={onOptionChange}\n focused={option.key === keyFocused}\n checked={option.key === keyChecked}\n disabled={option.disabled || disabled}\n id={getOptionId(option, id)}\n labelId={option.labelId || `${labelId}-${option.key}`}\n name={name || id}\n required={required}\n />\n );\n })}\n </div>\n </div>\n </div>\n );\n});\nChoiceGroupBase.displayName = COMPONENT_NAME;\n\nfunction useDebugWarnings(props: IChoiceGroupProps) {\n if (process.env.NODE_ENV !== 'production') {\n // eslint-disable-next-line react-hooks/rules-of-hooks -- build-time conditional\n useWarnings({\n name: COMPONENT_NAME,\n props,\n mutuallyExclusive: {\n selectedKey: 'defaultSelectedKey',\n },\n });\n }\n}\n"]}
\No newline at end of file